-
Couldn't load subscription status.
- Fork 100
feat: Asset reference assertion #1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7802555
feat: Create asset reference assertion
alextrnnn 5534f7e
docs: Add documentation
alextrnnn a911883
Merge branch 'main' into alextrnnn/asset-reference-assertion
gpeacock db0bfce
chore: Rename field and add integration test
alextrnnn afe264d
format: Fix formatting errors
alextrnnn 063f23a
fix: Use default version, do not export Reference type to and simplif…
alextrnnn 06d595b
chore: Simplify equality tests
alextrnnn 2752a71
Merge branch 'main' into alextrnnn/asset-reference-assertion
alextrnnn 581a617
fix: Add copyright
alextrnnn 03cf5f0
Merge branch 'main' into alextrnnn/asset-reference-assertion
alextrnnn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright 2022 Adobe. All rights reserved. | ||
| // This file is licensed to you under the Apache License, | ||
| // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
| // or the MIT license (http://opensource.org/licenses/MIT), | ||
| // at your option. | ||
|
|
||
| // Unless required by applicable law or agreed to in writing, | ||
| // this software is distributed on an "AS IS" BASIS, WITHOUT | ||
| // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
| // implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
| // specific language governing permissions and limitations under | ||
| // each license. | ||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| use crate::{ | ||
| assertion::{Assertion, AssertionBase, AssertionCbor}, | ||
| assertions::labels, | ||
| error::Result, | ||
| }; | ||
|
|
||
| /// An `AssetReference` assertion provides information on one or more locations of | ||
| /// where a copy of the asset may be obtained. | ||
| /// | ||
| /// This assertion contains a list of references, each one declaring a location expressed as a URI and | ||
| /// optionally a description. The URI may be either a single asset or it may reference a directory. | ||
| /// | ||
| /// <https://spec.c2pa.org/specifications/specifications/2.2/specs/C2PA_Specification.html#_asset_reference> | ||
| #[derive(Deserialize, Serialize, Debug, PartialEq)] | ||
| pub struct AssetReference { | ||
| pub references: Vec<Reference>, | ||
| } | ||
|
|
||
| impl AssetReference { | ||
| pub const LABEL: &'static str = labels::ASSET_REFERENCE; | ||
|
|
||
| /// Creates an AssetReference to a location. | ||
| pub fn new(uri: &str, description: Option<&str>) -> Self { | ||
| Self { | ||
| references: vec![Reference::new(uri, description)], | ||
| } | ||
| } | ||
|
|
||
| /// Adds an [`AssetReference`] to this assertion's list of references. | ||
| pub fn add_reference(mut self, uri: &str, description: Option<&str>) -> Self { | ||
| self.references.push(Reference::new(uri, description)); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| /// Defines a single location of where a copy of the asset may be obtained. | ||
| #[derive(Deserialize, Serialize, Debug, Default, PartialEq, Eq)] | ||
| pub struct Reference { | ||
| pub reference: ReferenceUri, | ||
|
|
||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub description: Option<String>, | ||
| } | ||
|
|
||
| impl Reference { | ||
| /// Creates a new reference to a location, and optionally a description. | ||
| pub fn new(uri: &str, description: Option<&str>) -> Self { | ||
| Reference { | ||
| reference: ReferenceUri { | ||
| uri: uri.to_owned(), | ||
| }, | ||
| description: description.map(String::from), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Deserialize, Serialize, Debug, Default, PartialEq, Eq)] | ||
| pub struct ReferenceUri { | ||
| pub uri: String, | ||
| } | ||
|
|
||
| impl AssertionCbor for AssetReference {} | ||
|
|
||
| impl AssertionBase for AssetReference { | ||
| const LABEL: &'static str = Self::LABEL; | ||
|
|
||
| fn to_assertion(&self) -> Result<Assertion> { | ||
| Self::to_cbor_assertion(self) | ||
| } | ||
|
|
||
| fn from_assertion(assertion: &Assertion) -> Result<Self> { | ||
| Self::from_cbor_assertion(assertion) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub mod tests { | ||
| #![allow(clippy::expect_used)] | ||
| #![allow(clippy::unwrap_used)] | ||
|
|
||
| use crate::{assertion::AssertionBase, assertions::AssetReference}; | ||
|
|
||
| #[test] | ||
| fn assertion_references() { | ||
| let original = AssetReference::new( | ||
| "https://some.storage.us/foo", | ||
| Some("A copy of the asset on the web"), | ||
| ) | ||
| .add_reference("ipfs://cid", Some("A copy of the asset on IPFS")); | ||
|
|
||
| assert_eq!(original.references.len(), 2); | ||
|
|
||
| let assertion = original.to_assertion().unwrap(); | ||
| assert_eq!(assertion.mime_type(), "application/cbor"); | ||
| assert_eq!(assertion.label(), AssetReference::LABEL); | ||
|
|
||
| let result = AssetReference::from_assertion(&assertion).unwrap(); | ||
| assert_eq!(result, original) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_json_round_trip() { | ||
| let json = serde_json::json!({ | ||
| "references": [ | ||
| { | ||
| "description": "A copy of the asset on the web", | ||
| "reference": { | ||
| "uri": "https://some.storage.us/foo" | ||
| } | ||
| }, | ||
| { | ||
| "description": "A copy of the asset on IPFS", | ||
| "reference": { | ||
| "uri": "ipfs://cid" | ||
| } | ||
| } | ||
| ] | ||
| }); | ||
|
|
||
| let original: AssetReference = serde_json::from_value(json).unwrap(); | ||
| let assertion = original.to_assertion().unwrap(); | ||
| let result = AssetReference::from_assertion(&assertion).unwrap(); | ||
|
|
||
| assert_eq!(result, original); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.