Skip to main content

rsynko_media/
observation.rs

1use crate::MediaSorts;
2use alux_ext::ext;
3use ambassador::delegatable_trait;
4
5/// Provides the carriers and constructors for open metadata.
6#[delegatable_trait]
7pub trait MetadataAlg: MediaSorts {
8    /// Defines the neutral metadata record.
9    fn empty_metadata(&self) -> Self::Metadata;
10    /// Defines unavailable metadata.
11    fn null_metadata(&self) -> Self::Value;
12    /// Defines a boolean observation.
13    fn boolean_metadata(&self, value: bool) -> Self::Value;
14    /// Defines an integral observation.
15    fn integer_metadata(&self, value: i64) -> Self::Value;
16    /// Defines a non-integral observation.
17    fn float_metadata(&self, value: f64) -> Self::Value;
18    /// Defines textual metadata.
19    fn string_metadata(&self, value: impl Into<String>) -> Self::Value;
20    /// Defines an ordered metadata sequence.
21    fn list_metadata(&self, values: impl IntoIterator<Item = Self::Value>) -> Self::Value;
22    /// Defines a nested metadata record.
23    fn record_metadata(&self, record: Self::Metadata) -> Self::Value;
24    /// Defines a record from fields, with later equal keys replacing earlier fields.
25    fn metadata(&self, fields: impl IntoIterator<Item = (String, Self::Value)>) -> Self::Metadata;
26}
27
28/// Specifies reading back what one metadata record states.
29///
30/// A record is open: it holds whatever an extractor observed. Reading one back is asking a
31/// question of it, and a field stating something else than the question asked answers nothing.
32#[delegatable_trait]
33pub trait MetadataViewAlg: MediaSorts {
34    /// Observes the text one field states, when it states text.
35    fn metadata_text<'a>(&self, metadata: &'a Self::Metadata, key: &str) -> Option<&'a str>;
36    /// Observes the whole number one field states, when it states one.
37    fn metadata_number(&self, metadata: &Self::Metadata, key: &str) -> Option<i64>;
38}
39
40/// Derives concise metadata records from primitive construction.
41#[ext(name = MetadataExt)]
42pub impl<This> This
43where
44    This: MetadataAlg,
45{
46    /// Defines a record containing one textual field.
47    fn text_metadata(&self, key: impl Into<String>, value: impl Into<String>) -> This::Metadata {
48        self.metadata([(key.into(), self.string_metadata(value))])
49    }
50}