diff options
Diffstat (limited to 'src/minemod.rs')
-rw-r--r-- | src/minemod.rs | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/minemod.rs b/src/minemod.rs index 6af50e3..d90605c 100644 --- a/src/minemod.rs +++ b/src/minemod.rs @@ -126,15 +126,39 @@ impl Modpack { } Ok(mods) } + + /// Copies the modpack to the given path. + /// + /// Note that the path should not include the modpack directory, that will be appended + /// automatically. + /// + /// Returns a new [`Modpack`] object pointing to the copy. + pub fn copy_to<P: AsRef<Path>>(&self, path: P) -> Result<Modpack> { + let mut options = CopyOptions::new(); + options.content_only = true; + let path = path.as_ref().join(self.name()?); + fs::create_dir_all(&path)?; + dir::copy(&self.path, &path, &options)?; + Modpack::open(&path) + } +} + +impl fmt::Display for Modpack { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} (pack)", self.name().map_err(|_| fmt::Error)?) + } } /// A thing that can contain mods. -pub trait ModContainer: Any { +pub trait ModContainer: Any + fmt::Display { /// Returns the name of the mod container. fn name(&self) -> Result<String>; /// Return all contained mods. fn mods(&self) -> Result<Vec<MineMod>>; + + /// Copies the content to the given directory. + fn install_to(&self, path: &Path) -> Result<Box<dyn ModContainer>>; } impl ModContainer for MineMod { @@ -145,6 +169,11 @@ impl ModContainer for MineMod { fn mods(&self) -> Result<Vec<MineMod>> { Ok(vec![self.clone()]) } + + fn install_to(&self, path: &Path) -> Result<Box<dyn ModContainer>> { + self.copy_to(path) + .map(|x| Box::new(x) as Box<dyn ModContainer>) + } } impl ModContainer for Modpack { @@ -155,6 +184,11 @@ impl ModContainer for Modpack { fn mods(&self) -> Result<Vec<MineMod>> { self.mods() } + + fn install_to(&self, path: &Path) -> Result<Box<dyn ModContainer>> { + self.copy_to(path) + .map(|x| Box::new(x) as Box<dyn ModContainer>) + } } /// Attempts to open the given path as either a single mod or a modpack. |