mirror of
https://github.com/YGGverse/ggemtext.git
synced 2026-04-01 17:45:36 +00:00
separate traits
This commit is contained in:
parent
5b751e3c7a
commit
c29f1ba529
4 changed files with 59 additions and 42 deletions
26
src/line/list/gemtext.rs
Normal file
26
src/line/list/gemtext.rs
Normal 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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue