jrnl: proper parsing and improve rust-analyzer stuff working
This commit is contained in:
parent
3b9aaf5f59
commit
54b25b154a
6 changed files with 1118 additions and 2 deletions
|
@ -1,5 +1,15 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, path::PathBuf};
|
||||
use time::format_description::well_known::{iso8601, Iso8601};
|
||||
|
||||
use crate::md::Doc;
|
||||
|
||||
const DATETIME_CONFIG: iso8601::EncodedConfig = iso8601::Config::DEFAULT
|
||||
.set_time_precision(iso8601::TimePrecision::Minute {
|
||||
decimal_digits: None,
|
||||
})
|
||||
.encode();
|
||||
const DT_FORMAT: Iso8601<DATETIME_CONFIG> = Iso8601::<DATETIME_CONFIG>;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct Cli {
|
||||
|
@ -18,5 +28,71 @@ enum Command {
|
|||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
println!("Hello, world!");
|
||||
println!("cli: {cli:#?}")
|
||||
println!("cli: {cli:#?}");
|
||||
|
||||
// TODO: handle btter
|
||||
let file = fs::read_to_string(cli.s10e_jrnl_file_loc).unwrap();
|
||||
|
||||
let doc = Doc::new(&file);
|
||||
dbg!(doc);
|
||||
}
|
||||
|
||||
mod md {
|
||||
use markdown::{Block, Span};
|
||||
use time::PrimitiveDateTime;
|
||||
|
||||
use crate::DT_FORMAT;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Doc {
|
||||
title: Vec<Span>,
|
||||
entries: Vec<Entry>,
|
||||
}
|
||||
|
||||
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 = PrimitiveDateTime::parse(ts, &DT_FORMAT).unwrap();
|
||||
|
||||
current = Some(Entry {
|
||||
timestamp: ts,
|
||||
title: entry_title.to_owned(),
|
||||
content: Vec::new(),
|
||||
});
|
||||
}
|
||||
other => current.as_mut().unwrap().content.push(other),
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
title: doc_title,
|
||||
entries,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Entry {
|
||||
timestamp: PrimitiveDateTime,
|
||||
title: String,
|
||||
content: Vec<Block>,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue