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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use super::{err, Dictionary};
// #[cfg(feature = "embed")]
use rust_embed::EmbeddedFile;
use std::path::PathBuf;

/// Helper for setting `default_locale` configuration
pub struct DefaultLocale<T>(pub T);

pub trait ConfigPart {
    fn add_to(self, config: Config) -> Config;
}

impl<T> ConfigPart for DefaultLocale<T>
where
    T: Into<String>,
{
    fn add_to(self, config: Config) -> Config {
        config.with_default_locale(self.0)
    }
}

impl<T> ConfigPart for (T,)
where
    T: ConfigPart,
{
    fn add_to(self, config: Config) -> Config {
        self.0.add_to(config)
    }
}

impl<'a, T, U> ConfigPart for (T, U)
where
    T: ConfigPart,
    U: ConfigPart,
{
    fn add_to(self, opts: Config) -> Config {
        self.1.add_to(self.0.add_to(opts))
    }
}

impl<'a, T, U, V> ConfigPart for (T, U, V)
where
    T: ConfigPart,
    U: ConfigPart,
    U: ConfigPart,
    V: ConfigPart,
{
    fn add_to(self, opts: Config) -> Config {
        self.2.add_to(self.1.add_to(self.0.add_to(opts)))
    }
}

impl<'a, T, U, V, W> ConfigPart for (T, U, V, W)
where
    T: ConfigPart,
    U: ConfigPart,
    U: ConfigPart,
    V: ConfigPart,
    W: ConfigPart,
{
    fn add_to(self, opts: Config) -> Config {
        self.3
            .add_to(self.2.add_to(self.1.add_to(self.0.add_to(opts))))
    }
}

impl<T> From<T> for Config
where
    T: ConfigPart,
{
    fn from(t: T) -> Self {
        t.add_to(Self::default())
    }
}

pub struct EmbeddedMeta {
    pub file: EmbeddedFile,
    pub name: String,
}

impl Clone for EmbeddedMeta {
    fn clone(&self) -> Self {
        Self {
            file: EmbeddedFile {
                data: self.file.data.clone(),
                metadata: rust_embed::Metadata::__rust_embed_new(
                    self.file.metadata.sha256_hash(),
                    self.file.metadata.last_modified(),
                ),
            },
            name: self.name.clone(),
        }
    }
}

/// Configuration to build a `Dictionary`
#[derive(Clone)]
pub struct Config {
    embed_assets: Vec<EmbeddedMeta>,
    default_locale: Option<String>,
}

impl Default for Config {
    fn default() -> Self {
        Self::global()
    }
}

impl Config {
    pub(crate) fn global() -> Self {
        Self {
            default_locale: None,
            embed_assets: Vec::new(),
        }
    }

    /// Set the default locale.
    pub fn with_default_locale<I: Into<String>>(mut self, default_locale: I) -> Self {
        self.default_locale = Some(default_locale.into());
        self
    }

    pub fn with_embed<T: rust_embed::RustEmbed>(mut self) -> Self {
        T::iter().for_each(|filename| {
            self.embed_assets.push(EmbeddedMeta {
                file: T::get(filename.as_ref()).unwrap(),
                name: filename.to_string(),
            });
        });
        self
    }

    /// Build the `Dictionary` item.
    pub fn finish(self) -> err::Result<Dictionary> {
        let mut out = Dictionary::default();

        for asset in self.embed_assets {
            let path = PathBuf::from(asset.name);

            let locale = match path.file_stem().and_then(|x| x.to_str()) {
                Some(locale) => locale.to_string(),
                None => continue,
            };

            let value = match path.extension().and_then(|x| x.to_str()) {
                Some("json") => {
                    serde_json::from_reader::<_, serde_json::Value>(asset.file.data.as_ref())?
                }
                #[cfg(feature = "yaml")]
                Some("yml") => {
                    serde_yaml::from_reader::<_, serde_json::Value>(asset.file.data.as_ref())?
                }
                #[cfg(feature = "toml")]
                Some("toml") => toml::from_slice::<serde_json::Value>(asset.file.data.as_ref())?,
                _ => {
                    continue;
                }
            };

            out.inner.insert(locale, value);
        }

        if let Some(locale) = self.default_locale {
            out.default_locale = locale;
        }

        Ok(out)
    }
}