jrnl: proper parsing and improve rust-analyzer stuff working

This commit is contained in:
Schrottkatze 2024-04-18 18:47:06 +02:00
parent 3b9aaf5f59
commit 54b25b154a
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
6 changed files with 1118 additions and 2 deletions

View file

@ -200,6 +200,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "deranged"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
dependencies = [
"powerfmt",
]
[[package]]
name = "dirs"
version = "5.0.1"
@ -221,6 +230,12 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "ego-tree"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591"
[[package]]
name = "either"
version = "1.11.0"
@ -309,9 +324,12 @@ version = "0.1.0"
dependencies = [
"clap",
"dirs",
"ego-tree",
"indexmap",
"markdown",
"petgraph",
"ratatui",
"time",
]
[[package]]
@ -390,6 +408,12 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "once_cell"
version = "1.19.0"
@ -447,6 +471,12 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d15b6607fa632996eb8a17c9041cb6071cb75ac057abd45dece578723ea8c7c0"
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "proc-macro2"
version = "1.0.80"
@ -552,6 +582,26 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.198"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "signal-hook"
version = "0.3.17"
@ -663,6 +713,25 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.3.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885"
dependencies = [
"deranged",
"num-conv",
"powerfmt",
"serde",
"time-core",
]
[[package]]
name = "time-core"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "unicode-ident"
version = "1.0.12"

View file

@ -6,6 +6,10 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.4", features = ["derive", "env"] }
dirs = "5.0.1"
ego-tree = "0.6.2"
indexmap = "2.2.6"
markdown = "0.3.0"
petgraph = "0.6.4"
ratatui = "0.26.2"
time = { version = "0.3.36", features = ["parsing"]}
time-macros = { version = "0.2.0-alpha.1" }

View file

@ -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>,
}
}