测试用例
./example/basic.lore
element
key = value
+ category 1
+ category 2
sub element
+ category 3
sub key = value
+ category 4
+ sub category
some element
some key = value
util
根据 file_path: &str 读取文件,然后按行切割为 Vec<String>。
./util/file.rs
pub fn read_file(file_path: &str) -> Vec<String> {
std::fs::read_to_string(std::path::PathBuf::from(file_path))
.unwrap()
.lines() // &str
.map(|s| s.to_string())
.collect()
}
识别 indent_line: &str 前第一个非空格字符前的空格数,据此计算缩进。
./util/indent.rs
use std::ops::Div;
pub fn indent_line(line: &str) -> usize {
line
.chars()
.take_while(|c| c.is_whitespace())
.count() // usize
.div(2)
}