diff options
| author | Fuwn <[email protected]> | 2022-06-10 03:02:16 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-06-10 03:02:16 +0000 |
| commit | ff518622bfed876d92cad857df09591e9f2a89c8 (patch) | |
| tree | b5fe728cb0fe884dbcafc89c8fb8eea5c20cfc4f | |
| parent | feat(meta): impl ToString for Meta (diff) | |
| download | germ-ff518622bfed876d92cad857df09591e9f2a89c8.tar.xz germ-ff518622bfed876d92cad857df09591e9f2a89c8.zip | |
feat(meta): mutable access to meta for construction
| -rw-r--r-- | src/meta.rs | 11 | ||||
| -rw-r--r-- | tests/meta.rs | 23 |
2 files changed, 33 insertions, 1 deletions
diff --git a/src/meta.rs b/src/meta.rs index 3973d3b..2aaa543 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -20,7 +20,7 @@ use std::collections::HashMap; /// Structure-ize a Gemini response's meta section into it's mime type and it's /// parameters. -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Meta { /// The mime type of a Gemini response mime: String, @@ -48,6 +48,9 @@ impl ToString for Meta { } impl Meta { #[must_use] + pub fn new() -> Self { Self::default() } + + #[must_use] pub fn from_string(meta: &str) -> Self { let mut metas = meta.split(';'); let mime = metas.next().unwrap_or("").to_string(); @@ -73,8 +76,14 @@ impl Meta { #[must_use] pub fn mime(&self) -> &str { &self.mime } + pub fn mime_mut(&mut self) -> &mut String { &mut self.mime } + #[must_use] pub const fn parameters(&self) -> &HashMap<String, String> { &self.parameters } + + pub fn parameters_mut(&mut self) -> &mut HashMap<String, String> { + &mut self.parameters + } } diff --git a/tests/meta.rs b/tests/meta.rs index 2bcccf1..70c8adc 100644 --- a/tests/meta.rs +++ b/tests/meta.rs @@ -21,6 +21,29 @@ mod test { use germ::meta::Meta; #[test] + fn construct_meta_with_mime() { + let mut meta = Meta::new(); + + *meta.mime_mut() = "text/gemini".to_string(); + + assert_eq!(meta.to_string(), "text/gemini"); + } + + #[test] + fn construct_meta_with_mime_and_parameters() { + let mut meta = Meta::new(); + let mut parameters = std::collections::HashMap::new(); + + parameters.insert("hi".to_string(), "2".to_string()); + parameters.insert("hi2".to_string(), "string=2".to_string()); + + *meta.mime_mut() = "text/gemini".to_string(); + *meta.parameters_mut() = parameters; + + assert_eq!(meta.to_string(), "text/gemini; hi=2; hi2=string=2"); + } + + #[test] fn meta_to_string_without_parameters() { let original_string = "text/gemini"; |