chore(solc): improve remapping errors (#1570)

* chore(solc): improve remapping errors

* fix: update error names

* chore: rename test
This commit is contained in:
Matthias Seitz 2022-08-06 00:13:54 +02:00 committed by GitHub
parent ef60f704d1
commit a4ff90a94a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 11 deletions

View File

@ -67,22 +67,26 @@ impl Remapping {
#[derive(thiserror::Error, Debug, PartialEq, Eq, PartialOrd)]
pub enum RemappingError {
#[error("no prefix found")]
NoPrefix,
#[error("no target found")]
NoTarget,
#[error("invalid remapping format, found `{0}`, expected `<key>=<value>`")]
InvalidRemapping(String),
#[error("remapping key can't be empty, found `{0}`, expected `<key>=<value>`")]
EmptyRemappingKey(String),
#[error("remapping value must be a path, found `{0}`, expected `<key>=<value>`")]
EmptyRemappingValue(String),
}
impl FromStr for Remapping {
type Err = RemappingError;
fn from_str(remapping: &str) -> std::result::Result<Self, Self::Err> {
let (name, path) = remapping.split_once('=').ok_or(RemappingError::NoPrefix)?;
fn from_str(remapping: &str) -> Result<Self, Self::Err> {
let (name, path) = remapping
.split_once('=')
.ok_or_else(|| RemappingError::InvalidRemapping(remapping.to_string()))?;
if name.trim().is_empty() {
return Err(RemappingError::NoPrefix)
return Err(RemappingError::EmptyRemappingKey(remapping.to_string()))
}
if path.trim().is_empty() {
return Err(RemappingError::NoTarget)
return Err(RemappingError::EmptyRemappingValue(remapping.to_string()))
}
Ok(Remapping { name: name.to_string(), path: path.to_string() })
}
@ -743,17 +747,17 @@ mod tests {
}
#[test]
fn serde() {
fn remapping_errors() {
let remapping = "oz=../b/c/d";
let remapping = Remapping::from_str(remapping).unwrap();
assert_eq!(remapping.name, "oz".to_string());
assert_eq!(remapping.path, "../b/c/d".to_string());
let err = Remapping::from_str("").unwrap_err();
assert_eq!(err, RemappingError::NoPrefix);
matches!(err, RemappingError::InvalidRemapping(_));
let err = Remapping::from_str("oz=").unwrap_err();
assert_eq!(err, RemappingError::NoTarget);
matches!(err, RemappingError::EmptyRemappingValue(_));
}
// <https://doc.rust-lang.org/rust-by-example/std_misc/fs.html>