1
- /// [Header](https://geminiprotocol.net/docs/gemtext-specification.gmi#heading-lines) tag
2
- /// * store as entire static chars array
3
- pub const TAG_H1 : & str = "#" ;
4
- pub const TAG_H2 : & str = "##" ;
5
- pub const TAG_H3 : & str = "###" ;
1
+ pub mod gemtext;
2
+ pub mod level;
6
3
7
- /// [Header](https://geminiprotocol.net/docs/gemtext-specification.gmi#heading-lines) type holder
8
- pub enum Level {
9
- H1 ,
10
- H2 ,
11
- H3 ,
12
- }
4
+ pub use gemtext:: Gemtext ;
5
+ pub use level:: Level ;
13
6
14
7
/// [Header](https://geminiprotocol.net/docs/gemtext-specification.gmi#heading-lines) entity holder
15
8
pub struct Header {
@@ -22,10 +15,25 @@ impl Header {
22
15
23
16
/// Parse `Self` from line string
24
17
pub fn parse ( line : & str ) -> Option < Self > {
25
- Some ( Self {
26
- level : line. to_level ( ) ?,
27
- value : line. as_value ( ) ?. to_string ( ) ,
28
- } )
18
+ if let Some ( value) = line. as_h1_value ( ) {
19
+ return Some ( Self {
20
+ level : Level :: H1 ,
21
+ value : value. to_string ( ) ,
22
+ } ) ;
23
+ }
24
+ if let Some ( value) = line. as_h2_value ( ) {
25
+ return Some ( Self {
26
+ level : Level :: H2 ,
27
+ value : value. to_string ( ) ,
28
+ } ) ;
29
+ }
30
+ if let Some ( value) = line. as_h3_value ( ) {
31
+ return Some ( Self {
32
+ level : Level :: H3 ,
33
+ value : value. to_string ( ) ,
34
+ } ) ;
35
+ }
36
+ None
29
37
}
30
38
31
39
// Converters
@@ -35,56 +43,3 @@ impl Header {
35
43
self . value . to_source ( & self . level )
36
44
}
37
45
}
38
-
39
- pub trait Gemtext {
40
- /// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value for `Self`
41
- fn as_value ( & self ) -> Option < & Self > ;
42
- /// Convert `Self` to `Level`
43
- fn to_level ( & self ) -> Option < Level > ;
44
- /// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
45
- fn to_source ( & self , level : & Level ) -> String ;
46
- }
47
-
48
- impl Gemtext for str {
49
- fn as_value ( & self ) -> Option < & str > {
50
- if let Some ( h3) = self . strip_prefix ( TAG_H3 ) {
51
- if h3. trim_start ( ) . starts_with ( TAG_H1 ) {
52
- return None ; // H4+
53
- }
54
- return Some ( h3. trim ( ) ) ;
55
- }
56
- if let Some ( h2) = self . strip_prefix ( TAG_H2 ) {
57
- return Some ( h2. trim ( ) ) ;
58
- }
59
- if let Some ( h1) = self . strip_prefix ( TAG_H1 ) {
60
- return Some ( h1. trim ( ) ) ;
61
- }
62
- None
63
- }
64
- fn to_level ( & self ) -> Option < Level > {
65
- if let Some ( h3) = self . strip_prefix ( TAG_H3 ) {
66
- if h3. trim_start ( ) . starts_with ( TAG_H1 ) {
67
- return None ; // H4+
68
- }
69
- return Some ( Level :: H3 ) ;
70
- }
71
- if self . starts_with ( TAG_H2 ) {
72
- return Some ( Level :: H2 ) ;
73
- }
74
- if self . starts_with ( TAG_H1 ) {
75
- return Some ( Level :: H1 ) ;
76
- }
77
- None
78
- }
79
- fn to_source ( & self , level : & Level ) -> String {
80
- format ! (
81
- "{} {}" ,
82
- match level {
83
- Level :: H1 => TAG_H1 ,
84
- Level :: H2 => TAG_H2 ,
85
- Level :: H3 => TAG_H3 ,
86
- } ,
87
- self . trim( )
88
- )
89
- }
90
- }
0 commit comments