refactor: changed how packages work

This commit is contained in:
Strix 2023-10-14 22:39:44 +02:00
parent 9416794101
commit 3293e407ae
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
7 changed files with 134 additions and 59 deletions

View file

@ -1,11 +1,9 @@
use std::error::Error;
use std::fmt::Display;
use std::fs::File;
use std::io::Read;
use log::warn;
use pkgfile::PKGFile;
use crate::package::identifier::PackageIdentifier;
use reqwest::blocking::get;
use crate::package::identifier::PackageLocator;
#[derive(Debug)]
pub enum FetchError {
@ -26,34 +24,33 @@ impl Display for FetchError {
impl Error for FetchError {}
pub fn fetch_package(package_identifier: PackageIdentifier) -> Result<PKGFile, FetchError> {
match package_identifier {
PackageIdentifier::Path(path) => {
std::fs::read(path)
.map_err(|e| FetchError::IOError(e)).and_then(|bytes| {
PKGFile::try_from(bytes).map_err(|_| FetchError::ParseError)
})
}
PackageIdentifier::URI(uri) => {
// get file contents as bytes
let mut bytes = Vec::new();
match get(&uri) {
Ok(mut response) => {
match response.take(1024 * 1024).read_to_end(&mut bytes) {
Ok(_) => (),
Err(e) => return Err(FetchError::IOError(e)),
};
}
Err(e) => return Err(FetchError::HTTPError(e)),
pub fn fetch_by_path<S: Into<String>>(path: S) -> Result<PKGFile, FetchError> {
std::fs::read(path.into())
.map_err(|e| FetchError::IOError(e)).and_then(|bytes| {
PKGFile::try_from(bytes).map_err(|_| FetchError::ParseError)
})
}
pub fn fetch_by_uri<S: Into<String>>(uri: S) -> Result<PKGFile, FetchError> {
// get file contents as bytes
let mut bytes = Vec::new();
match get(uri.into()) {
Ok(mut response) => {
match response.take(1024 * 1024).read_to_end(&mut bytes) {
Ok(_) => (),
Err(e) => return Err(FetchError::IOError(e)),
};
// parse bytes as PKGFile
match PKGFile::try_from(bytes) {
Ok(pkgfile) => Ok(pkgfile),
Err(e) => Err(FetchError::ParseError),
}
}
PackageIdentifier::PackageLocator(package_locator) => {
Ok(PKGFile::default())
}
Err(e) => return Err(FetchError::HTTPError(e)),
};
// parse bytes as PKGFile
match PKGFile::try_from(bytes) {
Ok(pkgfile) => Ok(pkgfile),
Err(e) => Err(FetchError::ParseError),
}
}
}
pub fn fetch_by_package_locator(package_locator: PackageLocator) -> Result<PKGFile, FetchError> {
// TODO: search index for package locator
Ok(PKGFile::default())
}

View file

@ -14,7 +14,9 @@ impl Display for BinError {
}
#[derive(Debug)]
pub enum BuildError {}
pub enum BuildError {
InvalidManifest
}
impl Display for BuildError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@ -28,7 +30,7 @@ impl Display for BuildError {
pub enum InstallError {
BuildError(BuildError),
BinError(BinError),
InstallError
Generic
}
impl ToString for InstallError {
@ -36,7 +38,7 @@ impl ToString for InstallError {
match self {
InstallError::BuildError(e) => format!("Build error: {}", e),
InstallError::BinError(e) => format!("Bin error: {}", e),
InstallError::InstallError => "Install error".to_string(),
InstallError::Generic => "Install error".to_string(),
}
}
}

View file

@ -54,6 +54,13 @@ impl PackageInstaller {
}
fn build(&self) -> Result<(), BuildError> {
if let None = self.manifest.build.clone() {
return Err(BuildError::InvalidManifest);
}
let build_manifest = self.manifest.build.clone().unwrap();
// TODO: Check dependencies
Ok(())
}

View file

@ -1,3 +1,68 @@
use log::trace;
pub mod identifier;
pub mod installer;
pub mod fetch;
pub mod fetch;
pub struct Package {
pub identifier: identifier::PackageIdentifier,
pub pkgfile: pkgfile::PKGFile,
is_installed: bool,
is_indexed: bool,
}
impl Package {
pub fn new(identifier: identifier::PackageIdentifier, pkgfile: pkgfile::PKGFile) -> Package {
Package {
identifier,
pkgfile,
is_installed: false,
is_indexed: false,
}
}
pub fn manifest(&self) -> manifest::Manifest {
manifest::Manifest::try_from(self.pkgfile.manifest.clone())
.unwrap()
}
pub fn fetch(package_identifier: identifier::PackageIdentifier) -> Result<Package, fetch::FetchError> {
match &package_identifier {
identifier::PackageIdentifier::Path(path) => {
trace!("fetching package from path: {}", path);
let pkgfile = fetch::fetch_by_path(path).unwrap();
Ok(Package::new(package_identifier, pkgfile))
},
identifier::PackageIdentifier::URI(url) => {
trace!("fetching package from uri: {}", url);
let pkgfile = fetch::fetch_by_uri(url).unwrap();
Ok(Package::new(package_identifier, pkgfile))
},
identifier::PackageIdentifier::PackageLocator(locator) => {
trace!("fetching package from locator: {}", locator);
let pkgfile = fetch::fetch_by_package_locator(locator.clone()).unwrap();
Ok(Package::new(package_identifier, pkgfile))
},
}
}
pub fn install(&mut self) -> Result<(), installer::errors::InstallError> {
let manifest = self.manifest();
let installer = installer::PackageInstaller::new(manifest.clone(), self.pkgfile.clone(), installer::InstallType::Bin);
match installer.install() {
Ok(_) => {
self.is_installed = true;
Ok(())
},
Err(e) => Err(e)
}
}
pub fn is_installed(&self) -> bool {
self.is_installed
}
pub fn is_indexed(&self) -> bool {
self.is_indexed
}
}