post_archiver/importer/
collection.rs1use std::hash::Hash;
2
3use crate::{
4 manager::{PostArchiverConnection, PostArchiverManager},
5 CollectionId,
6};
7
8impl<T> PostArchiverManager<T>
9where
10 T: PostArchiverConnection,
11{
12 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, ),
33 }
34 }
35
36 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#[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}