refactor: change name from builder to installer

This commit is contained in:
Didier Slof 2023-07-17 20:40:01 +02:00
parent c3db07dc64
commit 7a56816472
Signed by: didier
GPG key ID: 01E71F18AA4398E5
7 changed files with 70 additions and 41 deletions

View file

@ -0,0 +1,42 @@
use std::fmt::Display;
#[derive(Debug)]
pub enum BinError {
UnpackError(String),
}
impl Display for BinError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BinError::UnpackError(e) => write!(f, "Unpack error: {}", e),
}
}
}
#[derive(Debug)]
pub enum BuildError {}
impl Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
_ => write!(f, "Build error"),
}
}
}
#[derive(Debug)]
pub enum InstallError {
BuildError(BuildError),
BinError(BinError),
InstallError
}
impl ToString for InstallError {
fn to_string(&self) -> String {
match self {
InstallError::BuildError(e) => format!("Build error: {}", e),
InstallError::BinError(e) => format!("Bin error: {}", e),
InstallError::InstallError => "Install error".to_string(),
}
}
}

View file

@ -0,0 +1,66 @@
use std::fmt::Display;
use log::{debug, trace};
use errors::{BinError, BuildError, InstallError};
use manifest::Manifest;
use pkgfile::PKGFile;
use crate::tmpfs::TempDir;
pub mod errors;
#[derive(Debug)]
pub enum InstallType {
Build,
Bin
}
#[derive(Debug)]
pub struct PackageInstaller {
manifest: Manifest,
pkgfile: PKGFile,
install_type: InstallType
}
impl PackageInstaller {
pub fn new(m: Manifest, p: PKGFile, i: InstallType) -> PackageInstaller {
PackageInstaller {
manifest: m,
pkgfile: p,
install_type: i
}
}
fn extract_to(&self, path: String) -> Result<(), BinError> {
if !self.pkgfile.has_data() {
return Err(BinError::UnpackError("package has no data".to_string()));
}
if std::path::Path::new(&path).exists() {
return Err(BinError::UnpackError("path already exists".to_string()));
}
tar::Archive::new(self.pkgfile.data.as_slice())
.unpack(&path)
.map_err(|e| BinError::UnpackError(e.to_string()))
}
fn bin(&self) -> Result<(), BinError> {
let mut tmpdir = TempDir::default();
tmpdir.push(&self.manifest.package.name);
trace!("extracting package into: {}", tmpdir.to_string());
match self.extract_to(tmpdir.to_string()) {
Ok(_) => {},
Err(e) => return Err(e)
}
debug!("extracted package in: {}", tmpdir.to_string());
Ok(())
}
fn build(&self) -> Result<(), BuildError> {
Ok(())
}
pub fn install(&self) -> Result<(), InstallError> {
match self.install_type {
InstallType::Bin => self.bin().map_err(|e| InstallError::BinError(e)),
InstallType::Build => self.build().map_err(|e| InstallError::BuildError(e))
}
}
}