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
use crate::fs::{self, FsError};
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, PathBuf};
use thiserror::Error;
use tracing::trace;

pub use serde_yaml::{
    from_str, from_value, to_string, to_value, Mapping as YamlMapping, Number as YamlNumber,
    Sequence as YamlSequence, Value as YamlValue,
};

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

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

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

static WHITESPACE_PREFIX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\s+)").unwrap());

/// Recursively merge [YamlValue] objects, with values from next overwriting previous.
#[inline]
pub fn merge(prev: &YamlValue, next: &YamlValue) -> YamlValue {
    match (prev, next) {
        (YamlValue::Mapping(prev_object), YamlValue::Mapping(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());
                }
            }

            YamlValue::Mapping(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, YamlError>
where
    P: AsRef<Path>,
    D: DeserializeOwned,
{
    let path = path.as_ref();
    let contents = fs::read_file(path)?;

    trace!(file = %path.display(), "Parsing YAML");

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

/// 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, yaml: &D) -> Result<(), YamlError>
where
    P: AsRef<Path>,
    D: ?Sized + Serialize,
{
    let path = path.as_ref();

    trace!(file = %path.display(), "Stringifying YAML");

    let data = serde_yaml::to_string(&yaml).map_err(|error| YamlError::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_with_config<P, D>(path: P, yaml: &D) -> Result<(), YamlError>
where
    P: AsRef<Path>,
    D: ?Sized + Serialize,
{
    let path = path.as_ref();
    let editor_config = fs::get_editor_config_props(path);

    trace!(file = %path.display(), "Stringifying YAML with .editorconfig");

    let mut data = serde_yaml::to_string(&yaml)
        .map_err(|error| YamlError::StringifyFile {
            path: path.to_path_buf(),
            error,
        })?
        .trim()
        .to_string();

    // serde_yaml does not support customizing the indentation character. So to work around
    // this, we do it manually on the YAML string, but only if the indent is different than
    // a double space (the default), which can be customized with `.editorconfig`.
    if editor_config.indent != "  " {
        data = data
            .split('\n')
            .map(|line| {
                if !line.starts_with("  ") {
                    return line.to_string();
                }

                WHITESPACE_PREFIX
                    .replace_all(line, |caps: &regex::Captures| {
                        editor_config
                            .indent
                            .repeat(caps.get(1).unwrap().as_str().len() / 2)
                    })
                    .to_string()
            })
            .collect::<Vec<_>>()
            .join("\n");
    }

    data += &editor_config.eof;

    fs::write_file(path, data)?;

    Ok(())
}