mirror of
https://github.com/YGGverse/ggemtext.git
synced 2026-03-31 09:05:32 +00:00
rename constructor, implement zero-copy trait, remove extra regex parser
This commit is contained in:
parent
1b43f6aeaf
commit
bf4ac4bd27
2 changed files with 44 additions and 14 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
use glib::{Regex, RegexCompileFlags, RegexMatchFlags};
|
/// [Quote item](https://geminiprotocol.net/docs/gemtext-specification.gmi#quote-lines) tag
|
||||||
|
pub const TAG: char = '>';
|
||||||
|
|
||||||
/// [Quote](https://geminiprotocol.net/docs/gemtext-specification.gmi#quote-lines) entity holder
|
/// [Quote](https://geminiprotocol.net/docs/gemtext-specification.gmi#quote-lines) entity holder
|
||||||
pub struct Quote {
|
pub struct Quote {
|
||||||
|
|
@ -8,19 +9,48 @@ pub struct Quote {
|
||||||
impl Quote {
|
impl Quote {
|
||||||
// Constructors
|
// Constructors
|
||||||
|
|
||||||
/// Parse `Self` from line string
|
/// Parse `Self` from [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
|
||||||
pub fn from(line: &str) -> Option<Self> {
|
pub fn parse(line: &str) -> Option<Self> {
|
||||||
// Parse line
|
|
||||||
let regex = Regex::split_simple(
|
|
||||||
r"^>\s*(.*)$",
|
|
||||||
line,
|
|
||||||
RegexCompileFlags::DEFAULT,
|
|
||||||
RegexMatchFlags::DEFAULT,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Extract formatted value
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
value: regex.get(1)?.trim().to_string(),
|
value: line.as_value()?.to_string(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Converters
|
||||||
|
|
||||||
|
/// Convert `Self` to [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) line
|
||||||
|
pub fn to_source(&self) -> String {
|
||||||
|
self.value.to_source()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Gemtext {
|
||||||
|
/// Get [Gemtext](https://geminiprotocol.net/docs/gemtext-specification.gmi) value from `Self`
|
||||||
|
fn as_value(&self) -> Option<&Self>;
|
||||||
|
/// 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<&Self> {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ fn gemtext() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quote
|
// Quote
|
||||||
if let Some(result) = Quote::from(line) {
|
if let Some(result) = Quote::parse(line) {
|
||||||
quote.push(result);
|
quote.push(result);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue