Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(octez): serialise directory #668

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions crates/octez/src/async/directory.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
use std::path::PathBuf;
use std::{fmt::Display, path::PathBuf};

use anyhow::{bail, Result};
use serde_with::SerializeDisplay;
use tempfile::TempDir;

#[derive(Debug)]
#[derive(Debug, SerializeDisplay)]
pub enum Directory {
TempDir(TempDir),
Path(PathBuf),
}

impl Display for Directory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Directory::Path(p) => p.to_string_lossy(),
Directory::TempDir(p) => p.path().to_string_lossy(),
};
write!(f, "{}", s)
}
}

impl Default for Directory {
fn default() -> Self {
Self::TempDir(TempDir::new().unwrap())
Expand Down Expand Up @@ -121,4 +132,32 @@ mod test {
let path_buf: PathBuf = (&directory).into();
assert_eq!(path_buf, dir_path);
}

#[test]
fn serialize() {
let temp_dir = TempDir::new().unwrap();
let dir_path = temp_dir.path().to_path_buf();
let directory = Directory::Path(dir_path.clone());
assert_eq!(
serde_json::to_value(&directory).unwrap(),
serde_json::json!(dir_path.to_string_lossy())
);

let directory = Directory::TempDir(temp_dir);
assert_eq!(
serde_json::to_value(&directory).unwrap(),
serde_json::json!(dir_path.to_string_lossy())
);
}

#[test]
fn display() {
let temp_dir = TempDir::new().unwrap();
let dir_path = temp_dir.path().to_path_buf();
let directory = Directory::Path(dir_path.clone());
assert_eq!(directory.to_string(), dir_path.to_str().unwrap());

let directory = Directory::TempDir(temp_dir);
assert_eq!(directory.to_string(), dir_path.to_str().unwrap());
}
}