okcool
parent
039c4dad04
commit
c5ed06b76c
|
|
@ -17,6 +17,13 @@ impl Task {
|
|||
Task(serde_yaml::Mapping::new())
|
||||
}
|
||||
|
||||
pub fn from_reader_value(r: impl std::io::Read) -> Result<Task, String> {
|
||||
match serde_yaml::from_reader::<_, serde_yaml::Value>(r) {
|
||||
Ok(v) => Ok(Task::from_value(v)),
|
||||
Err(msg) => Err(format!("failed to read value: {}", msg)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_value(v: serde_yaml::Value) -> Task {
|
||||
let mut result = Task::new();
|
||||
match v.as_mapping() {
|
||||
|
|
@ -114,13 +121,29 @@ mod test_task {
|
|||
let tasks = Task::from_reader(
|
||||
std::fs::File::open("./src/testdata/mvp.yaml").expect("failed to open file")
|
||||
).expect("failed to read file");
|
||||
eprintln!("tasks from_reader ./src/testdata/mvp.yaml: {:?}", tasks);
|
||||
assert_eq!(2, tasks.len());
|
||||
assert_eq!(1, tasks[0].0.len());
|
||||
assert!(tasks[0].get("is".to_string()).is_some());
|
||||
assert_eq!("x".to_string(), tasks[0].get("is".to_string()).unwrap());
|
||||
assert_eq!(1, tasks[1].0.len());
|
||||
assert_eq!("y and z".to_string(), tasks[1].get("is".to_string()).unwrap());
|
||||
|
||||
let task = Task::from_reader_value(
|
||||
std::fs::File::open("./src/testdata/mvp.uuid-123-456-xyz.yaml").expect("failed to open file")
|
||||
).expect("failed to read 123...");
|
||||
assert_eq!(1, task.0.len());
|
||||
assert!(task.get("is".to_string()).is_some());
|
||||
assert_eq!("plaintext".to_string(), task.get("is".to_string()).unwrap());
|
||||
|
||||
let task = Task::from_reader_value(
|
||||
std::fs::File::open("./src/testdata/mvp.uuid-789-012-abc.yaml").expect("failed to open file")
|
||||
).expect("failed to read 789...");
|
||||
assert_eq!(3, task.0.len());
|
||||
assert!(task.get("is".to_string()).is_none());
|
||||
assert_eq!("todo here".to_string(), task.get("todo".to_string()).unwrap());
|
||||
assert_eq!("* * * * *".to_string(), task.get("schedule".to_string()).unwrap());
|
||||
assert_eq!("hello world\n".to_string(), task.get("details".to_string()).unwrap());
|
||||
assert!(task.is_due());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
todo: todo here
|
||||
schedule: * * * * *
|
||||
schedule: '* * * * *'
|
||||
details: |
|
||||
hello world
|
||||
|
|
|
|||
Loading…
Reference in New Issue