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
170
171
172
173
174
175
176
177
178
179
180
use std::{
    collections::{HashMap, HashSet},
    path::{Path, PathBuf},
};

use convert_case::{Case, Casing};
use fluent_syntax::ast;
use proc_macro2::Literal;
use quote::format_ident;
use syn::Ident;

use crate::{
    error::MessageValidationErrorEntry,
    message::{Message, NormalizedMessage},
    Error,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MessageBundle {
    pub name: String,
    pub path: PathBuf,
    pub language_bundles: Vec<LanguageBundle>,
}

impl MessageBundle {
    pub fn create(
        name: &str,
        path: impl AsRef<Path>,
        language_resource: Vec<(String, String)>,
    ) -> Result<Self, Error> {
        let bundles: Vec<LanguageBundle> = language_resource
            .into_iter()
            .map(|(language, resource)| LanguageBundle::create(language, resource))
            .collect::<Result<Vec<LanguageBundle>, Error>>()?;

        Self {
            name: name.to_string(),
            path: path.as_ref().to_path_buf(),
            language_bundles: bundles,
        }
        .validate()
    }

    fn validate(self) -> Result<Self, Error> {
        let all_langs = self
            .language_bundles
            .iter()
            .map(|bundle| bundle.language.as_str())
            .collect::<HashSet<&str>>();

        let validation_errors = self
            .language_bundles
            .iter()
            .flat_map(|bundle| {
                bundle
                    .messages()
                    .into_iter()
                    .map(|msg| (bundle.language.as_str(), msg.normalize()))
            })
            .fold(
                HashMap::<NormalizedMessage, HashSet<&str>>::new(),
                |mut acc, (lang, msg)| {
                    acc.entry(msg).or_insert_with(HashSet::new).insert(lang);
                    acc
                },
            )
            .into_iter()
            .filter_map(|(message, langs)| {
                if langs.len() != self.language_bundles.len() {
                    let undefined = all_langs
                        .difference(&langs)
                        .map(|lang| lang.to_string())
                        .collect();
                    let defined = langs.into_iter().map(String::from).collect();
                    Some(MessageValidationErrorEntry {
                        message,
                        defined_in_languages: defined,
                        undefined_in_languages: undefined,
                    })
                } else {
                    None
                }
            })
            .collect::<Vec<MessageValidationErrorEntry>>();

        if !validation_errors.is_empty() {
            Err(Error::MessageBundleValidationError {
                bundle: self.name,
                entries: validation_errors,
                path: self.path.to_string_lossy().to_string(),
            })
        } else {
            Ok(self)
        }
    }

    pub fn get_language_bundle(&self, lang: &str) -> Option<&LanguageBundle> {
        self.language_bundles
            .iter()
            .find(|bundle| bundle.language() == lang)
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn name_ident(&self) -> Ident {
        format_ident!("{}", self.name.to_case(Case::Snake))
    }

    pub fn language_literals(&self) -> Vec<Literal> {
        self.language_bundles
            .iter()
            .map(|lang| Literal::string(lang.language()))
            .collect()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LanguageBundle {
    pub(crate) language: String,
    pub(crate) resource: String,
    pub(crate) messages: Vec<Message>,
}

impl LanguageBundle {
    pub fn create(language: String, resource: String) -> Result<Self, Error> {
        let messages = Self::parse(&resource)?;
        Ok(Self {
            language,
            resource,
            messages,
        })
    }

    fn parse(content: &str) -> Result<Vec<Message>, Error> {
        let ast = fluent_syntax::parser::parse(content)
            .map_err(|(_, errors)| Error::FluentParserError { errors })?;

        ast.body
            .iter()
            .filter_map(|entry| {
                if let ast::Entry::Message(message) = entry {
                    Some(message)
                } else {
                    None
                }
            })
            .map(|message| Message::parse(message))
            .collect()
    }

    pub fn messages(&self) -> Vec<&Message> {
        self.messages.iter().collect()
    }

    pub fn language(&self) -> &str {
        &self.language
    }

    pub fn resource(&self) -> &str {
        &self.resource
    }

    pub fn resource_literal(&self) -> Literal {
        Literal::string(self.resource())
    }

    pub fn static_bundle_ident(&self) -> Ident {
        format_ident!("{}_BUNDLE", self.language.to_case(Case::ScreamingSnake))
    }

    pub fn static_resource_ident(&self) -> Ident {
        format_ident!("{}_RESOURCE", self.language.to_case(Case::ScreamingSnake))
    }
}