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
use std::{
    collections::BTreeSet,
    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::{message::Message, Error};

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

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

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

    fn validate(self) -> Result<Self, Error> {
        let mut result = vec![];
        for i in 0..self.langs.len() {
            for j in i + 1..self.langs.len() {
                let this = &self.langs[i];
                let that = &self.langs[j];
                let diff = this.diff(&that);
                if !diff.is_empty() {
                    let names: Vec<String> =
                        diff.into_iter().map(|msg| msg.name().to_string()).collect();
                    result.push((
                        this.language().to_string(),
                        that.language.to_string(),
                        names,
                    ));
                }
            }
        }
        if !result.is_empty() {
            Err(Error::MessageBundleValidationError {
                bundle: self.path.to_string_lossy().to_string(),
                mismatching_messages: result,
            })
        } else {
            Ok(self)
        }
    }

    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.langs
            .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: BTreeSet<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<BTreeSet<Message>, Error> {
        let resource = fluent_syntax::parser::parse(content)
            .map_err(|(_, errors)| Error::FluentParserError { errors })?;

        resource
            .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) -> BTreeSet<&Message> {
        self.messages.iter().collect()
    }
    pub fn diff<'a>(&'a self, other: &'a LanguageBundle) -> Vec<&'a Message> {
        self.messages.difference(&other.messages).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))
    }
}