3个版本 (破坏性更新)
0.3.0 | 2021年9月7日 |
---|---|
0.2.0 | 2021年9月1日 |
0.1.0 | 2021年8月26日 |
#1815 in 解析实现
78KB
2K SLoC
tdtxt
一个用于在todo.txt格式中反序列化/序列化文件和文本的Rust库。
状态
这个crate仍在开发中,API可能不稳定,未来可能会更改。
不应在生产代码中使用此crate。
用法
将其添加到你的Cargo.toml
的依赖项中
[dependencies]
tdtxt = "0.2"
然后使用它
use std::str::FromStr as _;
use tdtxt::{Task, Date, State, Priority, DateCompound};
let line = "x (A) 2016-05-20 2016-04-30 measure space for +chapelShelving @chapel due:2016-05-30";
let task = Task::from_str(line).unwrap();
assert_eq!(task.state(), &State::Done);
assert_eq!(task.priority(), Some(&Priority::A));
assert_eq!(task.date_compound(), Some(&DateCompound::Completed { created: Date::ymd(2016, 4, 30), completed: Date::ymd(2016, 5, 20) }));
assert_eq!(task.description().description(), "measure space for +chapelShelving @chapel due:2016-05-30");
assert_eq!(task.description().projects().collect::<Vec<_>>(), vec!["chapelShelving"]);
assert_eq!(task.description().contexts().collect::<Vec<_>>(), vec!["chapel"]);
assert_eq!(task.description().custom().collect::<Vec<_>>(), vec![("due", "2016-05-30")]);
use std::str::FromStr as _;
use tdtxt::{Task, Date, State, Priority, DateCompound};
let line = "x (A) 2016-05-20 2016-04-30 measure space for +chapelShelving @chapel due:2016-05-30";
let task = Task::build()
.state(State::Done)
.priority(Priority::A)
.date_compound(DateCompound::completed(Date::ymd(2016, 4, 30), Date::ymd(2016, 5, 20)))
.build("measure space for +chapelShelving @chapel due:2016-05-30");
assert_eq!(format!("{}", task), line);
assert_eq!(task.state(), &State::Done);
assert_eq!(task.priority(), Some(&Priority::A));
assert_eq!(task.date_compound(), Some(&DateCompound::Completed { created: Date::ymd(2016, 4, 30), completed: Date::ymd(2016, 5, 20) }));
assert_eq!(task.description().description(), "measure space for +chapelShelving @chapel due:2016-05-30");
assert_eq!(task.description().projects().collect::<Vec<_>>(), vec!["chapelShelving"]);
assert_eq!(task.description().contexts().collect::<Vec<_>>(), vec!["chapel"]);
assert_eq!(task.description().custom().collect::<Vec<_>>(), vec![("due", "2016-05-30")]);
示例
有关更详细的示例,请参阅examples/
目录。
运行一个
# Example `filter_open`
cargo run --example filter_open -- examples/todos.txt
功能
Serde (serde
)
使用serde序列化和反序列化Task结构体。
示例
use tdtxt::{Task, Date, State, Priority, DateCompound};
let task_should = Task::build()
.state(State::Done)
.priority(Priority::A)
.date_compound(DateCompound::completed(
Date::ymd(2016, 4, 30),
Date::ymd(2016, 5, 20),
))
.build("measure space for +chapelShelving @chapel due:2016-05-30");
let json = serde_json::to_string_pretty(&task_should).unwrap();
println!("{}", &json);
示例json输出
{
"state": "Done",
"priority": "A",
"created": "2016-04-30",
"completed": "2016-05-20",
"description": "measure space for +chapelShelving @chapel due:2016-05-30"
}
注意
created
和completed
出现的顺序很重要。
依赖项
~0–455KB