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

labels.rs (8148B)


      1 // Copyright 2022 Adobe. All rights reserved.
      2 // This file is licensed to you under the Apache License,
      3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
      4 // or the MIT license (http://opensource.org/licenses/MIT),
      5 // at your option.
      6 
      7 // Unless required by applicable law or agreed to in writing,
      8 // this software is distributed on an "AS IS" BASIS, WITHOUT
      9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
     10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
     11 // specific language governing permissions and limitations under
     12 // each license.
     13 
     14 #![deny(missing_docs)]
     15 
     16 //! Labels for assertion types as defined in C2PA 1.0 Specification.
     17 //!
     18 //! These constants do not include version suffixes.
     19 //!
     20 //! See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_c2pa_standard_assertions>.
     21 
     22 /// Label prefix for a claim assertion.
     23 ///
     24 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_overview_4>.
     25 pub const CLAIM: &str = "c2pa.claim";
     26 
     27 /// Label prefix for an assertion metadata assertion.
     28 ///
     29 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_metadata_about_assertions>.
     30 pub const ASSERTION_METADATA: &str = "c2pa.assertion.metadata";
     31 
     32 /// Label prefix for a data hash assertion.
     33 ///
     34 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_data_hash>.
     35 pub const DATA_HASH: &str = "c2pa.hash.data";
     36 
     37 /// Label prefix for a box hash assertion.
     38 ///
     39 /// See <https://c2pa.org/specifications/specifications/1.3/specs/C2PA_Specification.html#_general_boxes_hash>.
     40 pub const BOX_HASH: &str = "c2pa.hash.boxes";
     41 
     42 /// Label prefix for a BMFF-based hash assertion.
     43 ///
     44 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_bmff_based_hash>.
     45 pub const BMFF_HASH: &str = "c2pa.hash.bmff";
     46 
     47 /// Label prefix for a soft binding assertion.
     48 ///
     49 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_soft_binding_2>.
     50 pub const SOFT_BINDING: &str = "c2pa.soft-binding";
     51 
     52 /// Label prefix for a cloud data assertion.
     53 ///
     54 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_cloud_data>.
     55 pub const CLOUD_DATA: &str = "c2pa.cloud-data";
     56 
     57 /// Label prefix for a thumbnail assertion.
     58 ///
     59 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_thumbnail>.
     60 pub const THUMBNAIL: &str = "c2pa.thumbnail";
     61 
     62 /// Label prefix for a claim thumbnail assertion.
     63 ///
     64 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_thumbnail>.
     65 pub const CLAIM_THUMBNAIL: &str = "c2pa.thumbnail.claim";
     66 
     67 /// Label prefix for an ingredient thumbnail assertion.
     68 ///
     69 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_thumbnail>.
     70 pub const INGREDIENT_THUMBNAIL: &str = "c2pa.thumbnail.ingredient";
     71 
     72 /// Label prefix for a JPEG claim thumbnail assertion.
     73 ///
     74 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_thumbnail>.
     75 pub const JPEG_CLAIM_THUMBNAIL: &str = "c2pa.thumbnail.claim.jpeg";
     76 
     77 /// Label prefix for a JPEG ingredient thumbnail assertion.
     78 ///
     79 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_thumbnail>.
     80 pub const JPEG_INGREDIENT_THUMBNAIL: &str = "c2pa.thumbnail.ingredient.jpeg";
     81 
     82 /// Label prefix for a PNG claim thumbnail assertion.
     83 ///
     84 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_thumbnail>.
     85 pub const PNG_CLAIM_THUMBNAIL: &str = "c2pa.thumbnail.claim.png";
     86 
     87 /// Label prefix for a PNG ingredient thumbnail assertion.
     88 ///
     89 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_thumbnail>.
     90 pub const PNG_INGREDIENT_THUMBNAIL: &str = "c2pa.thumbnail.ingredient.png";
     91 
     92 /// Label prefix for an actions assertion.
     93 ///
     94 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_actions>.
     95 pub const ACTIONS: &str = "c2pa.actions";
     96 
     97 /// Label prefix for an ingredient assertion.
     98 ///
     99 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_ingredient>.
    100 pub const INGREDIENT: &str = "c2pa.ingredient";
    101 
    102 /// Label prefix for a depthmap assertion.
    103 ///
    104 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_depthmap>.
    105 pub const DEPTHMAP: &str = "c2pa.depthmap";
    106 
    107 /// Label prefix for a GDepth depthmap assertion.
    108 ///
    109 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_gdepth_depthmap>.
    110 pub const DEPTHMAP_GDEPTH: &str = "c2pa.depthmap.GDepth";
    111 
    112 /// Label prefix for an EXIF information assertion.
    113 ///
    114 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_exif_information>.
    115 pub const EXIF: &str = "stds.exif";
    116 
    117 /// Label prefix for an IPTC photo metadata assertion.
    118 ///
    119 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_iptc_photo_metadata>.
    120 pub const IPTC_PHOTO_METADATA: &str = "stds.iptc.photo-metadata";
    121 
    122 /// Label prefix for any assertion based on a schema.org grammar.
    123 ///
    124 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_use_of_schema_org>.
    125 pub const SCHEMA_ORG: &str = "schema.org";
    126 
    127 /// Label prefix for a claim review assertion.
    128 ///
    129 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_claim_review>.
    130 pub const CLAIM_REVIEW: &str = "stds.schema-org.ClaimReview";
    131 
    132 /// Label prefix for a creative work assertion.
    133 ///
    134 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_creative_work>.
    135 pub const CREATIVE_WORK: &str = "stds.schema-org.CreativeWork";
    136 
    137 /// Return the version suffix from an assertion label if it exists.
    138 ///
    139 /// When an assertion's schema is changed in a backwards-compatible manner,
    140 /// the label would consist of an incremented version number, for example
    141 /// moving from `c2pa.ingredient` to `c2pa.ingredient.v2`.
    142 ///
    143 /// If such a suffix exists (`.v(integer)`), that will be returned; otherwise,
    144 /// `None` will be returned.
    145 ///
    146 /// See <https://c2pa.org/specifications/specifications/1.0/specs/C2PA_Specification.html#_versioning>.
    147 ///
    148 /// # Examples
    149 ///
    150 /// ```
    151 /// use c2pa::assertions::labels;
    152 ///
    153 /// assert_eq!(labels::version("c2pa.ingredient"), None);
    154 /// assert_eq!(labels::version("c2pa.ingredient.v2"), Some(2));
    155 /// assert_eq!(labels::version("c2pa.ingredient.V2"), None);
    156 /// assert_eq!(labels::version("c2pa.ingredient.x2"), None);
    157 /// assert_eq!(labels::version("c2pa.ingredient.v-2"), None);
    158 /// ```
    159 pub fn version(label: &str) -> Option<usize> {
    160     let components: Vec<&str> = label.split('.').collect();
    161     if let Some(last) = components.last() {
    162         if last.len() > 1 {
    163             let (ver, ver_inst_str) = last.split_at(1);
    164             if ver == "v" {
    165                 if let Ok(ver) = ver_inst_str.parse::<usize>() {
    166                     return Some(ver);
    167                 }
    168             }
    169         }
    170     }
    171 
    172     None
    173 }
    174 
    175 /// Given a thumbnail label prefix such as `CLAIM_THUMBNAIL` and a file
    176 /// format (such as `png`), create a suitable label for an assertion.
    177 ///
    178 /// # Examples
    179 ///
    180 /// ```
    181 /// use c2pa::assertions::labels;
    182 ///
    183 /// assert_eq!(
    184 ///     labels::add_thumbnail_format(labels::CLAIM_THUMBNAIL, "image/jpeg"),
    185 ///     labels::JPEG_CLAIM_THUMBNAIL
    186 /// );
    187 ///
    188 /// assert_eq!(
    189 ///     labels::add_thumbnail_format(labels::INGREDIENT_THUMBNAIL, "image/png"),
    190 ///     labels::PNG_INGREDIENT_THUMBNAIL
    191 /// );
    192 /// ```
    193 pub fn add_thumbnail_format(label: &str, format: &str) -> String {
    194     match format {
    195         "image/jpeg" | "jpeg" | "jpg" => format!("{label}.jpeg"),
    196         "image/png" | "png" => format!("{label}.png"),
    197         _ => {
    198             let p: Vec<&str> = format.split('/').collect();
    199             if p.len() == 2 && p[0] == "image" {
    200                 format!("{}/{}", label, p[1]) // try to parse other image types
    201             } else {
    202                 format!("{label}/{format}")
    203             }
    204         }
    205     }
    206 }