1use crate::{
4 Asset, Error, ErrorKind, LocatorId, MediaRoot, Representation, ResourceId, Result,
5 SequenceNaming, Timestamp, uri::normalize_uri,
6};
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
10pub struct FileFacts {
11 size_bytes: u64,
12 modified_at: Option<Timestamp>,
13}
14
15impl FileFacts {
16 #[must_use]
18 pub const fn new(size_bytes: u64, modified_at: Option<Timestamp>) -> Self {
19 Self {
20 size_bytes,
21 modified_at,
22 }
23 }
24
25 #[must_use]
27 pub const fn size_bytes(self) -> u64 {
28 self.size_bytes
29 }
30
31 #[must_use]
33 pub const fn modified_at(self) -> Option<Timestamp> {
34 self.modified_at
35 }
36}
37
38#[derive(Clone, Debug, Eq, PartialEq)]
39struct FingerprintData {
40 algorithm: String,
41 version: u16,
42 value: Vec<u8>,
43}
44
45impl FingerprintData {
46 fn new(algorithm: impl Into<String>, version: u16, value: Vec<u8>) -> Result<Self> {
47 let algorithm = algorithm.into();
48 if algorithm.is_empty()
49 || algorithm.len() > 64
50 || !algorithm
51 .bytes()
52 .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
53 {
54 return Err(Error::new(
55 ErrorKind::InvalidArgument,
56 "fingerprint algorithm must be 1-64 ASCII letters, digits, '-' or '_'",
57 ));
58 }
59 if value.is_empty() {
60 return Err(Error::new(
61 ErrorKind::InvalidArgument,
62 "fingerprint value must not be empty",
63 ));
64 }
65 Ok(Self {
66 algorithm,
67 version,
68 value,
69 })
70 }
71}
72
73macro_rules! typed_fingerprint {
74 ($(#[$metadata:meta])* $name:ident) => {
75 $(#[$metadata])*
76 #[derive(Clone, Debug, Eq, PartialEq)]
77 pub struct $name(FingerprintData);
78
79 impl $name {
80 pub fn new(
86 algorithm: impl Into<String>,
87 version: u16,
88 value: Vec<u8>,
89 ) -> Result<Self> {
90 FingerprintData::new(algorithm, version, value).map(Self)
91 }
92
93 #[must_use]
95 pub fn algorithm(&self) -> &str {
96 &self.0.algorithm
97 }
98
99 #[must_use]
101 pub const fn version(&self) -> u16 {
102 self.0.version
103 }
104
105 #[must_use]
107 pub fn value(&self) -> &[u8] {
108 &self.0.value
109 }
110 }
111 };
112}
113
114typed_fingerprint!(
115 ResourceFingerprint
117);
118typed_fingerprint!(
119 RepresentationFingerprint
121);
122
123#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
125pub struct FingerprintSnapshot {
126 algorithm: String,
127 version: u16,
128 value: Vec<u8>,
129 observed_revision_sequence: Option<u64>,
130}
131
132impl FingerprintSnapshot {
133 pub fn new(
142 algorithm: impl Into<String>,
143 version: u16,
144 value: Vec<u8>,
145 observed_revision_sequence: Option<u64>,
146 ) -> Result<Self> {
147 let fingerprint = FingerprintData::new(algorithm, version, value)?;
148 Ok(Self {
149 algorithm: fingerprint.algorithm,
150 version: fingerprint.version,
151 value: fingerprint.value,
152 observed_revision_sequence,
153 })
154 }
155
156 #[must_use]
158 pub fn algorithm(&self) -> &str {
159 &self.algorithm
160 }
161
162 #[must_use]
164 pub const fn version(&self) -> u16 {
165 self.version
166 }
167
168 #[must_use]
170 pub fn value(&self) -> &[u8] {
171 &self.value
172 }
173
174 #[must_use]
176 pub const fn observed_revision_sequence(&self) -> Option<u64> {
177 self.observed_revision_sequence
178 }
179}
180
181#[derive(Clone, Debug, Eq, PartialEq)]
183pub struct Resource {
184 id: ResourceId,
185 fingerprints: Vec<ResourceFingerprint>,
186 file_facts: Option<FileFacts>,
187}
188
189impl Resource {
190 #[must_use]
192 pub fn new(
193 id: ResourceId,
194 fingerprints: Vec<ResourceFingerprint>,
195 file_facts: Option<FileFacts>,
196 ) -> Self {
197 Self {
198 id,
199 fingerprints,
200 file_facts,
201 }
202 }
203
204 #[must_use]
206 pub const fn id(&self) -> ResourceId {
207 self.id
208 }
209
210 #[must_use]
212 pub fn fingerprints(&self) -> &[ResourceFingerprint] {
213 &self.fingerprints
214 }
215
216 #[must_use]
218 pub const fn file_facts(&self) -> Option<FileFacts> {
219 self.file_facts
220 }
221
222 #[must_use]
225 pub fn with_observed_fingerprint(&self, fingerprint: &ResourceFingerprint) -> Self {
226 let mut fingerprints = self
227 .fingerprints
228 .iter()
229 .filter(|current| {
230 current.algorithm() != fingerprint.algorithm()
231 || current.version() != fingerprint.version()
232 })
233 .cloned()
234 .collect::<Vec<_>>();
235 fingerprints.push(fingerprint.clone());
236 Self::new(self.id, fingerprints, self.file_facts)
237 }
238}
239
240#[derive(Clone, Debug, Eq, PartialEq)]
246pub struct LocatorIdentity {
247 uri: String,
248 sequence_naming: Option<SequenceNaming>,
249}
250
251impl LocatorIdentity {
252 pub fn new(uri: impl Into<String>, sequence_naming: Option<SequenceNaming>) -> Result<Self> {
258 Ok(Self {
259 uri: normalize_uri(uri, "locator identity")?,
260 sequence_naming,
261 })
262 }
263
264 #[must_use]
266 pub fn uri(&self) -> &str {
267 &self.uri
268 }
269
270 #[must_use]
273 pub const fn sequence_naming(&self) -> Option<&SequenceNaming> {
274 self.sequence_naming.as_ref()
275 }
276}
277
278#[derive(Clone, Debug, Eq, PartialEq)]
280pub struct KnownMediaMatch {
281 asset: Asset,
282 representation: Representation,
283 resource: Resource,
284}
285
286impl KnownMediaMatch {
287 #[doc(hidden)]
289 #[must_use]
290 pub fn new(asset: Asset, representation: Representation, resource: Resource) -> Self {
291 Self {
292 asset,
293 representation,
294 resource,
295 }
296 }
297
298 #[must_use]
300 pub const fn asset(&self) -> &Asset {
301 &self.asset
302 }
303
304 #[must_use]
306 pub const fn representation(&self) -> &Representation {
307 &self.representation
308 }
309
310 #[must_use]
312 pub const fn resource(&self) -> &Resource {
313 &self.resource
314 }
315}
316
317#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
319#[non_exhaustive]
320pub enum LocatorAvailability {
321 Unknown,
323 Online,
325 Offline,
327}
328
329#[derive(Clone, Debug, Eq, PartialEq)]
334pub struct Locator {
335 id: LocatorId,
336 resource_id: ResourceId,
337 uri: String,
338 last_seen: Option<Timestamp>,
339 availability: LocatorAvailability,
340 media_root: Option<String>,
341 sequence_naming: Option<SequenceNaming>,
342}
343
344impl Locator {
345 pub fn new(
351 id: LocatorId,
352 resource_id: ResourceId,
353 uri: impl Into<String>,
354 last_seen: Option<Timestamp>,
355 availability: LocatorAvailability,
356 ) -> Result<Self> {
357 let uri = normalize_uri(uri, "locator")?;
358 Ok(Self {
359 id,
360 resource_id,
361 uri,
362 last_seen,
363 availability,
364 media_root: None,
365 sequence_naming: None,
366 })
367 }
368
369 #[must_use]
371 pub fn with_sequence_naming(mut self, naming: SequenceNaming) -> Self {
372 self.sequence_naming = Some(naming);
373 self
374 }
375
376 pub fn with_media_root(mut self, name: impl Into<String>) -> Result<Self> {
382 let name = name.into();
383 MediaRoot::validate_name(&name)?;
384 self.media_root = Some(name);
385 Ok(self)
386 }
387
388 #[must_use]
390 pub const fn id(&self) -> LocatorId {
391 self.id
392 }
393
394 #[must_use]
396 pub const fn resource_id(&self) -> ResourceId {
397 self.resource_id
398 }
399
400 #[must_use]
402 pub fn uri(&self) -> &str {
403 &self.uri
404 }
405
406 #[must_use]
408 pub const fn last_seen(&self) -> Option<Timestamp> {
409 self.last_seen
410 }
411
412 #[must_use]
414 pub const fn availability(&self) -> LocatorAvailability {
415 self.availability
416 }
417
418 #[must_use]
420 pub fn media_root(&self) -> Option<&str> {
421 self.media_root.as_deref()
422 }
423
424 #[must_use]
427 pub const fn sequence_naming(&self) -> Option<&SequenceNaming> {
428 self.sequence_naming.as_ref()
429 }
430}
431
432#[cfg(test)]
433mod tests {
434 use super::*;
435
436 #[test]
437 fn locator_belongs_to_a_resource() {
438 let resource_id = ResourceId::new();
439 let locator = Locator::new(
440 LocatorId::new(),
441 resource_id,
442 "file:///media/clip.mov",
443 None,
444 LocatorAvailability::Unknown,
445 )
446 .expect("valid locator");
447
448 assert_eq!(locator.resource_id(), resource_id);
449 assert_eq!(locator.uri(), "file:///media/clip.mov");
450 }
451
452 #[test]
453 fn fingerprint_domains_are_explicit() {
454 let resource = ResourceFingerprint::new("blake3", 1, vec![1]).expect("valid");
455 let representation =
456 RepresentationFingerprint::new("tree-blake3", 1, vec![2]).expect("valid");
457
458 assert_eq!(resource.algorithm(), "blake3");
459 assert_eq!(representation.algorithm(), "tree-blake3");
460 assert!(ResourceFingerprint::new("contains spaces", 1, vec![1]).is_err());
461 assert!(RepresentationFingerprint::new("valid", 1, Vec::new()).is_err());
462 }
463}