separate traits

This commit is contained in:
yggverse 2025-03-16 16:38:38 +02:00
parent 5b751e3c7a
commit c29f1ba529
4 changed files with 59 additions and 42 deletions

View file

@ -1,3 +1,6 @@
pub mod gemtext;
pub use gemtext::Gemtext;
/// [List item](https://geminiprotocol.net/docs/gemtext-specification.gmi#list-items) tag /// [List item](https://geminiprotocol.net/docs/gemtext-specification.gmi#list-items) tag
pub const TAG: char = '*'; 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] #[test]
fn test() { fn test() {
const SOURCE: &str = "* Item"; const SOURCE: &str = "* Item";
const VALUE: &str = "Item"; const VALUE: &str = "Item";
// test `List`
let list = List::parse(SOURCE).unwrap(); let list = List::parse(SOURCE).unwrap();
assert_eq!(list.value, VALUE); assert_eq!(list.value, VALUE);
assert_eq!(list.to_source(), SOURCE); assert_eq!(list.to_source(), SOURCE);
// test `Gemtext`
assert_eq!(SOURCE.as_value(), Some(VALUE));
assert_eq!(VALUE.to_source(), SOURCE)
} }

26
src/line/list/gemtext.rs Normal file
View file

@ -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)
}

View file

@ -1,3 +1,6 @@
pub mod gemtext;
pub use gemtext::Gemtext;
/// [Quote item](https://geminiprotocol.net/docs/gemtext-specification.gmi#quote-lines) tag /// [Quote item](https://geminiprotocol.net/docs/gemtext-specification.gmi#quote-lines) tag
pub const TAG: char = '>'; 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] #[test]
fn test() { fn test() {
const SOURCE: &str = "> Quote"; const SOURCE: &str = "> Quote";
const VALUE: &str = "Quote"; const VALUE: &str = "Quote";
// test `Quote`
let quote = Quote::parse(SOURCE).unwrap(); let quote = Quote::parse(SOURCE).unwrap();
assert_eq!(quote.value, VALUE); assert_eq!(quote.value, VALUE);
assert_eq!(quote.to_source(), SOURCE); assert_eq!(quote.to_source(), SOURCE);
// test `Gemtext`
assert_eq!(SOURCE.as_value(), Some(VALUE));
assert_eq!(VALUE.to_source(), SOURCE)
} }

26
src/line/quote/gemtext.rs Normal file
View file

@ -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)
}