Add data to LineType. Add parsing for filelist and defines

This commit is contained in:
Raamakrishnan 2020-01-22 22:46:56 +05:30
parent d5b0c72f03
commit 319a7108bf

View file

@ -1,16 +1,27 @@
use std::collections::HashMap;
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub enum LineType { enum LineType <'a> {
File, File(&'a str),
Define, IncDir(Vec<&'a str>),
Filelist, Define(HashMap<&'a str, &'a str>),
Filelist(&'a str),
Unknown, Unknown,
} }
pub fn parse_line(line: &str) -> LineType { fn parse_line<'a>(line: &'a str) -> LineType<'a> {
let line = line.trim();
if line.starts_with("-f ") { if line.starts_with("-f ") {
return LineType::Filelist; let filelist_name = line.trim_start_matches("-f ");
return LineType::Filelist(filelist_name);
} else if line.starts_with("+define+") { } else if line.starts_with("+define+") {
return LineType::Define; let defines = line.trim_start_matches("+define+");
let mut define_map = HashMap::new();
for define in defines.split("+") {
let split: Vec<&str> = define.splitn(2, "=").collect();
define_map.insert(split[0], split[1]);
}
return LineType::Define(define_map);
} else { } else {
return LineType::Unknown; return LineType::Unknown;
} }
@ -23,12 +34,16 @@ mod test {
#[test] #[test]
fn parse_line_filelist() { fn parse_line_filelist() {
let line = "-f sample/files.f"; let line = "-f sample/files.f";
assert_eq!(parse_line(line), LineType::Filelist); assert_eq!(parse_line(line), LineType::Filelist("sample/files.f"));
} }
#[test] #[test]
fn parse_line_define() { fn parse_line_define() {
let line = "+define+CONST1=const1+CONST2=const2"; let line = "+define+CONST1=const1+CONST2=const2+CONST3=const3=1";
assert_eq!(parse_line(line), LineType::Define); let mut define_map = HashMap::new();
define_map.insert("CONST1", "const1");
define_map.insert("CONST2", "const2");
define_map.insert("CONST3", "const3=1");
assert_eq!(parse_line(line), LineType::Define(define_map));
} }
} }