Skip to content

Commit

Permalink
Refactor RepoConfig and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rnestler committed Aug 19, 2024
1 parent 6cf8654 commit fd3997c
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions libgitcash/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::error::Error;

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct RepoConfig {
pub name: String,
pub currency: Currency,
Expand All @@ -15,14 +15,43 @@ impl RepoConfig {
pub fn load(repo_path: &Path) -> Result<Self, Error> {
let config_string = std::fs::read_to_string(repo_path.join("gitcash.toml"))
.map_err(|e| Error::RepoError(format!("Could not read gitcash.toml: {}", e)))?;
Self::from_str(&config_string)
}

pub fn from_str(config_string: &str) -> Result<Self, Error> {
let config: RepoConfig = toml::from_str(&config_string)
.map_err(|e| Error::RepoError(format!("Could not parse gitcash.toml: {}", e)))?;
Ok(config)
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct Currency {
pub code: String,
pub divisor: usize,
}

#[cfg(test)]
mod tests {
use crate::{Currency, RepoConfig};

#[test]
fn test_from_str() {
let repo_config_str = r#"name = "foo"
[currency]
code = "CHF"
divisor = 100"#;
println!("{}", repo_config_str);
let repo_config = RepoConfig::from_str(repo_config_str).unwrap();
assert_eq!(
RepoConfig {
name: "foo".to_owned(),
currency: Currency {
code: "CHF".to_owned(),
divisor: 100
}
},
repo_config
);
}
}

0 comments on commit fd3997c

Please sign in to comment.