diff options
Diffstat (limited to 'import/src/source_rank.rs')
| -rw-r--r-- | import/src/source_rank.rs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/import/src/source_rank.rs b/import/src/source_rank.rs new file mode 100644 index 0000000..28ab4f7 --- /dev/null +++ b/import/src/source_rank.rs @@ -0,0 +1,85 @@ +/* + This file is part of jellything (https://codeberg.org/metamuffin/jellything) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2026 metamuffin <metamuffin.org> +*/ + +use jellycommon::{ + jellyobject::{Object, ObjectBuffer, Tag, TypedTag, ValueStore}, + *, +}; +use std::marker::PhantomData; + +pub struct SourceRanks { + list: Vec<Tag>, +} + +#[derive(Clone, Copy)] +pub struct ImportSource<'a> { + pub tag: Tag, + pub ranks: &'a SourceRanks, +} + +pub trait ObjectImportSourceExt { + fn insert_s<T: ValueStore>(&self, is: ImportSource, key: TypedTag<T>, value: T) + -> ObjectBuffer; +} +impl<'a> ObjectImportSourceExt for Object<'a> { + fn insert_s<T: ValueStore>( + &self, + is: ImportSource, + key: TypedTag<T>, + value: T, + ) -> ObjectBuffer { + let ms = self.get(NO_METASOURCE).unwrap_or_default(); + let ms_key = TypedTag::<Tag>(key.0, PhantomData); + + if let Some(current_source) = ms.get(ms_key) { + if !is.ranks.compare(key.0, current_source, is.tag) { + return self.dump(); + } + } + + self.insert(key, value) + .as_object() + .update(NO_METASOURCE, |ms| ms.insert(ms_key, is.tag)) + } +} + +impl SourceRanks { + pub fn new() -> Self { + Self { + list: [ + MSOURCE_EXPLICIT, + MSOURCE_TRAKT, + MSOURCE_MUSICBRAINZ, + MSOURCE_MEDIA, + MSOURCE_TAGS, + MSOURCE_IMAGE_ATT, + MSOURCE_TMDB, + MSOURCE_WIKIDATA, + MSOURCE_VGMDB, + MSOURCE_ACOUSTID, + MSOURCE_INFOJSON, + MSOURCE_OMDB, + ] + .to_vec(), + } + } + pub fn compare(&self, key: Tag, old: Tag, new: Tag) -> bool { + let _ = key; + + let old_index = self + .list + .iter() + .position(|e| *e == old) + .unwrap_or(usize::MAX); + let new_index = self + .list + .iter() + .position(|e| *e == new) + .unwrap_or(usize::MAX); + + new_index <= old_index + } +} |