post_archiver/importer/
tag.rs

1use std::hash::Hash;
2
3use crate::{
4    manager::{PostArchiverConnection, PostArchiverManager},
5    PlatformId, TagId,
6};
7
8impl<T> PostArchiverManager<T>
9where
10    T: PostArchiverConnection,
11{
12    /// Import a tag into the archive.
13    ///
14    /// If the tag already exists, it returns the existing ID.
15    ///
16    /// # Errors
17    ///
18    /// Returns `rusqlite::Error` if there was an error accessing the database.
19    pub fn import_tag(&self, tag: UnsyncTag) -> Result<TagId, rusqlite::Error> {
20        let find_tag = (tag.name.as_str(), tag.platform);
21        match self.find_tag(&find_tag)? {
22            Some(id) => Ok(id),
23            None => self.add_tag(tag.name, tag.platform),
24        }
25    }
26
27    /// Import multiple tags into the archive.
28    ///
29    /// If a tag already exists, it returns the existing ID.
30    ///
31    /// # Errors
32    ///
33    /// Returns `rusqlite::Error` if there was an error accessing the database.
34    pub fn import_tags(
35        &self,
36        tags: impl IntoIterator<Item = UnsyncTag>,
37    ) -> Result<Vec<TagId>, rusqlite::Error> {
38        tags.into_iter()
39            .map(|tag| self.import_tag(tag))
40            .collect::<Result<Vec<TagId>, rusqlite::Error>>()
41    }
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Hash)]
45pub struct UnsyncTag {
46    pub name: String,
47    pub platform: Option<PlatformId>,
48}