cleanup and unwrap handleing
This commit is contained in:
parent
0dce3e8ab9
commit
6fccb02d3f
51
src/main.rs
51
src/main.rs
|
|
@ -10,7 +10,7 @@ use comrak::{
|
||||||
};
|
};
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::{copy, read, read_dir, File};
|
use std::fs::{read, read_dir, File};
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
@ -82,9 +82,17 @@ fn parse_todo_file<'a>(file: &TodoFile, arena: &'a Arena<AstNode<'a>>) -> &'a As
|
||||||
..ComrakOptions::default()
|
..ComrakOptions::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let contents = read(file.file.path()).unwrap();
|
let contents_utf8 = read(file.file.path())
|
||||||
|
.expect(format!("Could not read file {}", file.file.path().to_string_lossy()).as_str());
|
||||||
|
let contents = str::from_utf8(&contents_utf8).expect(
|
||||||
|
format!(
|
||||||
|
"failed to convert contents of file to string: {}",
|
||||||
|
file.file.path().to_string_lossy()
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
|
||||||
parse_document(arena, str::from_utf8(&contents).unwrap(), options)
|
parse_document(arena, contents, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cleanup_sections<'a>(
|
fn cleanup_sections<'a>(
|
||||||
|
|
@ -93,11 +101,26 @@ fn cleanup_sections<'a>(
|
||||||
target_level: u8,
|
target_level: u8,
|
||||||
) -> &'a AstNode<'a> {
|
) -> &'a AstNode<'a> {
|
||||||
for node in root.children() {
|
for node in root.children() {
|
||||||
match &node.data.borrow().value {
|
let node_ref = &node.data.borrow();
|
||||||
NodeValue::Heading(heading) if heading.level == target_level => {
|
if let NodeValue::Heading(heading) = node_ref.value {
|
||||||
if let NodeValue::Text(title) =
|
if heading.level != target_level {
|
||||||
&node.first_child().borrow().unwrap().data.borrow().value
|
continue;
|
||||||
{
|
}
|
||||||
|
|
||||||
|
let first_child_ref = &node.first_child();
|
||||||
|
let first_child = if let Some(child) = first_child_ref.borrow() {
|
||||||
|
child
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let data_ref = &first_child.data.borrow();
|
||||||
|
let title = if let NodeValue::Text(value) = &data_ref.value {
|
||||||
|
value
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
if !sections.contains(&title.as_str()) {
|
if !sections.contains(&title.as_str()) {
|
||||||
let level = heading.level;
|
let level = heading.level;
|
||||||
|
|
||||||
|
|
@ -111,11 +134,8 @@ fn cleanup_sections<'a>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
node.detach(); // remove heading as well
|
node.detach(); // remove heading as well
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
root
|
root
|
||||||
}
|
}
|
||||||
|
|
@ -128,12 +148,13 @@ fn get_editor(fallback: String) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_data_dir(dir_name: &str) -> PathBuf {
|
fn get_data_dir(dir_name: &str) -> PathBuf {
|
||||||
let mut dir = if let Ok(home) = env::var("HOME") {
|
let mut dir = match env::var("HOME") {
|
||||||
|
Ok(home) => {
|
||||||
let mut x = PathBuf::new();
|
let mut x = PathBuf::new();
|
||||||
x.push(home);
|
x.push(home);
|
||||||
x
|
x
|
||||||
} else {
|
}
|
||||||
env::current_dir().expect("PWD environment variable not set")
|
_ => env::current_dir().expect("PWD environment variable not set"),
|
||||||
};
|
};
|
||||||
dir = dir.join(dir_name);
|
dir = dir.join(dir_name);
|
||||||
dir
|
dir
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue