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