mirror of
https://codeberg.org/Toasterson/ips.git
synced 2026-04-10 21:30:41 +00:00
Add digest parsing
This commit is contained in:
parent
56ec3c718d
commit
f9ede380b1
3 changed files with 49 additions and 3 deletions
|
|
@ -20,14 +20,25 @@ trait FacetedAction {
|
||||||
fn remove_facet(&mut self, facet: Facet) -> bool;
|
fn remove_facet(&mut self, facet: Facet) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Action {
|
pub struct Action {
|
||||||
kind: ActionKind,
|
kind: ActionKind,
|
||||||
payload_reference: String,
|
payload: Payload,
|
||||||
properties: Vec<Property>,
|
properties: Vec<Property>,
|
||||||
facets: HashSet<Facet>,
|
facets: HashSet<Facet>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Action {
|
||||||
|
fn new(kind: ActionKind) -> Action{
|
||||||
|
Action{
|
||||||
|
kind,
|
||||||
|
payload: Payload::default(),
|
||||||
|
properties: Vec::new(),
|
||||||
|
facets: HashSet::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FacetedAction for Action {
|
impl FacetedAction for Action {
|
||||||
fn add_facet(&mut self, facet: Facet) -> bool {
|
fn add_facet(&mut self, facet: Facet) -> bool {
|
||||||
return self.facets.insert(facet)
|
return self.facets.insert(facet)
|
||||||
|
|
@ -59,7 +70,7 @@ impl FacetedAction for Dir {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug)]
|
||||||
pub struct File {
|
pub struct File {
|
||||||
pub payload: Payload,
|
pub payload: Payload,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
|
|
||||||
|
|
@ -37,10 +37,44 @@ pub struct Digest {
|
||||||
source: DigestSource,
|
source: DigestSource,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
impl FromStr for Digest {
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, failure::Error> {
|
||||||
|
if !s.contains(":") {
|
||||||
|
Ok(Digest{
|
||||||
|
hash: String::from(s),
|
||||||
|
algorithm: DigestAlgorithm::SHA1,
|
||||||
|
source: "primary".to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
let parts = String::from(s).split(':').collect();
|
||||||
|
if parts.len() < 3 {
|
||||||
|
Err(DigestError::InvalidDigestFormat{
|
||||||
|
digest: String::from(s),
|
||||||
|
details: "cannot split into 3 parts".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Digest{
|
||||||
|
source: String::from(&parts[0]),
|
||||||
|
algorithm: String::from(&parts[1]),
|
||||||
|
hash: String::from(&parts[2]),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Fail)]
|
#[derive(Debug, Fail)]
|
||||||
pub enum DigestError {
|
pub enum DigestError {
|
||||||
#[fail(display = "hashing algorithm {} is not known by this library", algorithm)]
|
#[fail(display = "hashing algorithm {} is not known by this library", algorithm)]
|
||||||
UnknownAlgorithm {
|
UnknownAlgorithm {
|
||||||
algorithm: String,
|
algorithm: String,
|
||||||
},
|
},
|
||||||
|
#[fail(display = "digest {} is not formatted properly: {}", digest, details)]
|
||||||
|
InvalidDigestFormat{
|
||||||
|
digest: String,
|
||||||
|
details: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
// obtain one at https://mozilla.org/MPL/2.0/.
|
// obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
use crate::digest::Digest;
|
use crate::digest::Digest;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PayloadCompressionAlgorithm {
|
pub enum PayloadCompressionAlgorithm {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue