From d369b4ef5cbec03b4b0152a93052a32a7cc4ed8d Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 10 Jun 2022 02:51:31 +0000 Subject: feat(meta): impl ToString for Meta --- src/meta.rs | 19 +++++++++++++++++++ tests/meta.rs | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/meta.rs b/src/meta.rs index 9ff2712..3973d3b 100644 --- a/src/meta.rs +++ b/src/meta.rs @@ -27,6 +27,25 @@ pub struct Meta { /// The parameters of a Gemini response parameters: HashMap, } +impl ToString for Meta { + fn to_string(&self) -> String { + format!("{}{}", self.mime, { + let mut parameters = self + .parameters + .iter() + .map(|(k, v)| format!("{}={}", *k, v)) + .collect::>(); + + parameters.reverse(); + + if parameters.is_empty() { + "".to_string() + } else { + format!("; {}", parameters.join("; ")) + } + }) + } +} impl Meta { #[must_use] pub fn from_string(meta: &str) -> Self { diff --git a/tests/meta.rs b/tests/meta.rs index 5690850..2bcccf1 100644 --- a/tests/meta.rs +++ b/tests/meta.rs @@ -20,6 +20,26 @@ mod test { use germ::meta::Meta; + #[test] + fn meta_to_string_without_parameters() { + let original_string = "text/gemini"; + + assert_eq!( + Meta::from_string(original_string).to_string(), + original_string + ); + } + + #[test] + fn meta_to_string_with_parameters() { + let original_string = "text/gemini; hi=2; hi2=string=2"; + + assert_eq!( + Meta::from_string(original_string).to_string(), + original_string + ); + } + #[test] fn meta_to_mime_without_parameters() { let meta = Meta::from_string("text/gemini"); -- cgit v1.2.3