update list State api

This commit is contained in:
yggverse 2026-03-10 18:31:12 +02:00
parent d40eab57ec
commit 0eebd1c85d

View file

@ -7,22 +7,20 @@ use regex::Regex;
const REGEX_LIST: &str = const REGEX_LIST: &str =
r"(?m)^(?P<level>[ \t]*)\*[ \t]+(?:(?P<state>\[[ xX]\])[ \t]+)?(?P<text>.*)"; r"(?m)^(?P<level>[ \t]*)\*[ \t]+(?:(?P<state>\[[ xX]\])[ \t]+)?(?P<text>.*)";
struct State { struct State(bool);
pub is_checked: bool,
//tag: TextTag,
}
impl State { impl State {
fn parse(value: Option<&str>) -> Option<Self> { fn parse(value: Option<&str>) -> Option<Self> {
if let Some(state) = value if let Some(state) = value
&& (state.starts_with("[ ]") || state.starts_with("[x]")) && (state.starts_with("[ ]") || state.starts_with("[x]"))
{ {
return Some(Self { return Some(Self(state.starts_with("[x]")));
is_checked: state.starts_with("[x]"),
});
} }
None None
} }
fn is_checked(&self) -> bool {
self.0
}
} }
struct Item { struct Item {
@ -79,7 +77,7 @@ pub fn render(buffer: &TextBuffer) {
if let Some(state) = item.state { if let Some(state) = item.state {
buffer.insert_with_tags( buffer.insert_with_tags(
&mut start_iter, &mut start_iter,
if state.is_checked { "[x] " } else { "[ ] " }, if state.is_checked() { "[x] " } else { "[ ] " },
&[&state_tag], &[&state_tag],
); );
} }
@ -134,13 +132,13 @@ fn test_regex() {
{ {
let item = item(&cap, 5); let item = item(&cap, 5);
assert_eq!(item.level, 2); assert_eq!(item.level, 2);
assert!(item.state.is_some_and(|this| this.is_checked)); assert!(item.state.is_some_and(|this| this.is_checked()));
assert_eq!(item.text, "list item 3.1"); assert_eq!(item.text, "list item 3.1");
} }
{ {
let item = item(&cap, 6); let item = item(&cap, 6);
assert_eq!(item.level, 2); assert_eq!(item.level, 2);
assert!(item.state.is_some_and(|this| !this.is_checked)); assert!(item.state.is_some_and(|this| !this.is_checked()));
assert_eq!(item.text, "list item 3.2"); assert_eq!(item.text, "list item 3.2");
} }
{ {