refactor: changed structure.

This commit is contained in:
Strix 2023-10-14 22:39:46 +02:00
parent 6098dce4aa
commit 4e64d3a73c
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
8 changed files with 113 additions and 89 deletions

View file

@ -1,6 +1,11 @@
use std::fmt::Display;
use std::path::Path;
use log::{info, trace};
use reqwest::blocking::get;
use pkgfile::PKGFile;
use crate::package::identifier::PackageIdentifier;
use crate::types::fetch::TryFetch;
pub mod fetch;
pub mod identifier;
pub mod installer;
@ -13,7 +18,7 @@ pub struct Package {
impl Package {
/// Create a new package from a package identifier and a package file.
pub fn new(identifier: identifier::PackageIdentifier, pkgfile: pkgfile::PKGFile) -> Package {
pub fn new(identifier: PackageIdentifier, pkgfile: PKGFile) -> Package {
Package {
identifier,
pkgfile,
@ -22,29 +27,6 @@ impl Package {
}
}
/// Fetch a package from a package identifier.
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))
}
}
}
/// Get the package manifest.
pub fn manifest(&self) -> manifest::Manifest {
manifest::Manifest::try_from(self.pkgfile.manifest.clone()).unwrap()
@ -96,3 +78,54 @@ impl Package {
unimplemented!();
}
}
#[derive(Debug)]
pub enum FetchError {
HTTPError(reqwest::Error),
IOError(std::io::Error),
ParseError,
}
impl Display for FetchError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
FetchError::HTTPError(e) => write!(f, "HTTP Error: {}", e),
FetchError::IOError(e) => write!(f, "IO Error: {}", e),
FetchError::ParseError => write!(f, "Parse Error"),
}
}
}
impl TryFetch<PackageIdentifier> for Package {
type Error = FetchError;
/// Fetch a package from a package identifier.
fn try_fetch(query: PackageIdentifier) -> Result<Package, Self::Error> {
let pkgfile = match &query {
PackageIdentifier::Path(s) => match PKGFile::try_from(Path::new(&s)) {
Ok(p) => Ok(p),
Err(e) => Err(FetchError::ParseError)
},
PackageIdentifier::URI(s) => {
let mut bytes = Vec::new();
match get::<String>(s.into()) {
Ok(response) => {
if let Ok(b) = response.bytes() {
bytes.extend(b);
}
},
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),
}
}
PackageIdentifier::PackageLocator(l) => Ok(PKGFile::default())
};
pkgfile
.map(|p| Package::new(query, p))
}
}