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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]

use std::{
    collections::HashMap,
    ffi::{CStr, CString},
    os::raw::{c_int, c_void},
    path::{Path, PathBuf},
    ptr,
};

use editorconfig_sys::{
    EDITORCONFIG_PARSE_MEMORY_ERROR, EDITORCONFIG_PARSE_NOT_FULL_PATH,
    EDITORCONFIG_PARSE_VERSION_TOO_NEW,
};

/// EditorConfig handle
pub struct EditorConfigHandle {
    handle: *mut c_void,
    config_filename: Option<CString>,
}

/// EditorConfig version
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version<T: Into<c_int>> {
    /// Major version number
    pub major: T,
    /// Minor version number
    pub minor: T,
    /// Patch version number
    pub patch: T,
}

impl<T: Into<c_int> + Copy> Version<T> {
    /// Safe [`Version`] constructor that panics when negative numbers are used
    pub fn new(major: T, minor: T, patch: T) -> Self {
        if c_int::is_negative(major.into())
            || c_int::is_negative(minor.into())
            || c_int::is_negative(patch.into())
        {
            panic!("Version numbers cannot be negative");
        }

        Version {
            major,
            minor,
            patch,
        }
    }
}

/// Parsing errors returned by [`EditorConfigHandle::parse`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseError {
    /// TODO: Add comment
    VersionTooNewError,
    /// TODO: Add comment
    MemoryError,
    /// [`EditorConfigHandle::parse`] must be called with an absolute path and
    /// returns this error if it was called with a relative path instead
    NotFullPathError,
    /// [`EditorConfigHandle::parse`] returns this error if your config file is
    /// invalid including the line number where the error occured
    LineError(c_int),
}

impl EditorConfigHandle {
    /// Creates a new [`EditorConfigHandle`]
    ///
    /// # Example
    ///
    /// ```
    /// let handle = editorconfig_rs::EditorConfigHandle::new();
    /// # assert!(handle.is_ok());
    /// ```
    ///
    pub fn new() -> Result<Self, &'static str> {
        let handle = unsafe { editorconfig_sys::editorconfig_handle_init() };
        if handle.is_null() {
            Err("Failed to create EditorConfigHandle")
        } else {
            Ok(EditorConfigHandle {
                handle,
                config_filename: None,
            })
        }
    }

    /// TODO: Add comment
    pub fn get_version(&self) -> Version<c_int> {
        let (mut major, mut minor, mut patch) = (-1, -1, -1);

        unsafe {
            editorconfig_sys::editorconfig_handle_get_version(
                self.handle,
                &mut major,
                &mut minor,
                &mut patch,
            );
        }

        Version::new(major, minor, patch)
    }

    /// TODO: Add comment
    pub fn set_version<T: Into<c_int>>(&self, version: Version<T>) {
        unsafe {
            editorconfig_sys::editorconfig_handle_set_version(
                self.handle,
                version.major.into(),
                version.minor.into(),
                version.patch.into(),
            );
        };
    }

    /// Returns the configuration filename iff it was previously set by calling
    /// [`EditorConfigHandle::set_config_filename`]; otherwise [`None`]
    ///
    /// Note: [`None`] just means the default filename `".editorconfig"` is used
    ///
    pub fn get_config_filename(&self) -> Option<String> {
        let filename =
            unsafe { editorconfig_sys::editorconfig_handle_get_conf_file_name(self.handle) };
        if filename.is_null() {
            None
        } else {
            let filename = unsafe { CStr::from_ptr(filename) };
            let filename = filename.to_str().map(|s| s.to_owned());
            filename.ok()
        }
    }

    /// Changes the default EditorConfig configuration filename
    ///
    pub fn set_config_filename(&mut self, filename: &str) {
        let err_msg = format!("Failed to create CString from filename: {}", filename);
        let filename = CString::new(filename).expect(&err_msg);
        unsafe {
            editorconfig_sys::editorconfig_handle_set_conf_file_name(
                self.handle,
                filename.as_ptr(),
            );
        };

        // Store the CString so it lives as long as the handle
        self.config_filename = Some(filename);
    }

    /// Searches an absolute path for the corresponding EditorConfig rules
    ///
    /// After parsing, you can get the rules by calling
    /// [`EditorConfigHandle::get_rules`].
    ///
    pub fn parse<P: AsRef<Path>>(&self, absolute_path: P) -> Option<ParseError> {
        let absolute_path = absolute_path.as_ref().to_str().expect("Invalid UTF-8 path");
        let err_msg = format!("Failed to create CString from path: {}", absolute_path);
        let absolute_path = CString::new(absolute_path).expect(&err_msg);

        let err_num =
            unsafe { editorconfig_sys::editorconfig_parse(absolute_path.as_ptr(), self.handle) };
        match err_num {
            0 => None,
            EDITORCONFIG_PARSE_VERSION_TOO_NEW => Some(ParseError::VersionTooNewError),
            EDITORCONFIG_PARSE_MEMORY_ERROR => Some(ParseError::MemoryError),
            EDITORCONFIG_PARSE_NOT_FULL_PATH => Some(ParseError::NotFullPathError),
            _ if err_num > 0 => Some(ParseError::LineError(err_num)),
            _ => unreachable!(),
        }
    }

    /// Returns the [path](PathBuf) of the invalid configuration file when
    /// [parse](EditorConfigHandle::parse) returned an [error](ParseError)
    ///
    /// # Returns
    ///
    /// The [path](PathBuf) of the invalid configuration file or [`None`] if
    /// there was no error
    ///
    pub fn get_error_file(&self) -> Option<PathBuf> {
        let err_file_path =
            unsafe { editorconfig_sys::editorconfig_handle_get_err_file(self.handle) };
        if err_file_path.is_null() {
            None
        } else {
            let err_file_path = unsafe { CStr::from_ptr(err_file_path) };
            err_file_path.to_str().map(PathBuf::from).ok()
        }
    }

    /// Returns the number of rules found after parsing
    ///
    /// # Example
    ///
    /// ```
    /// let handle = editorconfig_rs::EditorConfigHandle::new().unwrap();
    /// // Parse a file or a directory; otherwise `get_rule_count()` returns 0
    /// let rule_count = handle.get_rule_count();
    /// # assert_eq!(rule_count, 0);
    /// ```
    ///
    pub fn get_rule_count(&self) -> c_int {
        unsafe { editorconfig_sys::editorconfig_handle_get_name_value_count(self.handle) }
    }

    /// Returns a map of all rules found after parsing
    ///
    pub fn get_rules(&self) -> HashMap<String, String> {
        let rule_count = self.get_rule_count();
        let mut rules = HashMap::with_capacity(rule_count as usize);

        for rule_index in 0..rule_count {
            let (mut rule_name, mut rule_value) = (ptr::null(), ptr::null());

            unsafe {
                editorconfig_sys::editorconfig_handle_get_name_value(
                    self.handle,
                    rule_index,
                    &mut rule_name,
                    &mut rule_value,
                );
            }

            if rule_name.is_null() || rule_value.is_null() {
                panic!("rule name or value should never be null");
            }

            if let (Ok(rule_name), Ok(rule_value)) = (
                unsafe { CStr::from_ptr(rule_name) }
                    .to_str()
                    .map(|s| s.to_owned()),
                unsafe { CStr::from_ptr(rule_value) }
                    .to_str()
                    .map(|s| s.to_owned()),
            ) {
                rules.insert(rule_name, rule_value);
            }
        }

        rules
    }
}

impl Drop for EditorConfigHandle {
    fn drop(&mut self) {
        unsafe {
            editorconfig_sys::editorconfig_handle_destroy(self.handle);
        }
    }
}

/// Gets the error message for a [parsing error](ParseError) from the
/// underlying `libeditorconfig` C library
///
/// # Example
///
/// ```
/// use editorconfig_rs::ParseError;
///
/// let parse_err = ParseError::LineError(23);
/// if let Some(err_msg) = editorconfig_rs::get_error_message(parse_err) {
///     println!("Error parsing .editorconfig at line 23: {}", err_msg);
/// }
/// # else { panic!(); }
/// ```
///
pub fn get_error_message(parse_error: ParseError) -> Option<String> {
    let err_num = match parse_error {
        ParseError::VersionTooNewError => EDITORCONFIG_PARSE_VERSION_TOO_NEW,
        ParseError::MemoryError => EDITORCONFIG_PARSE_MEMORY_ERROR,
        ParseError::NotFullPathError => EDITORCONFIG_PARSE_NOT_FULL_PATH,
        ParseError::LineError(line_num) => line_num,
    };

    let err_msg = unsafe { editorconfig_sys::editorconfig_get_error_msg(err_num) };
    if err_msg.is_null() {
        None
    } else {
        let err_msg = unsafe { CStr::from_ptr(err_msg) };
        let err_msg = err_msg.to_str().map(|s| s.to_owned());
        err_msg.ok()
    }
}

/// Gets the [version](Version) number of the underlying `libeditorconfig` C library
///
/// # Example
///
/// ```
/// use editorconfig_rs::Version;
///
/// let Version{major, minor, patch} = editorconfig_rs::get_version();
/// # assert!(major >= 0);
/// # assert!(minor >= 12);
/// # assert!(patch >= 5);
/// ```
///
pub fn get_version() -> Version<c_int> {
    let (mut major, mut minor, mut patch) = (-1, -1, -1);
    unsafe {
        editorconfig_sys::editorconfig_get_version(&mut major, &mut minor, &mut patch);
    };

    Version::new(major, minor, patch)
}