Skip to main content

a2kit/lang/integer/
settings.rs

1//! Parse settings string sent by any client.
2//! 
3//! The server will check for specific keys that may affect its operation.
4//! These are then used by the various modules to set their own flags.
5
6use serde_json;
7use crate::DYNERR;
8use crate::lang::{update_json_bool,update_json_i64,update_json_vec,update_json_severity};
9use lsp_types::DiagnosticSeverity;
10
11#[derive(Clone)]
12pub struct Flag {
13    pub case_sensitive: Option<DiagnosticSeverity>,
14    pub undeclared_arrays: Option<DiagnosticSeverity>,
15    pub undefined_variables: Option<DiagnosticSeverity>,
16    pub bad_references: Option<DiagnosticSeverity>,
17    pub immediate_mode: Option<DiagnosticSeverity>
18}
19#[derive(Clone)]
20pub struct Warn {
21    pub length: i64
22}
23#[derive(Clone)]
24pub struct Hovers {
25    pub special_addresses: bool,
26    pub keywords: bool
27}
28#[derive(Clone)]
29pub struct Completions {
30    pub lower_case: bool
31}
32#[derive(Clone)]
33pub struct Detokenizer {
34    pub escapes: Vec<i64>,
35    pub max_lines: i64,
36    pub max_line_length: i64
37}
38#[derive(Clone)]
39pub struct Settings {
40    pub flag: Flag,
41    pub warn: Warn,
42    pub hovers: Hovers,
43    pub completions: Completions,
44    pub detokenizer: Detokenizer
45}
46
47impl Settings {
48    pub fn new() -> Self {
49        Self {
50            flag : Flag {
51                case_sensitive: None,
52                undeclared_arrays: Some(DiagnosticSeverity::WARNING),
53                undefined_variables: Some(DiagnosticSeverity::WARNING),
54                bad_references: Some(DiagnosticSeverity::ERROR),
55                immediate_mode: Some(DiagnosticSeverity::ERROR)
56            },
57            warn : Warn {
58                length: 150
59            },
60            hovers : Hovers {
61                special_addresses: true,
62                keywords: true
63            },
64            completions : Completions {
65                lower_case: true
66            },
67            detokenizer : Detokenizer {
68                escapes: vec![138,141],
69                max_lines: 5000,
70                max_line_length: 255
71            }
72        }
73    }
74}
75
76pub fn parse(json: &str) -> Result<Settings,DYNERR> {
77    let mut ans = Settings::new();
78    if let Ok(root) = serde_json::from_str::<serde_json::Value>(json) {
79        if let Some(obj) = root.as_object() {
80            for (key,val) in obj {
81                match key.as_str() {
82                    "flag" => {
83                        update_json_severity(val,"caseSensitive",&mut ans.flag.case_sensitive);
84                        update_json_severity(val,"undeclaredArrays",&mut ans.flag.undeclared_arrays);
85                        update_json_severity(val,"undefinedVariables",&mut ans.flag.undefined_variables);
86                        update_json_severity(val,"badReferences",&mut ans.flag.bad_references);
87                        update_json_severity(val,"immediateMode",&mut ans.flag.immediate_mode);
88                    },
89                    "warn" => {
90                        update_json_i64(val,"length",&mut ans.warn.length);
91                    }
92                    "hovers" => {
93                        update_json_bool(val,"specialAddresses",&mut ans.hovers.special_addresses);
94                        update_json_bool(val,"keywords",&mut ans.hovers.keywords);
95                    },
96                    "completions" => {
97                        update_json_bool(val,"lowerCase",&mut ans.completions.lower_case);
98                    },
99                    "detokenizer" => {
100                        update_json_i64(val, "maxLineLength", &mut ans.detokenizer.max_line_length);
101                        update_json_i64(val,"maxLines",&mut ans.detokenizer.max_lines);
102                        update_json_vec(val,"escapes",&mut ans.detokenizer.escapes);
103                    },
104                    _ => {}
105                }
106            }
107        }
108    }
109    Ok(ans)
110}