Implement bold token

This commit is contained in:
Juno Takano 2026-01-06 18:16:55 -03:00
commit 1faa0d5c3b
6 changed files with 84 additions and 11 deletions

View file

@ -0,0 +1,61 @@
use crate::{
syntax::content::{Parseable, Lexeme},
};
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Bold {
open: bool,
}
impl Bold {
pub fn new(open: bool) -> Bold {
Bold { open }
}
}
impl Parseable for Bold {
fn probe(lexeme: &Lexeme) -> bool {
lexeme.text() == "*"
}
fn lex(_lexeme: &Lexeme) -> Bold {
panic!("Attempt to lex a bold tag directly from a lexeme")
}
fn render(&self) -> String {
if self.open {
String::from("<strong>")
} else {
String::from("</strong>")
}
}
}
impl std::fmt::Display for Bold {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let display_open_state = if self.open { "open" } else { "closed" };
write!(f, "Bold [{display_open_state}]")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn render() {
let code_open = Bold::new(true);
assert_eq!(code_open.render(), "<strong>");
let code_closed = Bold::new(false);
assert_eq!(code_closed.render(), "</strong>");
}
#[test]
#[should_panic(
expected = "Attempt to lex a bold tag directly from a lexeme"
)]
fn lex() {
Bold::lex(&Lexeme::new("", ""));
}
}

View file

@ -8,7 +8,7 @@ pub struct Item {
impl Parseable for Item {
fn probe(lexeme: &Lexeme) -> bool {
(lexeme.match_as_char('-') || lexeme.match_as_char('+'))
&& lexeme.match_next_as_char(' ')
&& lexeme.match_next_as_char(' ')
}
fn lex(_lexeme: &Lexeme) -> Item {

View file

@ -17,7 +17,6 @@ impl Parseable for List {
}
fn render(&self) -> String {
let bar = if self.open { "" } else { "/" };
let tag = if self.ordered { "ol" } else { "ul" };