Skip to content

Commit c29f1ba

Browse files
author
yggverse
committed
separate traits
1 parent 5b751e3 commit c29f1ba

File tree

4 files changed

+59
-42
lines changed

4 files changed

+59
-42
lines changed

src/line/list.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
pub mod gemtext;
2+
pub use gemtext::Gemtext;
3+
14
/// [List item](https://geminiprotocol.net/docs/gemtext-specification.gmi#list-items) tag
25
pub const TAG: char = '*';
36

@@ -24,33 +27,12 @@ impl List {
2427
}
2528
}
2629

27-
pub trait Gemtext {
28-
/// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self`
29-
fn as_value(&self) -> Option<&str>;
30-
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
31-
fn to_source(&self) -> String;
32-
}
33-
34-
impl Gemtext for str {
35-
fn as_value(&self) -> Option<&str> {
36-
self.strip_prefix(TAG).map(|s| s.trim())
37-
}
38-
fn to_source(&self) -> String {
39-
format!("{TAG} {}", self.trim())
40-
}
41-
}
42-
4330
#[test]
4431
fn test() {
4532
const SOURCE: &str = "* Item";
4633
const VALUE: &str = "Item";
4734

48-
// test `List`
4935
let list = List::parse(SOURCE).unwrap();
5036
assert_eq!(list.value, VALUE);
5137
assert_eq!(list.to_source(), SOURCE);
52-
53-
// test `Gemtext`
54-
assert_eq!(SOURCE.as_value(), Some(VALUE));
55-
assert_eq!(VALUE.to_source(), SOURCE)
5638
}

src/line/list/gemtext.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use super::TAG;
2+
3+
pub trait Gemtext {
4+
/// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self`
5+
fn as_value(&self) -> Option<&str>;
6+
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
7+
fn to_source(&self) -> String;
8+
}
9+
10+
impl Gemtext for str {
11+
fn as_value(&self) -> Option<&str> {
12+
self.strip_prefix(TAG).map(|s| s.trim())
13+
}
14+
fn to_source(&self) -> String {
15+
format!("{TAG} {}", self.trim())
16+
}
17+
}
18+
19+
#[test]
20+
fn test() {
21+
const SOURCE: &str = "* Item";
22+
const VALUE: &str = "Item";
23+
24+
assert_eq!(SOURCE.as_value(), Some(VALUE));
25+
assert_eq!(VALUE.to_source(), SOURCE)
26+
}

src/line/quote.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
pub mod gemtext;
2+
pub use gemtext::Gemtext;
3+
14
/// [Quote item](https://geminiprotocol.net/docs/gemtext-specification.gmi#quote-lines) tag
25
pub const TAG: char = '>';
36

@@ -24,33 +27,13 @@ impl Quote {
2427
}
2528
}
2629

27-
pub trait Gemtext {
28-
/// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self`
29-
fn as_value(&self) -> Option<&str>;
30-
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
31-
fn to_source(&self) -> String;
32-
}
33-
34-
impl Gemtext for str {
35-
fn as_value(&self) -> Option<&str> {
36-
self.strip_prefix(TAG).map(|s| s.trim())
37-
}
38-
fn to_source(&self) -> String {
39-
format!("{TAG} {}", self.trim())
40-
}
41-
}
42-
4330
#[test]
4431
fn test() {
4532
const SOURCE: &str = "> Quote";
4633
const VALUE: &str = "Quote";
4734

48-
// test `Quote`
4935
let quote = Quote::parse(SOURCE).unwrap();
36+
5037
assert_eq!(quote.value, VALUE);
5138
assert_eq!(quote.to_source(), SOURCE);
52-
53-
// test `Gemtext`
54-
assert_eq!(SOURCE.as_value(), Some(VALUE));
55-
assert_eq!(VALUE.to_source(), SOURCE)
5639
}

src/line/quote/gemtext.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use super::TAG;
2+
3+
pub trait Gemtext {
4+
/// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self`
5+
fn as_value(&self) -> Option<&str>;
6+
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
7+
fn to_source(&self) -> String;
8+
}
9+
10+
impl Gemtext for str {
11+
fn as_value(&self) -> Option<&str> {
12+
self.strip_prefix(TAG).map(|s| s.trim())
13+
}
14+
fn to_source(&self) -> String {
15+
format!("{TAG} {}", self.trim())
16+
}
17+
}
18+
19+
#[test]
20+
fn test() {
21+
const SOURCE: &str = "> Quote";
22+
const VALUE: &str = "Quote";
23+
24+
assert_eq!(SOURCE.as_value(), Some(VALUE));
25+
assert_eq!(VALUE.to_source(), SOURCE)
26+
}

0 commit comments

Comments
 (0)