Skip to main content

capsule_core/sidecar/
smart_album.rs

1//! Smart-album label search (SSoT: [Organization — Labels] and
2//! [Organization — Smart-Album Definition Schema]).
3//!
4//! A smart-album `contains` term over `tags_user` matches a label at **any level attached to
5//! the asset**: the asset's own `tags_user`, the labels of each of its live regions, its stack's
6//! [`StackDocV1`] and its container album's [`AlbumDocV1`]. A deleted region (a stamped `None`
7//! in its register) contributes nothing, and neither does a stack record the asset is not a
8//! current member of.
9//!
10//! This module evaluates that one term. The rest of the predicate grammar (`All`/`Any`/`Not`,
11//! the other fields and operators) is not built yet; when it is, its `tags_user` `contains`
12//! term delegates here so every level stays covered by one function.
13//!
14//! Evaluation is a pure function of its inputs: the operand is compared byte-for-byte against
15//! stored labels, which are already NFC-normalized and trimmed by
16//! [`normalize_label`](super::normalize_label), so matching is case-sensitive like the labels
17//! themselves.
18//!
19//! [Organization — Labels]: https://docs/design/organization/#labels
20//! [Organization — Smart-Album Definition Schema]: https://docs/design/organization/#smart-album-definition-schema
21
22use super::album_doc::AlbumDocV1;
23use super::region::live_region;
24use super::sidecar_v1::SidecarV1;
25use super::stack_doc::StackDocV1;
26use crate::metadata::crdt::OrSet;
27
28/// The level a matched label sits at.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum LabelLevel {
31    /// The asset's own `tags_user`.
32    Asset,
33    /// A live region of the asset.
34    Region,
35    /// The stack the asset is currently a member of.
36    Stack,
37    /// The asset's container album.
38    Album,
39}
40
41/// Everything a `tags_user` term can see for one asset: its sidecar plus the records of the
42/// stack and container album it belongs to, where the caller holds them.
43#[derive(Debug, Clone, Copy)]
44pub struct LabelContext<'a> {
45    /// The asset's sidecar.
46    pub sidecar: &'a SidecarV1,
47    /// The stack record for the stack the asset belongs to. Ignored unless the sidecar's
48    /// current `stack_membership` names this record's `stack_id`.
49    pub stack: Option<&'a StackDocV1>,
50    /// The record of the asset's container album.
51    pub album: Option<&'a AlbumDocV1>,
52}
53
54fn has(set: &OrSet<String>, label: &str) -> bool {
55    set.value().iter().any(|l| l == label)
56}
57
58impl LabelContext<'_> {
59    /// The stack record, when the asset is a current member of it.
60    fn member_stack(&self) -> Option<&StackDocV1> {
61        let stack = self.stack?;
62        let membership = self.sidecar.stack_membership.get()?.as_ref()?;
63        (membership.stack_id == stack.stack_id).then_some(stack)
64    }
65
66    /// The first level, in [`LabelLevel`] order, at which `label` is attached to the asset, or
67    /// `None` when no level carries it.
68    pub fn level_of(&self, label: &str) -> Option<LabelLevel> {
69        let level = if has(&self.sidecar.tags_user, label) {
70            Some(LabelLevel::Asset)
71        } else if self
72            .sidecar
73            .regions
74            .values()
75            .filter_map(live_region)
76            .any(|region| has(&region.labels, label))
77        {
78            Some(LabelLevel::Region)
79        } else if self.member_stack().is_some_and(|s| has(&s.labels, label)) {
80            Some(LabelLevel::Stack)
81        } else if self.album.is_some_and(|a| has(&a.labels, label)) {
82            Some(LabelLevel::Album)
83        } else {
84            None
85        };
86        tracing::trace!(asset = %self.sidecar.uuid, ?level, "smart-album tags_user lookup");
87        level
88    }
89
90    /// Whether a `tags_user` `contains` term with operand `label` matches this asset.
91    pub fn tags_user_contains(&self, label: &str) -> bool {
92        self.level_of(label).is_some()
93    }
94}
95
96#[cfg(test)]
97mod tests;