aster_forge_cloud_files_macos_bridge/
version.rs1use aster_forge_cloud_files_core::{ContentRevision, MetadataRevision};
4
5use crate::{MacosBridgeError, Result};
6
7pub const FILE_PROVIDER_ITEM_VERSION_COMPONENT_MAX_BYTES: usize = 128;
9
10const DIRECTORY_CONTENT_VERSION: &[u8] = b"aster-forge-directory-v1";
11
12#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct MacosFileProviderItemVersion {
15 metadata: MetadataRevision,
16 content: ContentRevision,
17}
18
19impl MacosFileProviderItemVersion {
20 pub fn new(metadata: MetadataRevision, content: ContentRevision) -> Result<Self> {
26 validate_component(metadata.as_bytes(), "metadata version")?;
27 validate_component(content.as_bytes(), "content version")?;
28 Ok(Self { metadata, content })
29 }
30
31 pub fn directory(metadata: MetadataRevision) -> Result<Self> {
37 let content = ContentRevision::from_slice(DIRECTORY_CONTENT_VERSION).map_err(|_| {
38 MacosBridgeError::InvalidItemVersion {
39 reason: "directory content version could not be represented",
40 }
41 })?;
42 Self::new(metadata, content)
43 }
44
45 #[must_use]
47 pub const fn metadata(&self) -> &MetadataRevision {
48 &self.metadata
49 }
50
51 #[must_use]
53 pub const fn content(&self) -> &ContentRevision {
54 &self.content
55 }
56
57 #[must_use]
59 pub fn into_parts(self) -> (MetadataRevision, ContentRevision) {
60 (self.metadata, self.content)
61 }
62}
63
64fn validate_component(value: &[u8], _field: &'static str) -> Result<()> {
65 if value.len() > FILE_PROVIDER_ITEM_VERSION_COMPONENT_MAX_BYTES {
66 return Err(MacosBridgeError::InvalidItemVersion {
67 reason: "item version component exceeds the File Provider 128-byte limit",
68 });
69 }
70 Ok(())
71}