From 1f2f7e45ca7f068ad5ad5845f1d47661612449ba Mon Sep 17 00:00:00 2001 From: Vegard Matthey Date: Wed, 18 Sep 2024 17:42:37 +0200 Subject: [PATCH] inital --- .gitignore | 1 + Cargo.lock | 7 +++ Cargo.toml | 6 +++ src/main.rs | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ todo.txt | 5 ++ 5 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 todo.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f99de11 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "todo" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9f953e0 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "todo" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5a75031 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,144 @@ +fn main() { + let mut fake_node = FakeNode::new("todo".to_string(), None); + let file_data = std::fs::read_to_string("todo.txt").unwrap(); + let mut depth = 0; + for line in file_data.lines() { + string_to_node(&mut fake_node, &line, &mut depth); + } + let real = fake_node.to_real(); + println!("{:#?}", real); + // let mode = args(); + // let node = Some(Node::new(String::from("todo"))); + // if let Some(mut node) = node { + // modify( + // &mut node, + // &Modification { + // node: Vec::new(), + // operation: Operation::Add(String::from("other")), + // }, + // ) + // .unwrap(); + // modify( + // &mut node, + // &Modification { + // node: Vec::new(), + // operation: Operation::Add(String::from("other1")), + // }, + // ) + // .unwrap(); + // modify( + // &mut node, + // &Modification { + // node: vec![1], + // operation: Operation::Remove, + // }, + // ) + // .unwrap(); + // let mut s = String::new(); + // render(&node, 0, &mut s); + // print!("{s}"); + // std::fs::write("todo.txt", s).unwrap(); + // } +} + +#[derive(Clone, Debug)] +struct Node { + items: Vec, + value: String, +} + +impl Node { + fn new(v: String) -> Self { + Self { + items: Vec::new(), + value: v, + } + } +} + +// enum Mode { +// CLI, +// TUI, +// } + +// fn args() -> Mode { +// let mut args = std::env::args().skip(1); +// if args.next() == Some("-t".to_string()) { +// Mode::TUI +// } else { +// Mode::CLI +// } +// } + +fn render(node: &Node, depth: usize, mut s: &mut String) { + s.push_str(&format!("{}{}\n", "\t".repeat(depth), node.value)); + for node in &node.items { + render(&node, depth + 1, &mut s); + } +} + +struct Modification { + node: Vec, + operation: Operation, +} + +#[derive(PartialEq)] +enum Operation { + Remove, + Add(String), +} + +fn modify(mut node: &mut Node, change: &Modification) -> Option<()> { + for i in &change.node { + if Some(i) == change.node.last() && change.operation == Operation::Remove { + node.items.remove(*i); + return Some(()); + } + node = node.items.get_mut(*i)?; + } + if let Operation::Add(s) = &change.operation { + node.items.push(Node::new(s.to_string())); + } + Some(()) +} + +#[derive(Clone, Debug)] +struct FakeNode { + parent: Box>, + items: Vec, + value: String, +} + +impl FakeNode { + fn new(value: String, parent: Option) -> Self { + Self { + value, + parent: Box::new(parent), + items: Vec::new(), + } + } + fn to_real(&self) -> Node { + Node { + value: self.value.clone(), + items: self.items.iter().map(|m| m.to_real()).collect(), + } + } +} + +fn string_to_node(node: &mut FakeNode, line: &str, prev_depth: &mut usize) { + let mut depth = 0; + for c in line.chars() { + if c != '\t' { + break; + } + depth += 1; + } + node.items + .push(FakeNode::new(line[depth..].to_string(), Some(node.clone()))); + if depth > *prev_depth { + *node = node.items.last().unwrap().clone(); + } + if depth < *prev_depth { + *node = node.parent.clone().unwrap(); + } +} diff --git a/todo.txt b/todo.txt new file mode 100644 index 0000000..b421b5f --- /dev/null +++ b/todo.txt @@ -0,0 +1,5 @@ +todo + other + test + something + special