c2pa-rs

A fork of https://github.com/contentauth/c2pa-rs/
git clone git://archive.git.mtrnord.blog/mtrnords-photography-manager/c2pa-rs.git
Log | Files | Refs | README

commit f23822c4122c6f719d4cf665e4d2d0e9af9ca3ee
parent cfc423dd90929dd3a03a5eea57480bc0eb90a5a7
Author: Gavin  Peacock <gpeacock@adobe.com>
Date:   Fri, 15 Jul 2022 10:09:28 -0700

Adds an add_validation_status method to Ingredient (#68)

* ValidationStatus::new accepts Into<String>

* Add Ingredient.add_validation_status method.
Diffstat:
Msdk/src/ingredient.rs | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Msdk/src/validation_status.rs | 4++--
2 files changed, 56 insertions(+), 25 deletions(-)

diff --git a/sdk/src/ingredient.rs b/sdk/src/ingredient.rs @@ -233,7 +233,8 @@ impl Ingredient { /// Identifies this ingredient as the parent. /// - /// Only one ingredient can be flagged as a parent. + /// Only one ingredient should be flagged as a parent. + /// Use Manifest.set_parent to ensure this is the only parent ingredient pub fn set_is_parent(&mut self) -> &mut Self { self.is_parent = Some(true); self @@ -251,6 +252,15 @@ impl Ingredient { self } + /// Adds a [ValidationStatus] to this ingredient. + pub fn add_validation_status(&mut self, status: ValidationStatus) -> &mut Self { + match &mut self.validation_status { + None => self.validation_status = Some(vec![status]), + Some(validation_status) => validation_status.push(status), + } + self + } + /// Adds any desired [`Metadata`] to this ingredient. pub fn set_metadata(&mut self, metadata: Metadata) -> &mut Self { self.metadata = Some(metadata); @@ -651,33 +661,13 @@ pub struct IngredientOptions { } #[cfg(test)] -#[cfg(feature = "file_io")] mod tests { #![allow(clippy::expect_used)] #![allow(clippy::unwrap_used)] use super::*; - use crate::{assertions::Metadata, utils::test::fixture_path}; - - const MANIFEST_JPEG: &str = "C.jpg"; - const BAD_SIGNATURE_JPEG: &str = "E-sig-CA.jpg"; - const PRERELEASE_JPEG: &str = "prerelease.jpg"; - - fn stats(ingredient: &Ingredient) -> usize { - let thumb_size = ingredient.thumbnail().map_or(0, |(_, image)| image.len()); - let manifest_data_size = ingredient.manifest_data().map_or(0, |v| v.len()); - - println!( - " {} instance_id: {}, thumb size: {}, manifest_data size: {}", - ingredient.title(), - ingredient.instance_id(), - thumb_size, - manifest_data_size, - ); - ingredient.title().len() + ingredient.instance_id().len() + thumb_size + manifest_data_size - } - + use crate::assertions::Metadata; #[test] fn test_ingredient_api() { let mut ingredient = Ingredient::new("title", "format", "instance_id"); @@ -690,12 +680,14 @@ mod tests { .set_metadata(Metadata::new()) .set_thumbnail("format", "thumbnail".as_bytes().to_vec()) .set_active_manifest("active_manifest") - .set_manifest_data("data".as_bytes().to_vec()); + .set_manifest_data("data".as_bytes().to_vec()) + .add_validation_status(ValidationStatus::new("status_code")); assert_eq!(ingredient.title(), "title2"); assert_eq!(ingredient.format(), "format"); assert_eq!(ingredient.instance_id(), "instance_id"); assert_eq!(ingredient.document_id(), Some("document_id")); assert_eq!(ingredient.provenance(), Some("provenance")); + assert_eq!(ingredient.hash(), Some("hash")); assert!(ingredient.is_parent()); assert!(ingredient.metadata().is_some()); assert_eq!( @@ -704,9 +696,43 @@ mod tests { ); assert_eq!(ingredient.active_manifest(), Some("active_manifest")); assert_eq!(ingredient.manifest_data(), Some("data".as_bytes())); + assert_eq!( + ingredient.validation_status().unwrap()[0].code(), + "status_code" + ); + } +} + +#[cfg(test)] +#[cfg(feature = "file_io")] +mod tests_file_io { + #![allow(clippy::expect_used)] + #![allow(clippy::unwrap_used)] + + use super::*; + + use crate::utils::test::fixture_path; + + const MANIFEST_JPEG: &str = "C.jpg"; + const BAD_SIGNATURE_JPEG: &str = "E-sig-CA.jpg"; + const PRERELEASE_JPEG: &str = "prerelease.jpg"; + + fn stats(ingredient: &Ingredient) -> usize { + let thumb_size = ingredient.thumbnail().map_or(0, |(_, image)| image.len()); + let manifest_data_size = ingredient.manifest_data().map_or(0, |v| v.len()); + + println!( + " {} instance_id: {}, thumb size: {}, manifest_data size: {}", + ingredient.title(), + ingredient.instance_id(), + thumb_size, + manifest_data_size, + ); + ingredient.title().len() + ingredient.instance_id().len() + thumb_size + manifest_data_size } #[test] + #[cfg(feature = "file_io")] fn test_psd() { // std::env::set_var("RUST_LOG", "debug"); // env_logger::init(); @@ -722,6 +748,7 @@ mod tests { } #[test] + #[cfg(feature = "file_io")] fn test_jpg() { let ap = fixture_path(MANIFEST_JPEG); let ingredient = Ingredient::from_file(&ap).expect("from_file"); @@ -737,6 +764,7 @@ mod tests { } #[test] + #[cfg(feature = "file_io")] fn test_jpg_options() { let options = IngredientOptions { make_hash: true, @@ -758,6 +786,7 @@ mod tests { } #[test] + #[cfg(feature = "file_io")] fn test_png_no_claim() { let ap = fixture_path("libpng-test.png"); let ingredient = Ingredient::from_file(&ap).expect("from_file"); @@ -792,6 +821,7 @@ mod tests { } #[test] + #[cfg(feature = "file_io")] fn test_jpg_prerelease() { let ap = fixture_path(PRERELEASE_JPEG); let ingredient = Ingredient::from_file(&ap).expect("from_file"); @@ -811,6 +841,7 @@ mod tests { } #[test] + #[cfg(feature = "file_io")] fn test_jpg_nested() { let ap = fixture_path("CIE-sig-CA.jpg"); let ingredient = Ingredient::from_file(&ap).expect("from_file"); diff --git a/sdk/src/validation_status.rs b/sdk/src/validation_status.rs @@ -45,9 +45,9 @@ pub struct ValidationStatus { } impl ValidationStatus { - pub(crate) fn new(code: String) -> Self { + pub(crate) fn new<S: Into<String>>(code: S) -> Self { Self { - code, + code: code.into(), url: None, explanation: None, }