1use std::borrow::Cow;
17
18use crate::{range::ByteRange, segment::Name, structure::SegmentId, transfer::TransferEncoding};
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22#[non_exhaustive]
23pub enum ItemKind {
24 Xmp,
26 Exif,
28 C2pa,
33 EmbeddedAsset,
35 Other,
38}
39
40impl ItemKind {
41 pub fn as_str(&self) -> &'static str {
44 match self {
45 Self::Xmp => "xmp",
46 Self::Exif => "exif",
47 Self::C2pa => "c2pa",
48 Self::EmbeddedAsset => "embedded_asset",
49 Self::Other => "other",
50 }
51 }
52
53 pub fn category(&self) -> ItemCategory {
55 match self {
56 Self::Xmp | Self::Exif | Self::C2pa => ItemCategory::Metadata,
57 Self::EmbeddedAsset => ItemCategory::Asset,
58 Self::Other => ItemCategory::Other,
59 }
60 }
61}
62
63impl std::fmt::Display for ItemKind {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 f.write_str(self.as_str())
66 }
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
71#[non_exhaustive]
72pub enum ItemCategory {
73 Metadata,
75 Asset,
77 Other,
79}
80
81impl ItemCategory {
82 pub fn as_str(&self) -> &'static str {
84 match self {
85 Self::Metadata => "metadata",
86 Self::Asset => "asset",
87 Self::Other => "other",
88 }
89 }
90}
91
92impl std::fmt::Display for ItemCategory {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 f.write_str(self.as_str())
95 }
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
100#[non_exhaustive]
101pub enum Encoding {
102 Xml,
104 Tiff,
106 Jumbf,
108 IsoBmff,
110 Asset,
112 Unknown,
114}
115
116impl Encoding {
117 pub fn as_str(&self) -> &'static str {
119 match self {
120 Self::Xml => "xml",
121 Self::Tiff => "tiff",
122 Self::Jumbf => "jumbf",
123 Self::IsoBmff => "iso_bmff",
124 Self::Asset => "asset",
125 Self::Unknown => "unknown",
126 }
127 }
128}
129
130impl std::fmt::Display for Encoding {
131 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132 f.write_str(self.as_str())
133 }
134}
135
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
138#[non_exhaustive]
139pub enum RelationKind {
140 Thumbnail,
142 Auxiliary,
145 Describes,
147 Derived,
149 Other,
151}
152
153impl RelationKind {
154 pub fn from_native(native: [u8; 4]) -> Self {
156 match &native {
157 b"thmb" => Self::Thumbnail,
158 b"auxl" => Self::Auxiliary,
159 b"cdsc" => Self::Describes,
160 b"dimg" => Self::Derived,
161 _ => Self::Other,
162 }
163 }
164
165 pub fn as_str(&self) -> &'static str {
167 match self {
168 Self::Thumbnail => "thumbnail",
169 Self::Auxiliary => "auxiliary",
170 Self::Describes => "describes",
171 Self::Derived => "derived",
172 Self::Other => "other",
173 }
174 }
175}
176
177#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
179pub struct Relation {
180 kind: RelationKind,
181 native: [u8; 4],
182 target: u64,
183}
184
185impl Relation {
186 pub fn new(native: [u8; 4], target: u64) -> Self {
189 Self {
190 kind: RelationKind::from_native(native),
191 native,
192 target,
193 }
194 }
195
196 pub fn kind(&self) -> RelationKind {
198 self.kind
199 }
200
201 pub fn native(&self) -> [u8; 4] {
203 self.native
204 }
205
206 pub fn target(&self) -> u64 {
208 self.target
209 }
210}
211
212#[derive(Debug, Clone, PartialEq, Eq)]
216pub struct ItemInfo {
217 kind: ItemKind,
218 encoding: Encoding,
219 transfer: TransferEncoding,
220 label: Option<Cow<'static, str>>,
221 media_type: Option<Cow<'static, str>>,
222 jumbf_type: Option<[u8; 16]>,
223 native_type: Option<Name>,
224 native_id: Option<u64>,
225 uri: Option<Cow<'static, str>>,
226 relations: Vec<Relation>,
227}
228
229impl ItemInfo {
230 pub fn new(kind: ItemKind, encoding: Encoding) -> Self {
232 Self {
233 kind,
234 encoding,
235 transfer: TransferEncoding::Identity,
236 label: None,
237 media_type: None,
238 jumbf_type: None,
239 native_type: None,
240 native_id: None,
241 uri: None,
242 relations: Vec::new(),
243 }
244 }
245
246 pub fn xmp() -> Self {
248 Self::new(ItemKind::Xmp, Encoding::Xml)
249 }
250
251 pub fn exif() -> Self {
253 Self::new(ItemKind::Exif, Encoding::Tiff)
254 }
255
256 pub fn c2pa() -> Self {
258 Self::new(ItemKind::C2pa, Encoding::Jumbf)
259 }
260
261 pub fn transfer(mut self, transfer: TransferEncoding) -> Self {
263 self.transfer = transfer;
264 self
265 }
266
267 pub(crate) fn lacks_jumbf_type(&self) -> bool {
270 self.kind == ItemKind::Other
271 && self.encoding == Encoding::Jumbf
272 && self.jumbf_type.is_none()
273 }
274
275 pub(crate) fn transfer_encoding(&self) -> &TransferEncoding {
276 &self.transfer
277 }
278
279 pub fn label(mut self, label: impl Into<Cow<'static, str>>) -> Self {
281 self.label = Some(label.into());
282 self
283 }
284
285 pub fn media_type(mut self, media_type: impl Into<Cow<'static, str>>) -> Self {
287 self.media_type = Some(media_type.into());
288 self
289 }
290
291 pub fn jumbf_type(mut self, jumbf_type: Option<[u8; 16]>) -> Self {
293 self.jumbf_type = jumbf_type;
294 self
295 }
296
297 pub fn native_type(mut self, native_type: Name) -> Self {
299 self.native_type = Some(native_type);
300 self
301 }
302
303 pub fn native_id(mut self, native_id: u64) -> Self {
305 self.native_id = Some(native_id);
306 self
307 }
308
309 pub fn uri(mut self, uri: impl Into<Cow<'static, str>>) -> Self {
311 self.uri = Some(uri.into());
312 self
313 }
314
315 pub fn relation(mut self, relation: Relation) -> Self {
317 self.relations.push(relation);
318 self
319 }
320}
321
322#[derive(Debug, Clone, Copy, PartialEq, Eq)]
324pub struct Extent {
325 pub(crate) range: ByteRange,
326 pub(crate) segment: SegmentId,
327}
328
329impl Extent {
330 pub fn range(&self) -> ByteRange {
332 self.range
333 }
334
335 pub fn segment(&self) -> SegmentId {
337 self.segment
338 }
339}
340
341#[derive(Debug, Clone)]
347pub struct Item {
348 pub(crate) info: ItemInfo,
349 pub(crate) first: Extent,
350 pub(crate) rest: Vec<Extent>,
351}
352
353impl Item {
354 pub fn kind(&self) -> ItemKind {
356 self.info.kind
357 }
358
359 pub fn encoding(&self) -> Encoding {
361 self.info.encoding
362 }
363
364 pub fn transfer(&self) -> &TransferEncoding {
369 &self.info.transfer
370 }
371
372 pub(crate) fn info(&self) -> &ItemInfo {
374 &self.info
375 }
376
377 pub fn label(&self) -> Option<&str> {
382 self.info.label.as_deref()
383 }
384
385 pub fn media_type(&self) -> Option<&str> {
390 self.info.media_type.as_deref()
391 }
392
393 pub fn jumbf_type(&self) -> Option<[u8; 16]> {
400 self.info.jumbf_type
401 }
402
403 pub fn native_type(&self) -> Option<Name> {
406 self.info.native_type
407 }
408
409 pub fn native_id(&self) -> Option<u64> {
412 self.info.native_id
413 }
414
415 pub fn uri(&self) -> Option<&str> {
418 self.info.uri.as_deref()
419 }
420
421 pub fn relations(&self) -> &[Relation] {
423 &self.info.relations
424 }
425
426 pub fn extents(&self) -> impl Iterator<Item = &Extent> {
428 std::iter::once(&self.first).chain(self.rest.iter())
429 }
430
431 pub fn extent_count(&self) -> usize {
433 1 + self.rest.len()
434 }
435
436 pub fn len(&self) -> u64 {
438 self.extents()
439 .fold(0u64, |acc, p| acc.saturating_add(p.range.len()))
440 }
441
442 pub fn is_empty(&self) -> bool {
444 self.len() == 0
445 }
446
447 pub fn is_in(&self, id: SegmentId) -> bool {
449 self.extents().any(|e| e.segment == id)
450 }
451}
452
453#[cfg(test)]
454mod tests {
455 use super::*;
456
457 #[test]
458 fn kinds_map_to_categories_and_strings() {
459 assert_eq!(ItemKind::Xmp.category(), ItemCategory::Metadata);
460 assert_eq!(ItemKind::C2pa.category(), ItemCategory::Metadata);
461 assert_eq!(ItemKind::C2pa.as_str(), "c2pa");
462 assert_eq!(ItemInfo::c2pa().encoding, Encoding::Jumbf);
463 assert_eq!(ItemKind::Exif.category(), ItemCategory::Metadata);
464 assert_eq!(ItemKind::EmbeddedAsset.category(), ItemCategory::Asset);
465 assert_eq!(ItemKind::Other.category(), ItemCategory::Other);
466 assert_eq!(ItemKind::EmbeddedAsset.as_str(), "embedded_asset");
467 assert_eq!(Encoding::IsoBmff.as_str(), "iso_bmff");
468 assert_eq!(ItemInfo::exif().encoding, Encoding::Tiff);
469 }
470
471 #[test]
472 fn relations_keep_their_native_type() {
473 for (native, kind) in [
474 (*b"thmb", RelationKind::Thumbnail),
475 (*b"auxl", RelationKind::Auxiliary),
476 (*b"cdsc", RelationKind::Describes),
477 (*b"dimg", RelationKind::Derived),
478 (*b"base", RelationKind::Other),
479 ] {
480 let r = Relation::new(native, 7);
481 assert_eq!((r.kind(), r.native(), r.target()), (kind, native, 7));
482 }
483 }
484}