| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
pub trait Deb822LikeParagraph: FromIterator<(String, String)> { |
| 5 |
|
| 6 |
fn get(&self, key: &str) -> Option<String>; |
| 7 |
|
| 8 |
|
| 9 |
fn set(&mut self, key: &str, value: &str); |
| 10 |
|
| 11 |
|
| 12 |
fn remove(&mut self, key: &str); |
| 13 |
} |
| 14 |
|
| 15 |
impl Deb822LikeParagraph for crate::Paragraph { |
| 16 |
fn get(&self, key: &str) -> Option<String> { |
| 17 |
crate::Paragraph::get(self, key).map(|v| v.to_string()) |
| 18 |
} |
| 19 |
|
| 20 |
fn set(&mut self, key: &str, value: &str) { |
| 21 |
crate::Paragraph::set(self, key, value); |
| 22 |
} |
| 23 |
|
| 24 |
fn remove(&mut self, key: &str) { |
| 25 |
crate::Paragraph::remove(self, key); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
pub trait FromDeb822Paragraph<P: Deb822LikeParagraph> { |
| 31 |
|
| 32 |
fn from_paragraph(paragraph: &P) -> Result<Self, String> |
| 33 |
where |
| 34 |
Self: Sized; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
pub trait ToDeb822Paragraph<P: Deb822LikeParagraph> { |
| 39 |
|
| 40 |
fn to_paragraph(&self) -> P; |
| 41 |
|
| 42 |
|
| 43 |
fn update_paragraph(&self, paragraph: &mut P); |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(test)] |
| 47 |
mod tests { |
| 48 |
#[cfg(feature = "derive")] |
| 49 |
use super::*; |
| 50 |
|
| 51 |
#[cfg(feature = "derive")] |
| 52 |
mod derive { |
| 53 |
use super::*; |
| 54 |
use crate as deb822_fast; |
| 55 |
use crate::{FromDeb822, ToDeb822}; |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn test_derive() { |
| 59 |
#[derive(ToDeb822)] |
| 60 |
struct Foo { |
| 61 |
bar: String, |
| 62 |
baz: i32, |
| 63 |
blah: Option<String>, |
| 64 |
} |
| 65 |
|
| 66 |
let foo = Foo { |
| 67 |
bar: "hello".to_string(), |
| 68 |
baz: 42, |
| 69 |
blah: None, |
| 70 |
}; |
| 71 |
|
| 72 |
let paragraph: crate::Paragraph = foo.to_paragraph(); |
| 73 |
assert_eq!(paragraph.get("bar"), Some("hello")); |
| 74 |
assert_eq!(paragraph.get("baz"), Some("42")); |
| 75 |
assert_eq!(paragraph.get("blah"), None); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn test_optional_missing() { |
| 80 |
#[derive(ToDeb822)] |
| 81 |
struct Foo { |
| 82 |
bar: String, |
| 83 |
baz: Option<String>, |
| 84 |
} |
| 85 |
|
| 86 |
let foo = Foo { |
| 87 |
bar: "hello".to_string(), |
| 88 |
baz: None, |
| 89 |
}; |
| 90 |
|
| 91 |
let paragraph: crate::Paragraph = foo.to_paragraph(); |
| 92 |
assert_eq!(paragraph.get("bar"), Some("hello")); |
| 93 |
assert_eq!(paragraph.get("baz"), None); |
| 94 |
|
| 95 |
assert_eq!("bar: hello\n", paragraph.to_string()); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_deserialize_with() { |
| 100 |
let mut para: crate::Paragraph = |
| 101 |
"bar: bar\n# comment\nbaz: blah\n".parse().unwrap(); |
| 102 |
|
| 103 |
fn to_bool(s: &str) -> Result<bool, String> { |
| 104 |
Ok(s == "ja") |
| 105 |
} |
| 106 |
|
| 107 |
fn from_bool(s: &bool) -> String { |
| 108 |
if *s { |
| 109 |
"ja".to_string() |
| 110 |
} else { |
| 111 |
"nee".to_string() |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#[derive(FromDeb822, ToDeb822)] |
| 116 |
struct Foo { |
| 117 |
bar: String, |
| 118 |
#[deb822(deserialize_with = to_bool, serialize_with = from_bool)] |
| 119 |
baz: bool, |
| 120 |
} |
| 121 |
|
| 122 |
let mut foo: Foo = Foo::from_paragraph(¶).unwrap(); |
| 123 |
assert_eq!(foo.bar, "bar"); |
| 124 |
assert!(!foo.baz); |
| 125 |
|
| 126 |
foo.bar = "new".to_string(); |
| 127 |
|
| 128 |
foo.update_paragraph(&mut para); |
| 129 |
|
| 130 |
assert_eq!(para.get("bar"), Some("new")); |
| 131 |
assert_eq!(para.get("baz"), Some("nee")); |
| 132 |
assert_eq!(para.to_string(), "bar: new\nbaz: nee\n"); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn test_update_remove() { |
| 137 |
let mut para: crate::Paragraph = |
| 138 |
"bar: bar\n# comment\nbaz: blah\n".parse().unwrap(); |
| 139 |
|
| 140 |
#[derive(FromDeb822, ToDeb822)] |
| 141 |
struct Foo { |
| 142 |
bar: Option<String>, |
| 143 |
baz: String, |
| 144 |
} |
| 145 |
|
| 146 |
let mut foo: Foo = Foo::from_paragraph(¶).unwrap(); |
| 147 |
assert_eq!(foo.bar, Some("bar".to_string())); |
| 148 |
assert_eq!(foo.baz, "blah"); |
| 149 |
|
| 150 |
foo.bar = None; |
| 151 |
|
| 152 |
foo.update_paragraph(&mut para); |
| 153 |
|
| 154 |
assert_eq!(para.get("bar"), None); |
| 155 |
assert_eq!(para.get("baz"), Some("blah")); |
| 156 |
assert_eq!(para.to_string(), "baz: blah\n"); |
| 157 |
} |
| 158 |
} |
| 159 |
} |