1
0
Fork 0
mirror of https://github.com/archtechx/todo-system.git synced 2025-12-12 17:14:03 +00:00

struct refactor

This commit is contained in:
Samuel Štancl 2023-11-22 00:19:13 +01:00
parent 345c07c7f8
commit 80e074f60b

View file

@ -9,30 +9,27 @@ struct Location {
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
struct PriorityEntry { struct Entry {
text: String, text: String,
priority: isize,
location: Location, location: Location,
data: EntryData,
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
struct CategoryEntry { enum EntryData {
text: String, Priority(isize),
category: String, Category(String),
location: Location, Generic,
} }
#[derive(Debug, PartialEq, Clone)] impl Entry {
struct GenericEntry { fn render(&self) {
text: String, if self.text.len() > 0 {
location: Location, println!("- [ ] {} ({}:{})", self.text, self.location.file.to_string_lossy(), self.location.line);
} } else {
println!("- [ ] {}:{}", self.location.file.to_string_lossy(), self.location.line);
#[derive(Debug, PartialEq, Clone)] }
enum Entry { }
Priority(PriorityEntry),
Category(CategoryEntry),
Generic(GenericEntry),
} }
fn scan_string(str: String, filename: PathBuf, entries: &mut Vec<Entry>) { fn scan_string(str: String, filename: PathBuf, entries: &mut Vec<Entry>) {
@ -52,13 +49,14 @@ fn scan_string(str: String, filename: PathBuf, entries: &mut Vec<Entry>) {
let text_dirty = line.split_once(word).unwrap().1.replace("*/", ""); let text_dirty = line.split_once(word).unwrap().1.replace("*/", "");
let text = text_dirty.trim(); let text = text_dirty.trim();
entries.push(Entry::Generic(GenericEntry { entries.push(Entry {
text: text.to_string(), text: text.to_string(),
location: Location { location: Location {
file: filename.clone(), file: filename.clone(),
line: line_num + 1, line: line_num + 1,
} },
})); data: EntryData::Generic,
});
break; break;
} }
@ -68,14 +66,14 @@ fn scan_string(str: String, filename: PathBuf, entries: &mut Vec<Entry>) {
let text_dirty = line.split_once(word).unwrap().1.replace("*/", ""); let text_dirty = line.split_once(word).unwrap().1.replace("*/", "");
let text = text_dirty.trim(); let text = text_dirty.trim();
entries.push(Entry::Category(CategoryEntry { entries.push(Entry {
text: text.to_string(), text: text.to_string(),
category: category.to_string(),
location: Location { location: Location {
file: filename.clone(), file: filename.clone(),
line: line_num + 1, line: line_num + 1,
}, },
})); data: EntryData::Category(category.to_string()),
});
break; break;
} }
@ -102,14 +100,14 @@ fn scan_string(str: String, filename: PathBuf, entries: &mut Vec<Entry>) {
let text_dirty = line.split_once(word).unwrap().1.replace("*/", ""); let text_dirty = line.split_once(word).unwrap().1.replace("*/", "");
let text = text_dirty.trim(); let text = text_dirty.trim();
entries.push(Entry::Priority(PriorityEntry { entries.push(Entry {
text: text.to_string(), text: text.to_string(),
priority: priority,
location: Location { location: Location {
file: filename.clone(), file: filename.clone(),
line: line_num + 1, line: line_num + 1,
} },
})); data: EntryData::Priority(priority),
});
} }
} }
} }
@ -141,29 +139,29 @@ fn scan_dir(path: &Path, entries: &mut Vec<Entry>) -> io::Result<()> {
} }
fn render(entries: Vec<Entry>) { fn render(entries: Vec<Entry>) {
let mut priority_entries: HashMap<isize, Vec<PriorityEntry>> = HashMap::new(); let mut priority_entries: HashMap<isize, Vec<Entry>> = HashMap::new();
let mut category_entries: HashMap<String, Vec<CategoryEntry>> = HashMap::new(); let mut category_entries: HashMap<String, Vec<Entry>> = HashMap::new();
let mut generic_entries: Vec<GenericEntry> = Vec::new(); let mut generic_entries: Vec<Entry> = Vec::new();
for entry in entries { for entry in entries {
match entry { match entry.data {
Entry::Priority(entry) => { EntryData::Priority(priority) => {
if ! priority_entries.contains_key(&entry.priority) { if ! priority_entries.contains_key(&priority) {
priority_entries.insert(entry.priority, vec![]); priority_entries.insert(priority, vec![]);
} }
let vec = priority_entries.get_mut(&entry.priority).unwrap(); let vec = priority_entries.get_mut(&priority).unwrap();
vec.push(entry); vec.push(entry);
}, },
Entry::Category(entry) => { EntryData::Category(ref category) => {
if ! category_entries.contains_key(&entry.category) { if ! category_entries.contains_key(category) {
category_entries.insert(entry.category.clone(), vec![]); category_entries.insert(category.clone(), vec![]);
} }
let vec = category_entries.get_mut(&entry.category).unwrap(); let vec = category_entries.get_mut(category).unwrap();
vec.push(entry); vec.push(entry);
}, },
Entry::Generic(entry) => { EntryData::Generic => {
generic_entries.push(entry); generic_entries.push(entry);
} }
} }
@ -171,21 +169,6 @@ fn render(entries: Vec<Entry>) {
print!("# TODOs\n\n"); print!("# TODOs\n\n");
let render_entry = |entry: Entry| {
// todo refactor structs such that Entry is a struct which contains an enum
let (text, location) = match entry {
Entry::Priority(entry) => (entry.text, entry.location),
Entry::Category(entry) => (entry.text, entry.location),
Entry::Generic(entry) => (entry.text, entry.location),
};
if text.len() > 0 {
println!("- [ ] {} ({}:{})", text, location.file.to_string_lossy(), location.line);
} else {
println!("- [ ] {}:{}", location.file.to_string_lossy(), location.line);
}
};
let mut priority_keys = priority_entries.keys().collect::<Vec<&isize>>(); let mut priority_keys = priority_entries.keys().collect::<Vec<&isize>>();
priority_keys.sort_by(|a, b| a.partial_cmp(b).unwrap()); priority_keys.sort_by(|a, b| a.partial_cmp(b).unwrap());
@ -208,7 +191,7 @@ fn render(entries: Vec<Entry>) {
println!("## {}", priority_notation); println!("## {}", priority_notation);
for item in priority_entries.get(priority).unwrap() { for item in priority_entries.get(priority).unwrap() {
render_entry(Entry::Priority(item.clone())); item.render();
} }
println!(""); println!("");
@ -221,7 +204,7 @@ fn render(entries: Vec<Entry>) {
println!("## {}", category); println!("## {}", category);
for item in category_entries.get(category).unwrap() { for item in category_entries.get(category).unwrap() {
render_entry(Entry::Category(item.clone())); item.render();
} }
println!(""); println!("");
@ -232,7 +215,7 @@ fn render(entries: Vec<Entry>) {
generic_entries.sort_by(|a, b| a.text.partial_cmp(&b.text).unwrap()); generic_entries.sort_by(|a, b| a.text.partial_cmp(&b.text).unwrap());
for item in generic_entries { for item in generic_entries {
render_entry(Entry::Generic(item.clone())); item.render();
} }
} }
@ -277,45 +260,50 @@ fn generic_test() {
assert_eq!(5, entries.len()); assert_eq!(5, entries.len());
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("foo"), text: String::from("foo"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 4, line: 4,
} }
}), entries[0]); }, entries[0]);
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("foo bar"), text: String::from("foo bar"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 5, line: 5,
} }
}), entries[1]); }, entries[1]);
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("baz"), text: String::from("baz"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 8, line: 8,
} }
}), entries[2]); }, entries[2]);
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("baz2"), text: String::from("baz2"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 9, line: 9,
} }
}), entries[3]); }, entries[3]);
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("baz2 todo"), text: String::from("baz2 todo"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 10, line: 10,
} }
}), entries[4]); }, entries[4]);
} }
#[test] #[test]
@ -341,59 +329,59 @@ fn category_test() {
assert_eq!(6, entries.len()); assert_eq!(6, entries.len());
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("foo"), data: EntryData::Category(String::from("foo")),
text: String::from(""), text: String::from(""),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 4, line: 4,
} }
}), entries[0]); }, entries[0]);
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("bar"), data: EntryData::Category(String::from("bar")),
text: String::from("abc def"), text: String::from("abc def"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 5, line: 5,
} }
}), entries[1]); }, entries[1]);
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("baz"), data: EntryData::Category(String::from("baz")),
text: String::from("x y"), text: String::from("x y"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 7, line: 7,
} }
}), entries[2]); }, entries[2]);
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("baz2"), data: EntryData::Category(String::from("baz2")),
text: String::from("a"), text: String::from("a"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 9, line: 9,
} }
}), entries[3]); }, entries[3]);
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("baz3"), data: EntryData::Category(String::from("baz3")),
text: String::from(""), text: String::from(""),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 10, line: 10,
} }
}), entries[4]); }, entries[4]);
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("baz3"), data: EntryData::Category(String::from("baz3")),
text: String::from("b"), text: String::from("b"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 11, line: 11,
} }
}), entries[5]); }, entries[5]);
} }
#[test] #[test]
@ -422,86 +410,86 @@ fn priority_test() {
assert_eq!(9, entries.len()); assert_eq!(9, entries.len());
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: -1, data: EntryData::Priority(-1),
text: String::from(""), text: String::from(""),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 4, line: 4,
} }
}), entries[0]); }, entries[0]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: -2, data: EntryData::Priority(-2),
text: String::from("abc"), text: String::from("abc"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 5, line: 5,
} }
}), entries[1]); }, entries[1]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 0, data: EntryData::Priority(0),
text: String::from("abc def"), text: String::from("abc def"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 6, line: 6,
} }
}), entries[2]); }, entries[2]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 1, data: EntryData::Priority(1),
text: String::from("foo"), text: String::from("foo"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 7, line: 7,
} }
}), entries[3]); }, entries[3]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 1, data: EntryData::Priority(1),
text: String::from("x y"), text: String::from("x y"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 9, line: 9,
} }
}), entries[4]); }, entries[4]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 0, data: EntryData::Priority(0),
text: String::from("bar"), text: String::from("bar"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 11, line: 11,
} }
}), entries[5]); }, entries[5]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 1, data: EntryData::Priority(1),
text: String::from("a"), text: String::from("a"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 12, line: 12,
} }
}), entries[6]); }, entries[6]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 2, data: EntryData::Priority(2),
text: String::from(""), text: String::from(""),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 13, line: 13,
} }
}), entries[7]); }, entries[7]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 3, data: EntryData::Priority(3),
text: String::from("b"), text: String::from("b"),
location: Location { location: Location {
file: path.clone(), file: path.clone(),
line: 14, line: 14,
} }
}), entries[8]); }, entries[8]);
} }
#[test] #[test]
@ -518,90 +506,93 @@ fn sample_test_ts() {
assert_eq!(10, entries.len()); assert_eq!(10, entries.len());
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("types"), data: EntryData::Category(String::from("types")),
text: String::from(""), text: String::from(""),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 1, line: 1,
} }
}), entries[0]); }, entries[0]);
assert_eq!(Entry::Category(CategoryEntry { assert_eq!(Entry {
category: String::from("types"), data: EntryData::Category(String::from("types")),
text: String::from("add types"), text: String::from("add types"),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 5, line: 5,
} }
}), entries[1]); }, entries[1]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: -2, data: EntryData::Priority(-2),
text: String::from(""), text: String::from(""),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 10, line: 10,
} }
}), entries[2]); }, entries[2]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: -1, data: EntryData::Priority(-1),
text: String::from("add return typehint"), text: String::from("add return typehint"),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 14, line: 14,
} }
}), entries[3]); }, entries[3]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 0, data: EntryData::Priority(0),
text: String::from("add name typehint"), text: String::from("add name typehint"),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 19, line: 19,
} }
}), entries[4]); }, entries[4]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 1, data: EntryData::Priority(1),
text: String::from("add return typehint"), text: String::from("add return typehint"),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 23, line: 23,
} }
}), entries[5]); }, entries[5]);
assert_eq!(Entry::Priority(PriorityEntry { assert_eq!(Entry {
priority: 2, data: EntryData::Priority(2),
text: String::from("add return typehint"), text: String::from("add return typehint"),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 27, line: 27,
} }
}), entries[6]); }, entries[6]);
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from(""), text: String::from(""),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 31, line: 31,
} }
}), entries[7]); }, entries[7]);
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("generic todo 2"), text: String::from("generic todo 2"),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 33, line: 33,
} }
}), entries[8]); }, entries[8]);
assert_eq!(Entry::Generic(GenericEntry { assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("generic todo 3"), text: String::from("generic todo 3"),
location: Location { location: Location {
file: filepath.clone(), file: filepath.clone(),
line: 34, line: 34,
} }
}), entries[9]); }, entries[9]);
} }