From f9ede380b16760a9cdc04f6a04e3bd424d454dae Mon Sep 17 00:00:00 2001 From: Till Wegmueller Date: Thu, 4 Jun 2020 16:09:09 +0200 Subject: [PATCH] Add digest parsing --- src/actions/mod.rs | 17 ++++++++++++++--- src/digest/mod.rs | 34 ++++++++++++++++++++++++++++++++++ src/payload/mod.rs | 1 + 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/actions/mod.rs b/src/actions/mod.rs index f753e79..44f029b 100644 --- a/src/actions/mod.rs +++ b/src/actions/mod.rs @@ -20,14 +20,25 @@ trait FacetedAction { fn remove_facet(&mut self, facet: Facet) -> bool; } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct Action { kind: ActionKind, - payload_reference: String, + payload: Payload, properties: Vec, facets: HashSet, } +impl Action { + fn new(kind: ActionKind) -> Action{ + Action{ + kind, + payload: Payload::default(), + properties: Vec::new(), + facets: HashSet::new(), + } + } +} + impl FacetedAction for Action { fn add_facet(&mut self, facet: Facet) -> bool { return self.facets.insert(facet) @@ -59,7 +70,7 @@ impl FacetedAction for Dir { } } -#[derive(Debug, Default)] +#[derive(Debug)] pub struct File { pub payload: Payload, pub path: String, diff --git a/src/digest/mod.rs b/src/digest/mod.rs index dc02a7a..7eee0a2 100644 --- a/src/digest/mod.rs +++ b/src/digest/mod.rs @@ -37,10 +37,44 @@ pub struct Digest { source: DigestSource, } + + +impl FromStr for Digest { + + fn from_str(s: &str) -> Result { + 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)] pub enum DigestError { #[fail(display = "hashing algorithm {} is not known by this library", algorithm)] UnknownAlgorithm { algorithm: String, }, + #[fail(display = "digest {} is not formatted properly: {}", digest, details)] + InvalidDigestFormat{ + digest: String, + details: String, + }, } \ No newline at end of file diff --git a/src/payload/mod.rs b/src/payload/mod.rs index 04350d5..72150a3 100644 --- a/src/payload/mod.rs +++ b/src/payload/mod.rs @@ -4,6 +4,7 @@ // obtain one at https://mozilla.org/MPL/2.0/. use crate::digest::Digest; +use std::str::FromStr; #[derive(Debug)] pub enum PayloadCompressionAlgorithm {