use std::borrow::Cow;
use crate::{range::ByteRange, segment::Name, structure::SegmentId, transfer::TransferEncoding};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ItemKind {
Xmp,
Exif,
C2pa,
EmbeddedAsset,
Other,
}
impl ItemKind {
pub fn as_str(&self) -> &'static str {
match self {
Self::Xmp => "xmp",
Self::Exif => "exif",
Self::C2pa => "c2pa",
Self::EmbeddedAsset => "embedded_asset",
Self::Other => "other",
}
}
pub fn category(&self) -> ItemCategory {
match self {
Self::Xmp | Self::Exif | Self::C2pa => ItemCategory::Metadata,
Self::EmbeddedAsset => ItemCategory::Asset,
Self::Other => ItemCategory::Other,
}
}
}
impl std::fmt::Display for ItemKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ItemCategory {
Metadata,
Asset,
Other,
}
impl ItemCategory {
pub fn as_str(&self) -> &'static str {
match self {
Self::Metadata => "metadata",
Self::Asset => "asset",
Self::Other => "other",
}
}
}
impl std::fmt::Display for ItemCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Encoding {
Xml,
Tiff,
Jumbf,
IsoBmff,
Asset,
Unknown,
}
impl Encoding {
pub fn as_str(&self) -> &'static str {
match self {
Self::Xml => "xml",
Self::Tiff => "tiff",
Self::Jumbf => "jumbf",
Self::IsoBmff => "iso_bmff",
Self::Asset => "asset",
Self::Unknown => "unknown",
}
}
}
impl std::fmt::Display for Encoding {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RelationKind {
Thumbnail,
Auxiliary,
Describes,
Derived,
Other,
}
impl RelationKind {
pub fn from_native(native: [u8; 4]) -> Self {
match &native {
b"thmb" => Self::Thumbnail,
b"auxl" => Self::Auxiliary,
b"cdsc" => Self::Describes,
b"dimg" => Self::Derived,
_ => Self::Other,
}
}
pub fn as_str(&self) -> &'static str {
match self {
Self::Thumbnail => "thumbnail",
Self::Auxiliary => "auxiliary",
Self::Describes => "describes",
Self::Derived => "derived",
Self::Other => "other",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Relation {
kind: RelationKind,
native: [u8; 4],
target: u64,
}
impl Relation {
pub fn new(native: [u8; 4], target: u64) -> Self {
Self {
kind: RelationKind::from_native(native),
native,
target,
}
}
pub fn kind(&self) -> RelationKind {
self.kind
}
pub fn native(&self) -> [u8; 4] {
self.native
}
pub fn target(&self) -> u64 {
self.target
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ItemInfo {
kind: ItemKind,
encoding: Encoding,
transfer: TransferEncoding,
label: Option<Cow<'static, str>>,
media_type: Option<Cow<'static, str>>,
jumbf_type: Option<[u8; 16]>,
native_type: Option<Name>,
native_id: Option<u64>,
uri: Option<Cow<'static, str>>,
relations: Vec<Relation>,
}
impl ItemInfo {
pub fn new(kind: ItemKind, encoding: Encoding) -> Self {
Self {
kind,
encoding,
transfer: TransferEncoding::Identity,
label: None,
media_type: None,
jumbf_type: None,
native_type: None,
native_id: None,
uri: None,
relations: Vec::new(),
}
}
pub fn xmp() -> Self {
Self::new(ItemKind::Xmp, Encoding::Xml)
}
pub fn exif() -> Self {
Self::new(ItemKind::Exif, Encoding::Tiff)
}
pub fn c2pa() -> Self {
Self::new(ItemKind::C2pa, Encoding::Jumbf)
}
pub fn transfer(mut self, transfer: TransferEncoding) -> Self {
self.transfer = transfer;
self
}
pub(crate) fn lacks_jumbf_type(&self) -> bool {
self.kind == ItemKind::Other
&& self.encoding == Encoding::Jumbf
&& self.jumbf_type.is_none()
}
pub(crate) fn transfer_encoding(&self) -> &TransferEncoding {
&self.transfer
}
pub fn label(mut self, label: impl Into<Cow<'static, str>>) -> Self {
self.label = Some(label.into());
self
}
pub fn media_type(mut self, media_type: impl Into<Cow<'static, str>>) -> Self {
self.media_type = Some(media_type.into());
self
}
pub fn jumbf_type(mut self, jumbf_type: Option<[u8; 16]>) -> Self {
self.jumbf_type = jumbf_type;
self
}
pub fn native_type(mut self, native_type: Name) -> Self {
self.native_type = Some(native_type);
self
}
pub fn native_id(mut self, native_id: u64) -> Self {
self.native_id = Some(native_id);
self
}
pub fn uri(mut self, uri: impl Into<Cow<'static, str>>) -> Self {
self.uri = Some(uri.into());
self
}
pub fn relation(mut self, relation: Relation) -> Self {
self.relations.push(relation);
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Extent {
pub(crate) range: ByteRange,
pub(crate) segment: SegmentId,
}
impl Extent {
pub fn range(&self) -> ByteRange {
self.range
}
pub fn segment(&self) -> SegmentId {
self.segment
}
}
#[derive(Debug, Clone)]
pub struct Item {
pub(crate) info: ItemInfo,
pub(crate) first: Extent,
pub(crate) rest: Vec<Extent>,
}
impl Item {
pub fn kind(&self) -> ItemKind {
self.info.kind
}
pub fn encoding(&self) -> Encoding {
self.info.encoding
}
pub fn transfer(&self) -> &TransferEncoding {
&self.info.transfer
}
pub(crate) fn info(&self) -> &ItemInfo {
&self.info
}
pub fn label(&self) -> Option<&str> {
self.info.label.as_deref()
}
pub fn media_type(&self) -> Option<&str> {
self.info.media_type.as_deref()
}
pub fn jumbf_type(&self) -> Option<[u8; 16]> {
self.info.jumbf_type
}
pub fn native_type(&self) -> Option<Name> {
self.info.native_type
}
pub fn native_id(&self) -> Option<u64> {
self.info.native_id
}
pub fn uri(&self) -> Option<&str> {
self.info.uri.as_deref()
}
pub fn relations(&self) -> &[Relation] {
&self.info.relations
}
pub fn extents(&self) -> impl Iterator<Item = &Extent> {
std::iter::once(&self.first).chain(self.rest.iter())
}
pub fn extent_count(&self) -> usize {
1 + self.rest.len()
}
pub fn len(&self) -> u64 {
self.extents()
.fold(0u64, |acc, p| acc.saturating_add(p.range.len()))
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn is_in(&self, id: SegmentId) -> bool {
self.extents().any(|e| e.segment == id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kinds_map_to_categories_and_strings() {
assert_eq!(ItemKind::Xmp.category(), ItemCategory::Metadata);
assert_eq!(ItemKind::C2pa.category(), ItemCategory::Metadata);
assert_eq!(ItemKind::C2pa.as_str(), "c2pa");
assert_eq!(ItemInfo::c2pa().encoding, Encoding::Jumbf);
assert_eq!(ItemKind::Exif.category(), ItemCategory::Metadata);
assert_eq!(ItemKind::EmbeddedAsset.category(), ItemCategory::Asset);
assert_eq!(ItemKind::Other.category(), ItemCategory::Other);
assert_eq!(ItemKind::EmbeddedAsset.as_str(), "embedded_asset");
assert_eq!(Encoding::IsoBmff.as_str(), "iso_bmff");
assert_eq!(ItemInfo::exif().encoding, Encoding::Tiff);
}
#[test]
fn relations_keep_their_native_type() {
for (native, kind) in [
(*b"thmb", RelationKind::Thumbnail),
(*b"auxl", RelationKind::Auxiliary),
(*b"cdsc", RelationKind::Describes),
(*b"dimg", RelationKind::Derived),
(*b"base", RelationKind::Other),
] {
let r = Relation::new(native, 7);
assert_eq!((r.kind(), r.native(), r.target()), (kind, native, 7));
}
}
}