Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use error type for A2lFile.write() as well #25

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 27 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@
text: String,
},
}
#[derive(Debug, Error)]
#[non_exhaustive]

Check warning on line 80 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L79-L80

Added lines #L79 - L80 were not covered by tests
pub enum A2lWriteError {
/// FileWriteError: An IoError that occurred while writing from a file
#[error("Could not write to {filename}: {ioerror}")]
FileWriteError {
filename: PathBuf,
ioerror: std::io::Error,
},
}

/**
Create a new a2l file
Expand Down Expand Up @@ -207,10 +217,10 @@

// if a built-in A2ml specification was passed as a string, then it is parsed here
if let Some(spec) = a2ml_spec {
match a2ml::parse_a2ml(&spec) {
Ok(parsed_spec) => parser.builtin_a2mlspec = Some(parsed_spec),
Err(parse_err) => return Err(A2lError::InvalidBuiltinA2mlSpec { parse_err }),
}
parser.builtin_a2mlspec = Some(
a2ml::parse_a2ml(&spec)
.map_err(|parse_err| A2lError::InvalidBuiltinA2mlSpec { parse_err })?,
);
}

// try to get the file version. Starting with 1.60, the ASAP2_VERSION element is mandatory. For
Expand All @@ -223,14 +233,8 @@
}
}
// build the a2l data structures from the tokens
let a2l_file = match A2lFile::parse(&mut parser, &context, 0) {
Ok(data) => data,
Err(parse_error) => {
return Err(A2lError::ParserError {
parser_error: parse_error,
})
}
};
let a2l_file = A2lFile::parse(&mut parser, &context, 0)
.map_err(|parser_error| A2lError::ParserError { parser_error })?;

// make sure this is the end of the input, i.e. no additional data after the parsed data
if let Some(token) = parser.peek_token() {
Expand Down Expand Up @@ -293,16 +297,8 @@
parser.set_file_version(1, 71)?; // doesn't really matter with strict = false

// build the a2l data structures from the tokens
let module = match Module::parse(&mut parser, &context, 0) {
Ok(data) => data,
Err(parse_error) => {
return Err(A2lError::ParserError {
parser_error: parse_error,
})
} //Err(parser.stringify_parse_error(&parse_error, true)),
};

Ok(module)
Module::parse(&mut parser, &context, 0)
.map_err(|parser_error| A2lError::ParserError { parser_error })
}

pub fn load_fragment_file<P: AsRef<Path>>(path: P) -> Result<Module, A2lError> {
Expand All @@ -319,7 +315,11 @@

/// write this `A2lFile` object to the given file
/// the banner will be placed inside a comment at the beginning of the file; "/*" an "*/" should not be part of the banner string
pub fn write<P: AsRef<Path>>(&self, path: P, banner: Option<&str>) -> Result<(), String> {
pub fn write<P: AsRef<Path>>(
&self,
path: P,
banner: Option<&str>,
) -> Result<(), A2lWriteError> {
let mut outstr = "".to_string();

let file_text = self.write_to_string();
Expand All @@ -334,13 +334,10 @@
}
outstr.write_str(&file_text).unwrap();

if let Err(err) = std::fs::write(&path, outstr) {
return Err(format!(
"Error while writing output {}: {}\n",
path.as_ref().display(),
err
));
}
std::fs::write(&path, outstr).map_err(|ioerror| A2lWriteError::FileWriteError {
filename: path.as_ref().to_path_buf(),
ioerror,
})?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Writer {
}
}

// add a string to the outout and prefix it with whitespace
// add a string to the output and prefix it with whitespace
pub(crate) fn add_str(&mut self, text: &str, offset: u32) {
self.add_whitespace(offset);
self.outstring.push_str(text);
Expand Down