inital
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@@ -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"
|
||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "todo"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
144
src/main.rs
Normal file
144
src/main.rs
Normal file
@@ -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<Node>,
|
||||
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<usize>,
|
||||
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<Option<FakeNode>>,
|
||||
items: Vec<FakeNode>,
|
||||
value: String,
|
||||
}
|
||||
|
||||
impl FakeNode {
|
||||
fn new(value: String, parent: Option<FakeNode>) -> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user