paladin-ai-core 0.5.1

Pure domain types for the Paladin framework — zero infrastructure dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::base::entity::node::Node;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use std::io::Read;
use thiserror::Error;
use url::Url;
use uuid::Uuid;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ContentItem {
    pub node: Node<ContentData>,
}

impl Hash for ContentItem {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.node.hash(state);
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ContentData {
    pub content: ContentType,
    pub url: Option<Url>,
    pub hash: Option<String>,
    pub source_id: Option<String>,
    pub source_url: Option<Url>,
    pub description: Option<String>,
    pub tags: Option<Vec<String>>,
    pub source: Option<String>,
    pub author: Option<String>,
    pub pub_date: Option<DateTime<Utc>>,
    pub mod_date: Option<DateTime<Utc>>,
}

impl ContentItem {
    pub fn new(content: ContentType) -> Result<Self, ContentItemError> {
        let hash = match content.path() {
            Some(path) => Some(generate_file_hash(path)?),
            None => None,
        };

        let content_data = ContentData {
            content,
            url: None,
            hash,
            source_id: None,
            source_url: None,
            description: None,
            tags: None,
            source: None,
            author: None,
            pub_date: None,
            mod_date: None,
        };

        let node = Node::new(content_data, None);

        Ok(ContentItem { node })
    }

    pub fn new_with_title(content: ContentType, title: String) -> Result<Self, ContentItemError> {
        let hash = match content.path() {
            Some(path) => Some(generate_file_hash(path)?),
            None => None,
        };

        let content_data = ContentData {
            content,
            url: None,
            hash,
            source_id: None,
            source_url: None,
            description: None,
            tags: None,
            source: None,
            author: None,
            pub_date: None,
            mod_date: None,
        };

        let node = Node::new(content_data, Some(title));

        Ok(ContentItem { node })
    }

    pub fn update_content(&mut self, new_content: ContentType) -> Result<(), ContentItemError> {
        // Update hash if content has a path
        let new_hash = match new_content.path() {
            Some(path) => Some(generate_file_hash(path)?),
            None => None,
        };

        // Update the content data
        self.node.node.content = new_content;
        self.node.node.hash = new_hash;
        self.node.node.mod_date = Some(Utc::now());

        // Update the node's modified timestamp
        self.node.modified = Utc::now();

        Ok(())
    }

    pub fn set_title(&mut self, title: Option<String>) {
        self.node.name = title;
        self.node.modified = Utc::now();
    }

    pub fn set_description(&mut self, description: Option<String>) {
        self.node.node.description = description;
        self.node.modified = Utc::now();
    }

    pub fn set_tags(&mut self, tags: Option<Vec<String>>) {
        self.node.node.tags = tags;
        self.node.modified = Utc::now();
    }

    pub fn set_author(&mut self, author: Option<String>) {
        self.node.node.author = author;
        self.node.modified = Utc::now();
    }

    pub fn set_source(&mut self, source: Option<String>) {
        self.node.node.source = source;
        self.node.modified = Utc::now();
    }

    pub fn set_url(&mut self, url: Option<Url>) {
        self.node.node.url = url;
        self.node.modified = Utc::now();
    }

    pub fn set_source_url(&mut self, source_url: Option<Url>) {
        self.node.node.source_url = source_url;
        self.node.modified = Utc::now();
    }

    pub fn set_source_id(&mut self, source_id: Option<String>) {
        self.node.node.source_id = source_id;
        self.node.modified = Utc::now();
    }

    pub fn set_publication_date(&mut self, pub_date: Option<DateTime<Utc>>) {
        self.node.node.pub_date = pub_date;
        self.node.modified = Utc::now();
    }

    pub fn disable_versioning(&mut self) {
        self.node.version = false;
    }

    pub fn enable_versioning(&mut self) {
        self.node.version = true;
    }

    // Convenience getters
    pub fn uuid(&self) -> Uuid {
        self.node.uuid
    }

    pub fn created(&self) -> DateTime<Utc> {
        self.node.created
    }

    pub fn modified(&self) -> DateTime<Utc> {
        self.node.modified
    }

    pub fn title(&self) -> Option<&String> {
        self.node.name.as_ref()
    }

    pub fn content(&self) -> &ContentType {
        &self.node.node.content
    }

    pub fn content_mut(&mut self) -> &mut ContentType {
        &mut self.node.node.content
    }

    pub fn description(&self) -> Option<&String> {
        self.node.node.description.as_ref()
    }

    pub fn tags(&self) -> Option<&Vec<String>> {
        self.node.node.tags.as_ref()
    }

    pub fn author(&self) -> Option<&String> {
        self.node.node.author.as_ref()
    }

    pub fn source(&self) -> Option<&String> {
        self.node.node.source.as_ref()
    }

    pub fn url(&self) -> Option<&Url> {
        self.node.node.url.as_ref()
    }

    pub fn source_url(&self) -> Option<&Url> {
        self.node.node.source_url.as_ref()
    }

    pub fn hash(&self) -> Option<&String> {
        self.node.node.hash.as_ref()
    }

    pub fn publication_date(&self) -> Option<DateTime<Utc>> {
        self.node.node.pub_date
    }

    pub fn modification_date(&self) -> Option<DateTime<Utc>> {
        self.node.node.mod_date
    }

    pub fn versioning_enabled(&self) -> bool {
        self.node.version
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ContentType {
    Text(TextContent),
    Video(VideoContent),
    Audio(AudioContent),
    Image(ImageContent),
}

impl ContentType {
    pub fn path(&self) -> Option<&String> {
        match self {
            ContentType::Text(content) => content.path.as_ref(),
            ContentType::Video(content) => content.path.as_ref(),
            ContentType::Audio(content) => content.path.as_ref(),
            ContentType::Image(content) => content.path.as_ref(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct TextContent {
    pub path: Option<String>,
    pub content: Option<String>,
    pub filesize: u64,
}

impl TextContent {
    pub fn new(path: Option<String>, content: Option<String>) -> Result<Self, ContentItemError> {
        let filesize = match &path {
            Some(p) => {
                let metadata = std::fs::metadata(p).map_err(|_| ContentItemError::FileNotFound)?;
                let size = metadata.len();
                check_filesize(size)?;
                size
            }
            None => content.as_ref().map(|c| c.len() as u64).unwrap_or(0),
        };

        Ok(TextContent {
            path,
            content,
            filesize,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct VideoContent {
    pub path: Option<String>,
    pub duration: u64,
    pub filesize: u64,
}

impl VideoContent {
    pub fn new(path: Option<String>, duration: u64) -> Result<Self, ContentItemError> {
        let filesize = match &path {
            Some(p) => {
                let metadata = std::fs::metadata(p).map_err(|_| ContentItemError::FileNotFound)?;
                let size = metadata.len();
                check_filesize(size)?;
                size
            }
            None => 0,
        };

        Ok(VideoContent {
            path,
            duration,
            filesize,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct AudioContent {
    pub path: Option<String>,
    pub duration: u64,
    pub filesize: u64,
}

impl AudioContent {
    pub fn new(path: Option<String>, duration: u64) -> Result<Self, ContentItemError> {
        let filesize = match &path {
            Some(p) => {
                let metadata = std::fs::metadata(p).map_err(|_| ContentItemError::FileNotFound)?;
                let size = metadata.len();
                check_filesize(size)?;
                size
            }
            None => 0,
        };

        Ok(AudioContent {
            path,
            duration,
            filesize,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ImageContent {
    pub path: Option<String>,
    pub resolution: (u32, u32),
    pub filesize: u64,
}

impl ImageContent {
    pub fn new(path: Option<String>, resolution: (u32, u32)) -> Result<Self, ContentItemError> {
        let filesize = match &path {
            Some(p) => {
                let metadata = std::fs::metadata(p).map_err(|_| ContentItemError::FileNotFound)?;
                let size = metadata.len();
                check_filesize(size)?;
                size
            }
            None => 0,
        };

        Ok(ImageContent {
            path,
            resolution,
            filesize,
        })
    }
}

#[derive(Debug, Clone, Error)]
pub enum ContentItemError {
    #[error("File not found")]
    FileNotFound,
    #[error("Failed to read file")]
    FileReadError,
    #[error("File size exceeds the maximum limit")]
    FileSizeLimitExceeded,
    #[error("Content hash already exists in the system with UUID: {0}")]
    HashAlreadyExists(Uuid),
    #[error("No content item exists for that hash")]
    NoContentForHash,
}

fn generate_file_hash(path: &str) -> Result<String, ContentItemError> {
    let mut file = std::fs::File::open(path).map_err(|_| ContentItemError::FileNotFound)?;
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer)
        .map_err(|_| ContentItemError::FileReadError)?;
    let hash = murmur3::murmur3_x64_128(&mut std::io::Cursor::new(&buffer), 0)
        .map_err(|_| ContentItemError::FileReadError)?;
    Ok(format!("{:x}", hash))
}

fn check_filesize(filesize: u64) -> Result<(), ContentItemError> {
    let max_file_size = 100 * 1024 * 1024; // 100MB
    if filesize > max_file_size {
        Err(ContentItemError::FileSizeLimitExceeded)
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_content_item_new() {
        let text_content = TextContent::new(None, Some("test content".to_string())).unwrap();
        let content_type = ContentType::Text(text_content);

        let content_item = ContentItem::new(content_type.clone()).unwrap();

        assert_eq!(content_item.content(), &content_type);
        assert!(content_item.uuid() != Uuid::nil());
        assert!(content_item.created() <= Utc::now());
        assert_eq!(content_item.created(), content_item.modified());
        assert_eq!(content_item.url(), None);
        assert_eq!(content_item.hash(), None);
        assert!(content_item.versioning_enabled());
    }

    #[test]
    fn test_content_item_hash() {
        let text_content1 = TextContent::new(None, Some("content 1".to_string())).unwrap();
        let text_content2 = TextContent::new(None, Some("content 2".to_string())).unwrap();

        let item1 = ContentItem::new(ContentType::Text(text_content1)).unwrap();
        let item2 = ContentItem::new(ContentType::Text(text_content2)).unwrap();

        let mut hasher1 = std::collections::hash_map::DefaultHasher::new();
        let mut hasher2 = std::collections::hash_map::DefaultHasher::new();

        Hash::hash(&item1, &mut hasher1);
        Hash::hash(&item2, &mut hasher2);

        // Different content should produce different hashes
        assert_ne!(hasher1.finish(), hasher2.finish());
    }

    #[test]
    fn test_content_item_serialization() -> Result<(), Box<dyn std::error::Error>> {
        let text_content = TextContent::new(None, Some("serialize test".to_string()))?;
        let content_item =
            ContentItem::new_with_title(ContentType::Text(text_content), "Test Item".to_string())?;

        // Test serialization
        let serialized = serde_json::to_string(&content_item)?;
        assert!(!serialized.is_empty());

        // Test deserialization
        let deserialized: ContentItem = serde_json::from_str(&serialized)?;
        assert_eq!(content_item, deserialized);

        Ok(())
    }

    // ... rest of existing tests
}