use std::collections::HashMap;
#[cfg(feature = "json_schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::resource_store::UriOrResource;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "json_schema", derive(JsonSchema))]
pub struct ClaimGeneratorInfo {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<UriOrResource>,
#[serde(flatten)]
other: HashMap<String, Value>,
}
impl Default for ClaimGeneratorInfo {
fn default() -> Self {
Self {
name: crate::NAME.to_string(),
version: Some(env!("CARGO_PKG_VERSION").to_string()),
icon: None,
other: HashMap::new(),
}
}
}
impl ClaimGeneratorInfo {
pub fn new<S: Into<String>>(name: S) -> Self {
Self {
name: name.into(),
version: None,
icon: None,
other: HashMap::new(),
}
}
pub fn icon(&self) -> Option<&UriOrResource> {
self.icon.as_ref()
}
pub fn set_version<S: Into<String>>(&mut self, version: S) -> &mut Self {
self.version = Some(version.into());
self
}
pub fn set_icon<S: Into<UriOrResource>>(&mut self, uri_or_resource: S) -> &mut Self {
self.icon = Some(uri_or_resource.into());
self
}
pub fn insert<K, V>(&mut self, key: K, value: V) -> &Self
where
K: Into<String>,
V: Into<Value>,
{
self.other.insert(key.into(), value.into());
self
}
pub fn get(&self, key: &str) -> Option<&Value> {
self.other.get(key)
}
}
#[cfg(test)]
pub mod tests {
#![allow(clippy::expect_used)]
#![allow(clippy::unwrap_used)]
use super::*;
use crate::{hashed_uri::HashedUri, resource_store::ResourceRef};
#[test]
fn test_resource_ref() {
let mut g = super::ClaimGeneratorInfo::new("test");
g.set_version("1.0")
.set_icon(ResourceRef::new("image/svg", "myicon"));
let json = serde_json::to_string_pretty(&g).expect("Failed to serialize");
println!("{json}");
let result: ClaimGeneratorInfo =
serde_json::from_str(&json).expect("Failed to deserialize");
assert_eq!(g, result);
}
#[test]
fn test_hashed_uri() {
let mut g = super::ClaimGeneratorInfo::new("test");
g.set_version("1.0").set_icon(HashedUri::new(
"self#jumbf=c2pa.databoxes.data_box".to_string(),
None,
b"hashed",
));
let json = serde_json::to_string_pretty(&g).expect("Failed to serialize");
println!("{json}");
let result: ClaimGeneratorInfo =
serde_json::from_str(&json).expect("Failed to deserialize");
assert_eq!(g, result);
}
}