aboutsummaryrefslogtreecommitdiff
path: root/import/src/source_rank.rs
blob: 0ac399fce7516beb5a09828493e84941296599bf (plain)
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
/*
    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::{EMPTY, Object, Tag, TypedTag, ValueMarker},
    *,
};

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<'a, T: ValueMarker<'a> + ?Sized>(
        &'a self,
        is: ImportSource,
        key: TypedTag<T>,
        value: T::Inner,
    ) -> Box<Object>;
}
impl ObjectImportSourceExt for Object {
    fn insert_s<'a, T: ValueMarker<'a> + ?Sized>(
        &'a self,
        is: ImportSource,
        key: TypedTag<T>,
        value: T::Inner,
    ) -> Box<Object> {
        let ms = self.get(NO_METASOURCE).unwrap_or(EMPTY);
        let ms_key = TypedTag::<Tag>::new(key.0);

        if let Some(current_source) = ms.get(ms_key)
            && !is.ranks.compare(key.0, current_source, is.tag)
        {
            return self.to_owned();
        }

        self.insert(key, value)
            .update(NO_METASOURCE, |ms| ms.insert(ms_key, is.tag))
    }
}
impl Default for SourceRanks {
    fn default() -> Self {
        Self {
            list: [
                MSOURCE_EXPLICIT,
                MSOURCE_FILENAME_PATTERN,
                MSOURCE_INFOJSON,
                MSOURCE_TRAKT,
                MSOURCE_MUSICBRAINZ,
                MSOURCE_MEDIA,
                MSOURCE_TAGS,
                MSOURCE_TMDB,
                MSOURCE_IMAGE_ATT,
                MSOURCE_WIKIDATA,
                MSOURCE_VGMDB,
                MSOURCE_ACOUSTID,
                MSOURCE_OMDB,
            ]
            .to_vec(),
        }
    }
}
impl SourceRanks {
    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
    }
}