Skip to main content

reinhardt_utils/storage/
file.rs

1//! File metadata and representation
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6/// Metadata about a stored file
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct FileMetadata {
9	/// The storage path of the file.
10	pub path: String,
11	/// The size of the file in bytes.
12	pub size: u64,
13	/// The MIME content type of the file, if known.
14	pub content_type: Option<String>,
15	/// The timestamp when the file was created.
16	pub created_at: DateTime<Utc>,
17	/// The timestamp when the file was last modified.
18	pub modified_at: DateTime<Utc>,
19	/// An optional checksum (e.g., MD5, SHA-256) for integrity verification.
20	pub checksum: Option<String>,
21}
22
23impl FileMetadata {
24	/// Create new file metadata
25	///
26	/// # Examples
27	///
28	/// ```
29	/// use reinhardt_utils::storage::FileMetadata;
30	///
31	/// let metadata = FileMetadata::new("test.txt".to_string(), 1024);
32	/// assert_eq!(metadata.path, "test.txt");
33	/// assert_eq!(metadata.size, 1024);
34	/// assert!(metadata.checksum.is_none());
35	/// ```
36	pub fn new(path: String, size: u64) -> Self {
37		let now = Utc::now();
38		Self {
39			path,
40			size,
41			content_type: None,
42			created_at: now,
43			modified_at: now,
44			checksum: None,
45		}
46	}
47	/// Set content type for the file
48	///
49	/// # Examples
50	///
51	/// ```
52	/// use reinhardt_utils::storage::FileMetadata;
53	///
54	/// let metadata = FileMetadata::new("test.txt".to_string(), 1024)
55	///     .with_content_type("text/plain".to_string());
56	/// assert_eq!(metadata.content_type, Some("text/plain".to_string()));
57	/// ```
58	pub fn with_content_type(mut self, content_type: String) -> Self {
59		self.content_type = Some(content_type);
60		self
61	}
62	/// Set checksum for the file
63	///
64	/// # Examples
65	///
66	/// ```
67	/// use reinhardt_utils::storage::FileMetadata;
68	///
69	/// let metadata = FileMetadata::new("test.txt".to_string(), 1024)
70	///     .with_checksum("abc123".to_string());
71	/// assert_eq!(metadata.checksum, Some("abc123".to_string()));
72	/// ```
73	pub fn with_checksum(mut self, checksum: String) -> Self {
74		self.checksum = Some(checksum);
75		self
76	}
77}
78
79/// Represents a stored file
80#[derive(Debug)]
81pub struct StoredFile {
82	/// Metadata describing the stored file.
83	pub metadata: FileMetadata,
84	/// The raw file content.
85	pub content: Vec<u8>,
86}
87
88impl StoredFile {
89	/// Create a new stored file with metadata and content
90	///
91	/// # Examples
92	///
93	/// ```
94	/// use reinhardt_utils::storage::{FileMetadata, StoredFile};
95	///
96	/// let metadata = FileMetadata::new("test.txt".to_string(), 5);
97	/// let file = StoredFile::new(metadata, b"hello".to_vec());
98	/// assert_eq!(file.content, b"hello");
99	/// assert_eq!(file.size(), 5);
100	/// ```
101	pub fn new(metadata: FileMetadata, content: Vec<u8>) -> Self {
102		Self { metadata, content }
103	}
104	/// Get the size of the file content in bytes
105	///
106	/// # Examples
107	///
108	/// ```
109	/// use reinhardt_utils::storage::{FileMetadata, StoredFile};
110	///
111	/// let metadata = FileMetadata::new("test.txt".to_string(), 100);
112	/// let file = StoredFile::new(metadata, b"hello world".to_vec());
113	/// assert_eq!(file.size(), 11);
114	/// ```
115	pub fn size(&self) -> u64 {
116		self.content.len() as u64
117	}
118}