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
181
182
183
184
185
186
187
188
189
use crate::fs::{self, FsError};
use json_comments::StripComments;
use miette::Diagnostic;
use once_cell::sync::Lazy;
use regex::Regex;
use serde::de::DeserializeOwned;
use serde::Serialize;
use starbase_styles::{Style, Stylize};
use std::path::Path;
use std::{io::Read, path::PathBuf};
use thiserror::Error;
use tracing::trace;

pub use serde_json::{
    from_str, from_value, json, to_string, to_string_pretty, to_value, Map as JsonMap,
    Number as JsonNumber, Value as JsonValue,
};

#[derive(Error, Diagnostic, Debug)]
pub enum JsonError {
    #[diagnostic(transparent)]
    #[error(transparent)]
    Fs(#[from] FsError),

    #[diagnostic(code(json::parse_file))]
    #[error("Failed to parse JSON file {}.", .path.style(Style::Path))]
    ReadFile {
        path: PathBuf,
        #[source]
        error: serde_json::Error,
    },

    #[diagnostic(code(json::stringify))]
    #[error("Failed to stringify JSON.")]
    Stringify {
        #[source]
        error: serde_json::Error,
    },

    #[diagnostic(code(json::stringify_file))]
    #[error("Failed to stringify JSON for file {}.", .path.style(Style::Path))]
    StringifyFile {
        path: PathBuf,
        #[source]
        error: serde_json::Error,
    },
}

static CLEAN_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r",(?P<valid>\s*})").unwrap());

/// Clean a JSON string by removing comments and trailing commas.
#[inline]
#[track_caller]
pub fn clean<D: AsRef<str>>(json: D) -> String {
    let json = json.as_ref();

    // Remove comments
    let mut stripped = String::with_capacity(json.len());

    StripComments::new(json.as_bytes())
        .read_to_string(&mut stripped)
        .unwrap();

    // Remove trailing commas
    CLEAN_REGEX.replace_all(&stripped, "$valid").to_string()
}

/// Recursively merge [`JsonValue`] objects, with values from next overwriting previous.
#[inline]
pub fn merge(prev: &JsonValue, next: &JsonValue) -> JsonValue {
    match (prev, next) {
        (JsonValue::Object(prev_object), JsonValue::Object(next_object)) => {
            let mut object = prev_object.clone();

            for (key, value) in next_object.iter() {
                if let Some(prev_value) = prev_object.get(key) {
                    object.insert(key.to_owned(), merge(prev_value, value));
                } else {
                    object.insert(key.to_owned(), value.to_owned());
                }
            }

            JsonValue::Object(object)
        }
        _ => next.to_owned(),
    }
}

/// Read a file at the provided path and deserialize into the required type.
/// The path must already exist.
#[inline]
pub fn read_file<P, D>(path: P) -> Result<D, JsonError>
where
    P: AsRef<Path>,
    D: DeserializeOwned,
{
    let path = path.as_ref();
    let contents = read_to_string(path)?;

    trace!(file = ?path, "Parsing JSON");

    serde_json::from_str(&contents).map_err(|error| JsonError::ReadFile {
        path: path.to_path_buf(),
        error,
    })
}

/// Read a file at the provided path into a string, without deserializing it.
/// The path must already exist.
#[inline]
pub fn read_to_string<T: AsRef<Path>>(path: T) -> Result<String, JsonError> {
    Ok(clean(fs::read_file(path.as_ref())?))
}

/// Write a file and serialize the provided data to the provided path. If the parent directory
/// does not exist, it will be created.
///
/// This function is primarily used internally for non-consumer facing files.
#[inline]
pub fn write_file<P, D>(path: P, json: &D, pretty: bool) -> Result<(), JsonError>
where
    P: AsRef<Path>,
    D: ?Sized + Serialize,
{
    let path = path.as_ref();

    trace!(file = ?path, "Stringifying JSON");

    let data = if pretty {
        serde_json::to_string_pretty(&json).map_err(|error| JsonError::StringifyFile {
            path: path.to_path_buf(),
            error,
        })?
    } else {
        serde_json::to_string(&json).map_err(|error| JsonError::StringifyFile {
            path: path.to_path_buf(),
            error,
        })?
    };

    fs::write_file(path, data)?;

    Ok(())
}

/// Write a file and serialize the provided data to the provided path, while taking the
/// closest `.editorconfig` into account. If the parent directory does not exist,
/// it will be created.
///
/// This function is used for consumer facing files, like configs.
#[cfg(feature = "editor-config")]
#[inline]
pub fn write_file_with_config<P: AsRef<Path>>(
    path: P,
    json: JsonValue,
    pretty: bool,
) -> Result<(), JsonError> {
    if !pretty {
        return write_file(path, &json, false);
    }

    use serde_json::ser::PrettyFormatter;
    use serde_json::Serializer;

    let path = path.as_ref();
    let editor_config = fs::get_editor_config_props(path);

    trace!(file = ?path, "Stringifying JSON with .editorconfig");

    // Based on serde_json::to_string_pretty!
    let mut writer = Vec::with_capacity(128);
    let mut serializer = Serializer::with_formatter(
        &mut writer,
        PrettyFormatter::with_indent(editor_config.indent.as_bytes()),
    );

    json.serialize(&mut serializer)
        .map_err(|error| JsonError::StringifyFile {
            path: path.to_path_buf(),
            error,
        })?;

    let mut data = unsafe { String::from_utf8_unchecked(writer) };
    editor_config.apply_eof(&mut data);

    fs::write_file(path, data)?;

    Ok(())
}