aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-06-10 02:51:31 +0000
committerFuwn <[email protected]>2022-06-10 02:51:31 +0000
commitd369b4ef5cbec03b4b0152a93052a32a7cc4ed8d (patch)
tree7ca3424d4aced133de448e3bfa8db39babb43858
parenttest(meta): add no params test (diff)
downloadgerm-d369b4ef5cbec03b4b0152a93052a32a7cc4ed8d.tar.xz
germ-d369b4ef5cbec03b4b0152a93052a32a7cc4ed8d.zip
feat(meta): impl ToString for Meta
-rw-r--r--src/meta.rs19
-rw-r--r--tests/meta.rs20
2 files changed, 39 insertions, 0 deletions
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<String, String>,
}
+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::<Vec<_>>();
+
+ 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
@@ -21,6 +21,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");