Skip to content

Commit

Permalink
v0.1.5
Browse files Browse the repository at this point in the history
- Added Plain variant to FileType enum.
  • Loading branch information
edfloreshz committed Feb 11, 2024
1 parent 81f788b commit 338afc3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libset"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
license = "GPL-2.0"
description = "A configuration file management library for Rust applications."
Expand Down
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,12 @@ impl Config {
///
/// A `Result` containing the file path or an `Error` if an error occurred.
pub fn path(&self, key: &str, file_type: FileType) -> Result<PathBuf, Error> {
let path = self
.path
.join(sanitize_name(&format!("{key}.{file_type}"))?);
let name = if FileType::Plain == file_type {
key.to_string()
} else {
format!("{key}.{file_type}")
};
let path = self.path.join(sanitize_name(&name)?);
info!("Found key {}.", key);
Ok(path)
}
Expand Down Expand Up @@ -314,6 +317,7 @@ impl Get for Config {
FileType::Json => serde_json::from_str(&data)?,
#[cfg(feature = "ron")]
FileType::Ron => ron::from_str(&data)?,
FileType::Plain => unreachable!("Never get plain text with get method."),
};
info!("Retrieved file from {}.", key_path.display());
Ok(t)
Expand All @@ -340,6 +344,7 @@ impl Set for Config {
FileType::Json => serde_json::to_string_pretty(&value)?,
#[cfg(feature = "ron")]
FileType::Ron => ron::ser::to_string_pretty(&value, ron::ser::PrettyConfig::new())?,
FileType::Plain => unreachable!("Never get plain text with get method."),
};
atomicwrites::AtomicFile::new(&key_path, atomicwrites::OverwriteBehavior::AllowOverwrite)
.write(|file| file.write_all(data.as_bytes()))?;
Expand Down
4 changes: 3 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use tracing::error;

use crate::Error;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FileType {
Plain,
#[cfg(feature = "toml")]
Toml,
#[cfg(feature = "json")]
Expand All @@ -23,6 +24,7 @@ impl Display for FileType {
FileType::Json => write!(f, "json"),
#[cfg(feature = "ron")]
FileType::Ron => write!(f, "ron"),
FileType::Plain => write!(f, ""),
}
}
}
Expand Down

0 comments on commit 338afc3

Please sign in to comment.