Skip to content

Commit

Permalink
Merge pull request #19 from tkhr0/bump-toml
Browse files Browse the repository at this point in the history
bump toml crate
  • Loading branch information
tkhr0 committed Dec 2, 2023
2 parents bc9f70c + 1e34c17 commit ca743d1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 38 deletions.
111 changes: 89 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ digest = "0.8.1"
byteorder = "1"
base32 = "0.4.0"
clap = "2.33"
toml = "0.5"
serde = { version = "1.0.0", features = ["derive"] }
toml = "0.8"
regex = "1"
19 changes: 8 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ impl Config {
}

// Serialize to strings
pub fn serialize(&self) -> Result<Vec<u8>, String> {
match toml::to_vec(&self) {
pub fn serialize(&self) -> Result<String, String> {
match toml::to_string(&self) {
Ok(data) => Ok(data),
Err(err) => Err(err.to_string()),
}
}

// Deserialize config from strings
pub fn deserialize(&mut self, content: Vec<u8>) -> Result<(), String> {
match toml::from_slice(&content) {
pub fn deserialize(&mut self, content: &str) -> Result<(), String> {
match toml::from_str(content) {
Ok(config) => {
*self = config;
Ok(())
Expand Down Expand Up @@ -202,9 +202,9 @@ mod tests {
#[test]
fn serialize_profile() {
let profile = Profile::new("test", "secret");
let expected = b"name = \"test\"\nsecret = \"secret\"\n";
let expected = "name = \"test\"\nsecret = \"secret\"\n";

assert_eq!(toml::to_vec(&profile).unwrap(), expected);
assert_eq!(toml::to_string(&profile).unwrap(), expected);
}

#[test]
Expand All @@ -217,15 +217,12 @@ name = "test"
secret = "secret"
"#;

assert_eq!(
String::from_utf8(config.serialize().unwrap()).unwrap(),
expected
);
assert_eq!(config.serialize().unwrap(), expected);
}

#[test]
fn deserialize_config() {
let string_config = b"[[profiles]]\nname = \"test\"\nsecret = \"secret\"\n ".to_vec();
let string_config = "[[profiles]]\nname = \"test\"\nsecret = \"secret\"\n ";
let mut config: Config = Default::default();

config.deserialize(string_config).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/mfa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Mfa {
Ok(file) => file,
Err(err) => return Err(err.to_string()),
};
match file.write_all(&config_data) {
match file.write_all(config_data.as_bytes()) {
Ok(()) => Ok(()),
Err(err) => Err(err.to_string()),
}
Expand All @@ -109,12 +109,12 @@ impl Mfa {
Ok(file) => file,
Err(err) => return Err(err.to_string()),
};
let mut buffer = Vec::new();
if let Err(err) = file.read_to_end(&mut buffer) {
let mut contents = String::new();
if let Err(err) = file.read_to_string(&mut contents) {
return Err(err.to_string());
};

self.config.deserialize(buffer)
self.config.deserialize(&contents)
}

// Run setup steps.
Expand Down

0 comments on commit ca743d1

Please sign in to comment.