Skip to content

Commit

Permalink
make makefile#new does not return result but just makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Aug 7, 2023
1 parent 206b6f1 commit bd5f925
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/file/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn create_makefile() -> Result<makefile::Makefile, &'static str> {

Ok(parser::makefile::Makefile::new(
Path::new(&makefile_name).to_path_buf(),
)?)
))
}

fn specify_makefile_name(target_path: String) -> Option<String> {
Expand Down
28 changes: 6 additions & 22 deletions src/parser/makefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,19 @@ pub struct Makefile {

impl Makefile {
// TODO: add UT
pub fn new(path: PathBuf) -> Result<Makefile, &'static str> {
pub fn new(path: PathBuf) -> Makefile {
let file_content = Makefile::path_to_content(path.clone());
let including_file_paths = include::extract_including_file_paths(file_content.clone());
let include_files: Vec<Result<Makefile, &'static str>> = including_file_paths
let include_files: Vec<Makefile> = including_file_paths
.iter()
.map(|path| Makefile::new(Path::new(&path).to_path_buf()))
.collect();

// この辺のエラーの扱いどうしよう
let mut include_files_err: Vec<&'static str> = vec![];
let mut include_files_ok: Vec<Makefile> = vec![];
for i in include_files {
match i {
Ok(m) => include_files_ok.push(m),
Err(err) => include_files_err.push(err),
}
}

let targets = target::content_to_commands(file_content);
if let Err(e) = targets {
print!("failed to parse target\n");
return Err(e);
}

Ok(Makefile {
Makefile {
path,
include_files: include_files_ok,
targets: targets.unwrap(),
})
include_files,
targets: target::content_to_commands(file_content),
}
}

pub fn to_include_path_string(&self) -> Vec<String> {
Expand Down
31 changes: 13 additions & 18 deletions src/parser/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ use regex::Regex;

pub type Targets = Vec<String>;

pub fn content_to_commands(content: String) -> Result<Targets, &'static str> {
pub fn content_to_commands(content: String) -> Targets {
let mut result: Vec<String> = Vec::new();
for line in content.lines() {
if let Some(c) = line_to_command(line.to_string()) {
result.push(c);
}
}

if !result.is_empty() {
Ok(result)
} else {
Err("target not found")
}
result
}

fn line_to_command(line: String) -> Option<String> {
Expand All @@ -34,8 +30,6 @@ fn line_to_command(line: String) -> Option<String> {

#[cfg(test)]
mod test {
use std::str::FromStr;

use super::*;

#[test]
Expand Down Expand Up @@ -113,7 +107,7 @@ mod test {
struct Case {
title: &'static str,
contents: &'static str,
expect: Result<Vec<&'static str>, &'static str>, // NOTE: order of elements of `expect` order should be same as vec function returns
expect: Vec<String>, // NOTE: order of elements of `expect` order should be same as vec function returns
}
let cases = vec![
Case {
Expand All @@ -136,7 +130,13 @@ test: # run test
echo:
@echo good",
expect: Ok(vec!["run", "build", "check", "test", "echo"]),
expect: vec![
"run".to_string(),
"build".to_string(),
"check".to_string(),
"test".to_string(),
"echo".to_string(),
],
},
Case {
title: "comment line",
Expand All @@ -149,23 +149,18 @@ clone:
build:
@cargo build",
expect: Ok(vec!["clone", "build"]),
expect: vec!["clone".to_string(), "build".to_string()],
},
Case {
title: "invalid format",
contents: "echo hello",
expect: Err("target not found"),
expect: vec![],
},
];

for case in cases {
let expect = case.expect.map(|x| {
x.iter()
.map(|y| String::from_str(y).unwrap())
.collect::<Vec<String>>()
});
assert_eq!(
expect,
case.expect,
content_to_commands(case.contents.to_string()),
"\nFailed in the 🚨{:?}🚨",
case.title,
Expand Down

0 comments on commit bd5f925

Please sign in to comment.