ssi_vc/v2/syntax/
language.rs

1use serde::{Deserialize, Serialize};
2use ssi_json_ld::LangString;
3
4use crate::v2::data_model;
5
6/// International string.
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
8#[serde(untagged)]
9pub enum InternationalString {
10    String(String),
11    LanguageValue(LangString),
12    LanguageMap(Vec<LangString>),
13}
14
15impl data_model::AnyInternationalString for InternationalString {
16    fn default_value(&self) -> Option<data_model::LanguageValueRef> {
17        match self {
18            Self::String(s) => s.default_value(),
19            Self::LanguageValue(v) => v.default_value(),
20            Self::LanguageMap(_) => None,
21        }
22    }
23
24    fn get_language(
25        &self,
26        lang: &ssi_json_ld::syntax::LangTag,
27    ) -> Option<data_model::LanguageValueRef> {
28        match self {
29            Self::String(_) => None,
30            Self::LanguageValue(v) => {
31                if let Some(tag) = v.language() {
32                    if tag.as_str() == lang.as_str() {
33                        return Some(v.into());
34                    }
35                }
36
37                None
38            }
39            Self::LanguageMap(values) => values.iter().find_map(|v| {
40                if let Some(tag) = v.language() {
41                    if tag.as_str() == lang.as_str() {
42                        return Some(v.into());
43                    }
44                }
45
46                None
47            }),
48        }
49    }
50}
51
52impl From<String> for InternationalString {
53    fn from(value: String) -> Self {
54        Self::String(value)
55    }
56}
57
58impl From<LangString> for InternationalString {
59    fn from(value: LangString) -> Self {
60        Self::LanguageValue(value)
61    }
62}