post_archiver/importer/
collection.rs

1use std::hash::Hash;
2
3use crate::{
4    manager::{PostArchiverConnection, PostArchiverManager},
5    CollectionId,
6};
7
8impl<T> PostArchiverManager<T>
9where
10    T: PostArchiverConnection,
11{
12    /// Import a collection into the archive.
13    ///
14    /// If the collection already exists (by source), it updates its name and returns the existing ID.
15    ///
16    /// # Errors
17    ///
18    /// Returns `rusqlite::Error` if there was an error accessing the database.
19    pub fn import_collection(
20        &self,
21        collection: UnsyncCollection,
22    ) -> Result<CollectionId, rusqlite::Error> {
23        match self.find_collection(&collection.source)? {
24            Some(id) => {
25                self.set_collection_name(id, collection.name)?;
26                Ok(id)
27            }
28            None => self.add_collection(
29                collection.name,
30                Some(collection.source),
31                None, // No thumbnail for unsynced collections
32            ),
33        }
34    }
35
36    /// Import multiple collections into the archive.
37    ///
38    /// This method takes an iterator of `UnsyncCollection` and imports each one.
39    ///
40    /// # Errors
41    ///
42    /// Returns `rusqlite::Error` if there was an error accessing the database.
43    pub fn import_collections(
44        &self,
45        collections: impl IntoIterator<Item = UnsyncCollection>,
46    ) -> Result<Vec<CollectionId>, rusqlite::Error> {
47        collections
48            .into_iter()
49            .map(|collection| self.import_collection(collection))
50            .collect::<Result<Vec<CollectionId>, rusqlite::Error>>()
51    }
52}
53
54/// Represents a file metadata that is not yet synced to the database.
55#[derive(Debug, Clone, PartialEq, Eq, Hash)]
56pub struct UnsyncCollection {
57    pub name: String,
58    pub source: String,
59}
60
61impl UnsyncCollection {
62    pub fn new(name: String, source: String) -> Self {
63        Self { name, source }
64    }
65    pub fn name(mut self, name: String) -> Self {
66        self.name = name;
67        self
68    }
69    pub fn source(mut self, source: String) -> Self {
70        self.source = source;
71        self
72    }
73}