From ce92e736afc8c64cf096da41c46626ac0ff6b6e8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 5 Dec 2021 17:02:24 +0100 Subject: Make state directory path an own type For more typesafety with smaller API surface of the type. Signed-off-by: Matthias Beyer --- src/commands/profile.rs | 2 +- src/profile.rs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/commands/profile.rs b/src/commands/profile.rs index 98fbc08..dbb55bf 100644 --- a/src/commands/profile.rs +++ b/src/commands/profile.rs @@ -14,7 +14,7 @@ pub async fn profile(matches: &ArgMatches) -> Result<()> { async fn profile_create(matches: &ArgMatches) -> Result<()> { let name = matches.value_of("name").map(String::from).unwrap(); // required let state_dir = Profile::state_dir_path(&name)?; - log::info!("Creating '{}' in {}", name, state_dir.display()); + log::info!("Creating '{}' in {}", name, state_dir.path().display()); let profile = Profile::create(&state_dir, &name, Config::default()).await?; log::info!("Saving..."); diff --git a/src/profile.rs b/src/profile.rs index abbb92d..671df26 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -18,7 +18,7 @@ pub struct Profile { } impl Profile { - pub async fn create(state_dir: &Path, name: &str, config: Config) -> Result { + pub async fn create(state_dir: &StateDir, name: &str, config: Config) -> Result { let bootstrap = vec![]; // TODO let mdns = false; // TODO let keypair = ipfs::Keypair::generate_ed25519(); @@ -66,8 +66,8 @@ impl Profile { client.post_text_node(vec![], text).await } - async fn ipfs_path(state_dir: &Path, name: &str) -> Result { - let path = state_dir.join(name).join("ipfs"); + async fn ipfs_path(state_dir: &StateDir, name: &str) -> Result { + let path = state_dir.path().join(name).join("ipfs"); tokio::fs::create_dir_all(&path).await?; Ok(path) } @@ -86,11 +86,13 @@ impl Profile { }) } - pub fn state_dir_path(name: &str) -> Result { + pub fn state_dir_path(name: &str) -> Result { xdg::BaseDirectories::with_prefix("distrox") .map_err(anyhow::Error::from) .and_then(|dirs| { - dirs.create_state_directory(name).map_err(anyhow::Error::from) + dirs.create_state_directory(name) + .map(StateDir::from) + .map_err(anyhow::Error::from) }) } @@ -134,6 +136,21 @@ impl Profile { } +#[derive(Debug)] +pub struct StateDir(PathBuf); + +impl StateDir { + pub fn path(&self) -> &Path { + &self.0 + } +} + +impl From for StateDir { + fn from(p: PathBuf) -> Self { + Self(p) + } +} + #[derive(getset::Getters)] pub struct ProfileState { #[getset(get = "pub")] @@ -171,12 +188,12 @@ impl ProfileStateSaveable { }) } - pub async fn save_to_disk(&self, state_dir_path: &Path) -> Result<()> { + pub async fn save_to_disk(&self, state_dir_path: &StateDir) -> Result<()> { let state_s = serde_json::to_string(&self)?; tokio::fs::OpenOptions::new() .create(true) .truncate(true) - .open(state_dir_path.join("profile_state")) + .open(state_dir_path.path().join("profile_state")) .await? .write_all(state_s.as_bytes()) .await @@ -184,10 +201,10 @@ impl ProfileStateSaveable { .map_err(anyhow::Error::from) } - pub async fn load_from_disk(state_dir_path: &Path) -> Result { + pub async fn load_from_disk(state_dir_path: &StateDir) -> Result { let reader = tokio::fs::OpenOptions::new() .read(true) - .open(state_dir_path.join("profile_state")) + .open(state_dir_path.path().join("profile_state")) .await? .into_std() .await; -- cgit v1.2.3