Skip to content

Commit

Permalink
Add error handling when the path passed to include directive could no…
Browse files Browse the repository at this point in the history
…t be found #150 (#154)
  • Loading branch information
kyu08 authored Dec 15, 2023
1 parent 297b534 commit afae0b4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/models/makefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Makefile {
impl Makefile {
pub fn create_makefile() -> Result<Makefile> {
let Some(makefile_name) = Makefile::specify_makefile_name(".".to_string()) else { return Err(anyhow!("makefile not found.\n")) };
Ok(Makefile::new(Path::new(&makefile_name).to_path_buf()))
Makefile::new(Path::new(&makefile_name).to_path_buf())
}

pub fn to_include_files_string(&self) -> Vec<String> {
Expand All @@ -39,20 +39,21 @@ impl Makefile {

// I gave up writing tests using temp_dir because it was too difficult (it was necessary to change the implementation to some extent).
// It is not difficult to ensure that it works with manual tests, so I will not do it for now.
fn new(path: PathBuf) -> Makefile {
fn new(path: PathBuf) -> Result<Makefile> {
// If the file path does not exist, the make command cannot be executed in the first place,
// so it is not handled here.
let file_content = util::path_to_content(path.clone());
let file_content = util::path_to_content(path.clone())?;
let include_files = content_to_include_file_paths(file_content.clone())
.iter()
.map(|included_file_path| Makefile::new(included_file_path.clone()))
.filter_map(Result::ok)
.collect();

Makefile {
Ok(Makefile {
path,
include_files,
targets: Targets::new(file_content),
}
})
}

fn specify_makefile_name(target_path: String) -> Option<String> {
Expand Down
5 changes: 4 additions & 1 deletion src/models/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ impl Targets {
}

pub fn target_line_number(path: PathBuf, target_to_search: String) -> Option<u32> {
let content = util::path_to_content(path);
let content = match util::path_to_content(path) {
Ok(c) => c,
Err(_) => return None,
};
for (index, line) in content.lines().enumerate() {
if let Some(t) = line_to_target(line.to_string()) {
if t == target_to_search {
Expand Down
13 changes: 4 additions & 9 deletions src/models/util.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use std::{fs::File, io::Read, path::PathBuf};
use anyhow::{anyhow, Result};

pub fn path_to_content(path: PathBuf) -> String {
let mut content = String::new();
use std::{fs::read_to_string, path::PathBuf};

// Not handle cases where files are not found because make command cannot be
// executed in the first place if Makefile or included files are not found.
let mut f = File::open(path).unwrap();
f.read_to_string(&mut content).unwrap();

content
pub fn path_to_content(path: PathBuf) -> Result<String> {
read_to_string(path.as_path()).map_err(|e| anyhow!(e))
}

0 comments on commit afae0b4

Please sign in to comment.