Just doodling around with basic main and lib file

This commit is contained in:
Raamakrishnan 2020-01-20 22:15:06 +05:30
parent f50981370f
commit 8d36a3a46d
2 changed files with 67 additions and 0 deletions

34
src/lib.rs Normal file
View file

@ -0,0 +1,34 @@
#[derive(PartialEq, Debug)]
pub enum LineType {
File,
Define,
Filelist,
Unknown,
}
pub fn parse_line(line: &str) -> LineType {
if line.starts_with("-f ") {
return LineType::Filelist;
} else if line.starts_with("+define+") {
return LineType::Define;
} else {
return LineType::Unknown;
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_line_filelist() {
let line = "-f sample/files.f";
assert_eq!(parse_line(line), LineType::Filelist);
}
#[test]
fn parse_line_define() {
let line = "+define+CONST1=const1+CONST2=const2";
assert_eq!(parse_line(line), LineType::Define);
}
}

33
src/main.rs Normal file
View file

@ -0,0 +1,33 @@
use std::env;
use std::path::PathBuf;
use std::fs;
fn main() {
let args: Vec<String> = env::args().collect();
// get .f file
let dot_f_file = PathBuf::from(&args[1]);
println!("pathbuf {:?}", dot_f_file);
// check if .f file exists
if dot_f_file.is_file() {
println!("File exists", )
} else {
println!("File does not exist", )
}
// read .f file
let contents = fs::read_to_string(&dot_f_file)
.expect("Error while reading file");
println!("contents:\n{}", contents);
// check if all the files exist
for line in contents.lines() {
if PathBuf::from(line).is_file() {
println!("{} exists", line)
}
else {
println!("{} does not exist", line)
}
}
}