summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThang Pham <phamducthang1234@gmail.com>2022-05-28 16:08:06 -0400
committerThang Pham <phamducthang1234@gmail.com>2022-05-28 16:08:06 -0400
commit0a4b3badeaab3aba3a61ac046810a3be967ce2d6 (patch)
treef5e04a7979247d15085bd6db8dc897bb2e7265aa
parent83ee1dcf52e2e5638490f958e1ab808133881a57 (diff)
bump `config_parser2` to `v0.1.3`
- added `ConfigParser` default implementation for `Option<T>` - added more tests, cleanup test structure naming
-rw-r--r--Cargo.lock2
-rw-r--r--config_parser/Cargo.toml2
-rw-r--r--config_parser/src/lib.rs12
-rw-r--r--config_parser/tests/test.rs178
4 files changed, 144 insertions, 50 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0f9f0d5..f1148ed 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -134,7 +134,7 @@ dependencies = [
[[package]]
name = "config_parser2"
-version = "0.1.2"
+version = "0.1.3"
dependencies = [
"anyhow",
"config_parser_derive",
diff --git a/config_parser/Cargo.toml b/config_parser/Cargo.toml
index 91e4eb7..87a8b7b 100644
--- a/config_parser/Cargo.toml
+++ b/config_parser/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "config_parser2"
-version = "0.1.2"
+version = "0.1.3"
authors = ["Thang Pham <phamducthang1234@gmail.com>"]
edition = "2018"
license = "MIT"
diff --git a/config_parser/src/lib.rs b/config_parser/src/lib.rs
index 760bb1f..0f05ced 100644
--- a/config_parser/src/lib.rs
+++ b/config_parser/src/lib.rs
@@ -20,6 +20,18 @@ macro_rules! config_parser_impl {
};
}
+impl<'de, T> ConfigParser for Option<T>
+where
+ T: serde::de::Deserialize<'de>,
+{
+ fn parse(&mut self, value: toml::Value) -> Result<()> {
+ if let Ok(value) = value.try_into::<T>() {
+ *self = Some(value);
+ }
+ Ok(())
+ }
+}
+
impl<'de, T> ConfigParser for Vec<T>
where
T: serde::de::Deserialize<'de>,
diff --git a/config_parser/tests/test.rs b/config_parser/tests/test.rs
index 6b9614f..34b50d6 100644
--- a/config_parser/tests/test.rs
+++ b/config_parser/tests/test.rs
@@ -3,26 +3,31 @@ use serde::Deserialize;
#[derive(ConfigParse, Deserialize, Default, Debug, PartialEq)]
struct A {
- pub field_1: String,
- pub field_2: u32,
- pub field_3: bool,
- pub field_4: Vec<B>,
- pub field_5: C,
+ pub a1: String,
+ pub a2: u32,
+ pub a3: bool,
+ pub a4: Vec<B>,
+ pub a5: C,
}
#[derive(ConfigParse, Deserialize, Default, Debug, PartialEq)]
struct B {
#[serde(default)]
- pub field_1: String,
+ pub b1: String,
#[serde(default)]
- pub field_2: String,
+ pub b2: String,
}
#[derive(ConfigParse, Deserialize, Default, Debug, PartialEq)]
struct C {
- pub field_1: B,
- pub field_2: bool,
- pub field_3: bool,
+ pub c1: B,
+ pub c2: bool,
+ pub c3: bool,
+}
+
+#[derive(ConfigParse, Deserialize, Default, Debug, PartialEq)]
+struct D {
+ pub d1: Option<B>,
}
#[cfg(test)]
@@ -31,6 +36,83 @@ mod tests {
use config_parser2::ConfigParser;
#[test]
+ fn option_test_none_to_none() {
+ let mut value = D { d1: None };
+ let toml = "".parse::<toml::Value>().unwrap();
+ let expected_value = D { d1: None };
+
+ value.parse(toml).unwrap();
+ assert!(value == expected_value);
+ }
+
+ #[test]
+ fn option_test_none_to_some() {
+ let mut value = D { d1: None };
+ let toml = "
+[d1]
+b1 = 'd1.b1'
+b2 = 'd1.b2'
+"
+ .parse::<toml::Value>()
+ .unwrap();
+ let expected_value = D {
+ d1: Some(B {
+ b1: "d1.b1".to_owned(),
+ b2: "d1.b2".to_owned(),
+ }),
+ };
+
+ value.parse(toml).unwrap();
+ assert!(value == expected_value);
+ }
+
+ #[test]
+ fn option_test_some_to_none() {
+ let mut value = D {
+ d1: Some(B {
+ b1: "d1.b1".to_owned(),
+ b2: "d1.b2".to_owned(),
+ }),
+ };
+ let toml = "".parse::<toml::Value>().unwrap();
+ let expected_value = D {
+ d1: Some(B {
+ b1: "d1.b1".to_owned(),
+ b2: "d1.b2".to_owned(),
+ }),
+ };
+
+ value.parse(toml).unwrap();
+ assert!(value == expected_value);
+ }
+
+ #[test]
+ fn option_test_some_to_some() {
+ let mut value = D {
+ d1: Some(B {
+ b1: "d1.b1".to_owned(),
+ b2: "d1.b2".to_owned(),
+ }),
+ };
+ let toml = "
+[d1]
+b1 = 'd1.b1_new'
+b2 = 'd1.b2_new'
+"
+ .parse::<toml::Value>()
+ .unwrap();
+ let expected_value = D {
+ d1: Some(B {
+ b1: "d1.b1_new".to_owned(),
+ b2: "d1.b2_new".to_owned(),
+ }),
+ };
+
+ value.parse(toml).unwrap();
+ assert!(value == expected_value);
+ }
+
+ #[test]
fn simple_test() {
let value = "a = ['b', 'c', 'd']".parse::<toml::Value>().unwrap();
let mut a: Vec<String> = vec![];
@@ -41,65 +123,65 @@ mod tests {
#[test]
fn complex_test() {
let mut value = A {
- field_1: "field_1".to_owned(),
- field_2: 510,
- field_3: false,
- field_4: vec![B {
- field_1: "b_field_1".to_owned(),
- field_2: "b_field_2".to_owned(),
+ a1: "a1".to_owned(),
+ a2: 510,
+ a3: false,
+ a4: vec![B {
+ b1: "a4.b1".to_owned(),
+ b2: "a4.b2".to_owned(),
}],
- field_5: C {
- field_1: B {
- field_1: "cb_field_1".to_owned(),
- field_2: "cb_field_2".to_owned(),
+ a5: C {
+ c1: B {
+ b1: "a5.c1.b1".to_owned(),
+ b2: "a5.c1.b2".to_owned(),
},
- field_2: true,
- field_3: false,
+ c2: true,
+ c3: false,
},
};
let toml = "
-field_1 = 'new_field_1'
-field_2 = 150
+a1 = 'a1_new'
+a2 = 150
-[[field_4]]
-field_2 = 'new_field_2'
+[[a4]]
+b2 = 'a4.b2'
-[[field_4]]
-field_1 = 'new_field_1'
+[[a4]]
+b1 = 'a4.b1'
-[field_5.field_1]
-field_1 = 'new_field_1'
-field_2 = 'new_field_2'
+[a5.c1]
+b1 = 'a5.c1.b1_new'
+b2 = 'a5.c1.b2_new'
-[field_5]
-field_2 = false
-field_3 = true
+[a5]
+c2 = false
+c3 = true
"
.parse::<toml::Value>()
.unwrap();
let expected_value = A {
- field_1: "new_field_1".to_owned(),
- field_2: 150,
- field_3: false,
- field_4: vec![
+ a1: "a1_new".to_owned(),
+ a2: 150,
+ a3: false,
+ a4: vec![
B {
- field_1: "".to_owned(),
- field_2: "new_field_2".to_owned(),
+ b1: "".to_owned(),
+ b2: "a4.b2".to_owned(),
},
B {
- field_1: "new_field_1".to_owned(),
- field_2: "".to_owned(),
+ b1: "a4.b1".to_owned(),
+ b2: "".to_owned(),
},
],
- field_5: C {
- field_1: B {
- field_1: "new_field_1".to_owned(),
- field_2: "new_field_2".to_owned(),
+ a5: C {
+ c1: B {
+ b1: "a5.c1.b1_new".to_owned(),
+ b2: "a5.c1.b2_new".to_owned(),
},
- field_2: false,
- field_3: true,
+ c2: false,
+ c3: true,
},
};