feat(solc): remapping helper functions (#1003)

This commit is contained in:
Matthias Seitz 2022-03-10 15:40:58 +01:00 committed by GitHub
parent f55f02476c
commit b7f278faf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 2 deletions

View File

@ -49,6 +49,13 @@ pub struct Remapping {
pub path: String, pub path: String,
} }
impl Remapping {
/// Convenience function for [`RelativeRemapping::new`]
pub fn into_relative(self, root: impl AsRef<Path>) -> RelativeRemapping {
RelativeRemapping::new(self, root)
}
}
#[derive(thiserror::Error, Debug, PartialEq, PartialOrd)] #[derive(thiserror::Error, Debug, PartialEq, PartialOrd)]
pub enum RemappingError { pub enum RemappingError {
#[error("no prefix found")] #[error("no prefix found")]
@ -222,6 +229,12 @@ impl RelativeRemapping {
self.path.parent = Some(root); self.path.parent = Some(root);
self.into() self.into()
} }
/// Converts this relative remapping into [`Remapping`] without the root path
pub fn to_relative_remapping(mut self) -> Remapping {
self.path.parent.take();
self.into()
}
} }
// Remappings are printed as `prefix=target` // Remappings are printed as `prefix=target`
@ -263,8 +276,8 @@ impl From<Remapping> for RelativeRemapping {
/// resolve as a `weird-erc20/=/var/lib/weird-erc20/src/` remapping. /// resolve as a `weird-erc20/=/var/lib/weird-erc20/src/` remapping.
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)] #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct RelativeRemappingPathBuf { pub struct RelativeRemappingPathBuf {
parent: Option<PathBuf>, pub parent: Option<PathBuf>,
path: PathBuf, pub path: PathBuf,
} }
impl RelativeRemappingPathBuf { impl RelativeRemappingPathBuf {
@ -647,6 +660,9 @@ mod tests {
assert_eq!(relative.path.relative(), Path::new(&remapping.path)); assert_eq!(relative.path.relative(), Path::new(&remapping.path));
assert_eq!(relative.path.original(), Path::new(&remapping.path)); assert_eq!(relative.path.original(), Path::new(&remapping.path));
assert!(relative.path.parent.is_none()); assert!(relative.path.parent.is_none());
let relative = RelativeRemapping::new(remapping.clone(), "/a/b");
assert_eq!(relative.to_relative_remapping(), Remapping::from_str("oz/=c/d/").unwrap());
} }
#[test] #[test]