refactor: changed structure.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
6335be4b04
commit
759c219f96
8 changed files with 113 additions and 89 deletions
|
@ -1,3 +1,8 @@
|
|||
use std::error::Error;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PKGFile {
|
||||
pub manifest: String,
|
||||
|
@ -23,28 +28,56 @@ impl Default for PKGFile {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for PKGFile {
|
||||
type Error = ();
|
||||
#[derive(Debug)]
|
||||
pub enum PKGFileError {
|
||||
IOError(io::Error),
|
||||
ParsingError(String)
|
||||
}
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, ()> {
|
||||
impl Display for PKGFileError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
PKGFileError::IOError(e) => write!(f, "IOError: {}", e),
|
||||
PKGFileError::ParsingError(s) => write!(f, "ParsingError: {}", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for PKGFileError {}
|
||||
|
||||
impl<'a> TryFrom<&'a Path> for PKGFile {
|
||||
type Error = PKGFileError;
|
||||
fn try_from(path: &'a Path) -> Result<Self, Self::Error> {
|
||||
let d = match std::fs::read(path) {
|
||||
Ok(d) => d,
|
||||
Err(e) => return Err(PKGFileError::IOError(e))
|
||||
};
|
||||
PKGFile::try_from(d)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for PKGFile {
|
||||
type Error = PKGFileError;
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
match value[0] {
|
||||
1 => {
|
||||
let header: Vec<u32> = value[..3].iter().map(|v| u32::from(*v)).collect();
|
||||
let manifest_size: u32 = (header[1] << 8) | header[2];
|
||||
if manifest_size > value.len() as u32 {
|
||||
return Err(());
|
||||
return Err(PKGFileError::ParsingError("Invalid header length".into()));
|
||||
}
|
||||
Ok(PKGFile {
|
||||
manifest: match String::from_utf8(
|
||||
value[3..(manifest_size as usize + 3)].to_vec(),
|
||||
) {
|
||||
Ok(s) => s,
|
||||
_ => return Err(()),
|
||||
_ => return Err(PKGFileError::ParsingError("Could not parse manifest".into())),
|
||||
},
|
||||
data: value[(manifest_size as usize + 3)..].to_vec(),
|
||||
})
|
||||
}
|
||||
_ => Err(()),
|
||||
_ => Err(PKGFileError::ParsingError("Unknown pkgfile version".into())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue