From c29f1ba52950a2c9198e7b21e65ead2f65b13517 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 16 Mar 2025 16:38:38 +0200 Subject: [PATCH] separate traits --- src/line/list.rs | 24 +++--------------------- src/line/list/gemtext.rs | 26 ++++++++++++++++++++++++++ src/line/quote.rs | 25 ++++--------------------- src/line/quote/gemtext.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 42 deletions(-) create mode 100644 src/line/list/gemtext.rs create mode 100644 src/line/quote/gemtext.rs diff --git a/src/line/list.rs b/src/line/list.rs index 0b6e966..44d69f4 100644 --- a/src/line/list.rs +++ b/src/line/list.rs @@ -1,3 +1,6 @@ +pub mod gemtext; +pub use gemtext::Gemtext; + /// [List item](https://geminiprotocol.net/docs/gemtext-specification.gmi#list-items) tag pub const TAG: char = '*'; @@ -24,33 +27,12 @@ impl List { } } -pub trait Gemtext { - /// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self` - fn as_value(&self) -> Option<&str>; - /// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line - fn to_source(&self) -> String; -} - -impl Gemtext for str { - fn as_value(&self) -> Option<&str> { - self.strip_prefix(TAG).map(|s| s.trim()) - } - fn to_source(&self) -> String { - format!("{TAG} {}", self.trim()) - } -} - #[test] fn test() { const SOURCE: &str = "* Item"; const VALUE: &str = "Item"; - // test `List` let list = List::parse(SOURCE).unwrap(); assert_eq!(list.value, VALUE); assert_eq!(list.to_source(), SOURCE); - - // test `Gemtext` - assert_eq!(SOURCE.as_value(), Some(VALUE)); - assert_eq!(VALUE.to_source(), SOURCE) } diff --git a/src/line/list/gemtext.rs b/src/line/list/gemtext.rs new file mode 100644 index 0000000..5700287 --- /dev/null +++ b/src/line/list/gemtext.rs @@ -0,0 +1,26 @@ +use super::TAG; + +pub trait Gemtext { + /// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self` + fn as_value(&self) -> Option<&str>; + /// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line + fn to_source(&self) -> String; +} + +impl Gemtext for str { + fn as_value(&self) -> Option<&str> { + self.strip_prefix(TAG).map(|s| s.trim()) + } + fn to_source(&self) -> String { + format!("{TAG} {}", self.trim()) + } +} + +#[test] +fn test() { + const SOURCE: &str = "* Item"; + const VALUE: &str = "Item"; + + assert_eq!(SOURCE.as_value(), Some(VALUE)); + assert_eq!(VALUE.to_source(), SOURCE) +} diff --git a/src/line/quote.rs b/src/line/quote.rs index dbfb3fe..e098703 100644 --- a/src/line/quote.rs +++ b/src/line/quote.rs @@ -1,3 +1,6 @@ +pub mod gemtext; +pub use gemtext::Gemtext; + /// [Quote item](https://geminiprotocol.net/docs/gemtext-specification.gmi#quote-lines) tag pub const TAG: char = '>'; @@ -24,33 +27,13 @@ impl Quote { } } -pub trait Gemtext { - /// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self` - fn as_value(&self) -> Option<&str>; - /// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line - fn to_source(&self) -> String; -} - -impl Gemtext for str { - fn as_value(&self) -> Option<&str> { - self.strip_prefix(TAG).map(|s| s.trim()) - } - fn to_source(&self) -> String { - format!("{TAG} {}", self.trim()) - } -} - #[test] fn test() { const SOURCE: &str = "> Quote"; const VALUE: &str = "Quote"; - // test `Quote` let quote = Quote::parse(SOURCE).unwrap(); + assert_eq!(quote.value, VALUE); assert_eq!(quote.to_source(), SOURCE); - - // test `Gemtext` - assert_eq!(SOURCE.as_value(), Some(VALUE)); - assert_eq!(VALUE.to_source(), SOURCE) } diff --git a/src/line/quote/gemtext.rs b/src/line/quote/gemtext.rs new file mode 100644 index 0000000..e55d2f3 --- /dev/null +++ b/src/line/quote/gemtext.rs @@ -0,0 +1,26 @@ +use super::TAG; + +pub trait Gemtext { + /// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self` + fn as_value(&self) -> Option<&str>; + /// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line + fn to_source(&self) -> String; +} + +impl Gemtext for str { + fn as_value(&self) -> Option<&str> { + self.strip_prefix(TAG).map(|s| s.trim()) + } + fn to_source(&self) -> String { + format!("{TAG} {}", self.trim()) + } +} + +#[test] +fn test() { + const SOURCE: &str = "> Quote"; + const VALUE: &str = "Quote"; + + assert_eq!(SOURCE.as_value(), Some(VALUE)); + assert_eq!(VALUE.to_source(), SOURCE) +}