Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit b7f278f

Browse files
authored
feat(solc): remapping helper functions (#1003)
1 parent f55f024 commit b7f278f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Diff for: ethers-solc/src/remappings.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ pub struct Remapping {
4949
pub path: String,
5050
}
5151

52+
impl Remapping {
53+
/// Convenience function for [`RelativeRemapping::new`]
54+
pub fn into_relative(self, root: impl AsRef<Path>) -> RelativeRemapping {
55+
RelativeRemapping::new(self, root)
56+
}
57+
}
58+
5259
#[derive(thiserror::Error, Debug, PartialEq, PartialOrd)]
5360
pub enum RemappingError {
5461
#[error("no prefix found")]
@@ -222,6 +229,12 @@ impl RelativeRemapping {
222229
self.path.parent = Some(root);
223230
self.into()
224231
}
232+
233+
/// Converts this relative remapping into [`Remapping`] without the root path
234+
pub fn to_relative_remapping(mut self) -> Remapping {
235+
self.path.parent.take();
236+
self.into()
237+
}
225238
}
226239

227240
// Remappings are printed as `prefix=target`
@@ -263,8 +276,8 @@ impl From<Remapping> for RelativeRemapping {
263276
/// resolve as a `weird-erc20/=/var/lib/weird-erc20/src/` remapping.
264277
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
265278
pub struct RelativeRemappingPathBuf {
266-
parent: Option<PathBuf>,
267-
path: PathBuf,
279+
pub parent: Option<PathBuf>,
280+
pub path: PathBuf,
268281
}
269282

270283
impl RelativeRemappingPathBuf {
@@ -647,6 +660,9 @@ mod tests {
647660
assert_eq!(relative.path.relative(), Path::new(&remapping.path));
648661
assert_eq!(relative.path.original(), Path::new(&remapping.path));
649662
assert!(relative.path.parent.is_none());
663+
664+
let relative = RelativeRemapping::new(remapping.clone(), "/a/b");
665+
assert_eq!(relative.to_relative_remapping(), Remapping::from_str("oz/=c/d/").unwrap());
650666
}
651667

652668
#[test]

0 commit comments

Comments
 (0)