unarchiver 0.0.2

CLI Tool for un/archive multiple formats of compressed files
Documentation
mod kind;

pub use kind::ArchiveKind;

use std::path::{Path, PathBuf};

use anyhow::{bail, Result};

pub trait Unarchiver {
    async fn archive<P: AsRef<Path>>(&self, path: P, out: P) -> Result<()>;
    async fn unarchive<P: AsRef<Path>>(&self, path: P, out: P) -> Result<()>;
}

/// [`Archive`] represents a compressed directory or archive which
/// can be of any supported format.
pub struct Archive {
    pub path: PathBuf,
    pub kind: ArchiveKind,
}

impl Archive {
    pub fn new<P: AsRef<Path>>(path: P, kind: ArchiveKind) -> Result<Self> {
        if path.as_ref().exists() {
            let path = PathBuf::from(path.as_ref());

            return Ok(Self { path, kind });
        }

        bail!("The path doesnt exists")
    }

    pub async fn unarchive<P: AsRef<Path>>(&self, out: P) -> Result<()> {
        let input = PathBuf::from(&self.path);
        let output = PathBuf::from(out.as_ref());

        self.kind.unarchive(&input, &output).await
    }

    pub async fn archive<P: AsRef<Path>>(&self, out: P) -> Result<()> {
        let input = PathBuf::from(&self.path);
        let output = PathBuf::from(out.as_ref());

        self.kind.archive(&input, &output).await
    }
}