jrnl: simplify (or complexify, if you dont like iterators and zero-copy) the parsing

This commit is contained in:
Schrottkatze 2024-04-22 11:15:45 +02:00
parent 28bebd5aaa
commit 0bf5ed0c76
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
5 changed files with 52 additions and 56 deletions

View file

@ -1,59 +1,43 @@
use chrono::{DateTime, FixedOffset};
use markdown::{Block, Span};
use std::convert::identity;
#[derive(Debug)]
pub struct Doc {
pub title: Vec<Span>,
pub entries: Vec<Entry>,
pub struct Doc<'src> {
pub entries: Vec<Entry<'src>>,
}
impl Doc {
pub fn new(f: &str) -> Self {
let mut entries = Vec::new();
let mut doc_title = vec![Span::Text("Journal".to_owned())];
let toks = markdown::tokenize(f);
let mut current = None;
for tok in toks {
match tok {
Block::Header(title, 1) => doc_title = title,
Block::Header(entry_title, 2) => {
if let Some(cur) = current.take() {
entries.push(cur);
}
let Some(Span::Text(title)) = entry_title.first() else {
eprintln!("Error: Titles should be text.");
std::process::exit(1);
};
let (ts, entry_title) = title.split_once(": ").unwrap();
let ts = DateTime::parse_from_rfc3339(ts).unwrap();
// let ts = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap();
current = Some(Entry {
timestamp: ts,
title: entry_title.to_owned(),
content: Vec::new(),
});
impl<'src> Doc<'src> {
// TODO: better parsing errors?
pub fn new(f: &'src str) -> Option<Self> {
let entries = f
.split("\n## ")
.map(|s| s.split_once("\n"))
.skip(1)
.filter_map(identity)
.map(|(title, content)| (title.split_once(": "), content))
.map(|(title, content)| {
if let Some((ts, title)) = title {
Some(Entry {
timestamp: DateTime::parse_from_rfc3339(ts).unwrap(),
title,
content: content.trim_matches('\n'),
})
} else {
None
}
other => current.as_mut().unwrap().content.push(other),
}
}
if let Some(cur) = current {
entries.push(cur);
}
})
.collect::<Vec<_>>();
Self {
title: doc_title,
entries,
}
entries.iter().all(|it| it.is_some()).then_some(Self {
entries: entries.into_iter().filter_map(identity).collect(),
})
}
}
#[derive(Debug)]
pub struct Entry {
#[derive(Debug, Clone)]
pub struct Entry<'src> {
pub timestamp: DateTime<FixedOffset>,
pub title: String,
pub content: Vec<Block>,
pub title: &'src str,
pub content: &'src str,
}