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
use std::collections::BTreeMap;

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct IEmbeddedLanguagesMap {
    #[serde(flatten)]
    pub map: BTreeMap<String, String>,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct TokenTypesContribution {
    #[serde(flatten)]
    pub map: BTreeMap<String, String>,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct TMGrammar {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    #[serde(alias = "scopeName")]
    pub scope_name: String,
    pub path: String,

    #[serde(alias = "injectTo")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub inject_to: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(alias = "embeddedLanguages")]
    pub embedded_languages: Option<BTreeMap<String, String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(alias = "tokenTypes")]
    pub token_types: Option<BTreeMap<String, String>>,
}

#[cfg(test)]
mod tests {
    use crate::tm_grammar::TMGrammar;

    #[test]
    fn should_serialise_language_map() {
        let code = "{
        \"scopeName\": \"text.html.basic\",
        \"path\": \"./syntaxes/html.tmLanguage.json\",
        \"embeddedLanguages\": {
          \"text.html\": \"html\",
          \"source.css\": \"css\",
          \"source.js\": \"javascript\",
          \"source.python\": \"python\",
          \"source.smarty\": \"smarty\"
        },
        \"tokenTypes\": {
          \"meta.tag string.quoted\": \"other\"
        }
      }";

        let grammar: TMGrammar = serde_json::from_str(&code).unwrap();
        assert_eq!("html", grammar.embedded_languages.unwrap()["text.html"]);
    }
}