wot_td/builder/
human_readable_info.rs

1//! Human readable information and semantic tagging
2//!
3//! This module contains the logic shared across multiple builders for the respective
4//! Thing Description Vocabulary definitions.
5
6use alloc::{string::*, vec::Vec};
7
8use super::MultiLanguageBuilder;
9
10/// Human readable informations and semantic tagging
11#[derive(Debug, Default, Clone, PartialEq, Eq)]
12pub struct HumanReadableInfo {
13    /// JSON-LD @type
14    /// The meaning of each @type is given by the current JSON-LD @context.
15    pub(super) attype: Option<Vec<String>>,
16    /// Human readable title in the default language
17    pub(super) title: Option<String>,
18    /// Human redable title, multilanguage
19    pub(super) titles: Option<MultiLanguageBuilder<String>>,
20    /// Human readable description in the default language
21    pub(super) description: Option<String>,
22    /// Human readable description, multilanguage
23    pub(super) descriptions: Option<MultiLanguageBuilder<String>>,
24}
25
26/// Trait shared across builders dealing with the same information
27pub trait BuildableHumanReadableInfo {
28    /// Set JSON-LD @type
29    ///
30    /// It can be called as many times as needed to add multiple @types.
31    fn attype(self, value: impl Into<String>) -> Self;
32
33    /// Set the title
34    ///
35    /// Calling it multiple times overwrites the field.
36    fn title(self, value: impl Into<String>) -> Self;
37
38    /// Set the translations of the title
39    ///
40    /// Calling it multiple times overwrites the field.
41    ///
42    /// See [`ThingBuilder::titles`] for examples.
43    ///
44    /// [`ThingBuilder::titles`]: crate::builder::ThingBuilder::titles
45    fn titles<F>(self, f: F) -> Self
46    where
47        F: FnOnce(&mut MultiLanguageBuilder<String>) -> &mut MultiLanguageBuilder<String>;
48
49    /// Set the description
50    ///
51    /// Calling it multiple times overwrites the field.
52    fn description(self, value: impl Into<String>) -> Self;
53
54    /// Set the translations of the description
55    ///
56    /// Calling it multiple times overwrites the field.
57    ///
58    /// See [`ThingBuilder::titles`] for examples.
59    ///
60    /// [`ThingBuilder::titles`]: crate::builder::ThingBuilder::titles
61    fn descriptions<F>(self, f: F) -> Self
62    where
63        F: FnOnce(&mut MultiLanguageBuilder<String>) -> &mut MultiLanguageBuilder<String>;
64}
65
66impl BuildableHumanReadableInfo for HumanReadableInfo {
67    fn attype(mut self, value: impl Into<String>) -> Self {
68        self.attype
69            .get_or_insert_with(Default::default)
70            .push(value.into());
71        self
72    }
73
74    fn title(mut self, value: impl Into<String>) -> Self {
75        self.title = Some(value.into());
76        self
77    }
78
79    fn titles<F>(mut self, f: F) -> Self
80    where
81        F: FnOnce(&mut MultiLanguageBuilder<String>) -> &mut MultiLanguageBuilder<String>,
82    {
83        let mut builder = MultiLanguageBuilder::default();
84        f(&mut builder);
85        self.titles = Some(builder);
86        self
87    }
88
89    fn description(mut self, value: impl Into<String>) -> Self {
90        self.description = Some(value.into());
91        self
92    }
93
94    fn descriptions<F>(mut self, f: F) -> Self
95    where
96        F: FnOnce(&mut MultiLanguageBuilder<String>) -> &mut MultiLanguageBuilder<String>,
97    {
98        let mut builder = MultiLanguageBuilder::default();
99        f(&mut builder);
100        self.descriptions = Some(builder);
101        self
102    }
103}
104
105macro_rules! impl_delegate_buildable_hr_info {
106    ($( $ty:ident $( < $( $generic:ident $(: $bound:ident)? ),+ > )? on $($inner_path:ident).+ ),+ $(,)?) => {
107        $(
108            impl $(< $($generic $(: $bound)? ),+ >)? BuildableHumanReadableInfo for $ty $(< $($generic),+ >)?
109            {
110                #[inline]
111                fn attype(mut self, value: impl Into<String>) -> Self {
112                    self. $($inner_path).+ = self. $($inner_path).+ .attype(value);
113                    self
114                }
115
116                #[inline]
117                fn title(mut self, value: impl Into<String>) -> Self {
118                    self. $($inner_path).+ = self. $($inner_path).+ .title(value);
119                    self
120                }
121
122                #[inline]
123                fn titles<F>(mut self, f: F) -> Self
124                where
125                    F: FnOnce(&mut MultiLanguageBuilder<String>) -> &mut MultiLanguageBuilder<String>,
126                {
127                    self. $($inner_path).+ = self. $($inner_path).+ .titles(f);
128                    self
129                }
130
131                #[inline]
132                fn description(mut self, value: impl Into<String>) -> Self {
133                    self. $($inner_path).+ = self. $($inner_path).+ .description(value);
134                    self
135                }
136
137                #[inline]
138                fn descriptions<F>(mut self, f: F) -> Self
139                where
140                    F: FnOnce(&mut MultiLanguageBuilder<String>) -> &mut MultiLanguageBuilder<String>,
141                {
142                    self. $($inner_path).+ = self. $($inner_path).+ .descriptions(f);
143                    self
144                }
145            }
146        )+
147    };
148}
149
150pub(super) use impl_delegate_buildable_hr_info;