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
use crate::glob::Glob;
use crate::{rawvalue::RawValue, Properties};
use std::path::Path;
// Glob internals aren't stable enough to safely implement PartialEq here.
/// One section of an EditorConfig file.
#[derive(Clone)]
pub struct Section {
pattern: Glob,
props: crate::Properties,
}
impl Section {
/// Constrcts a new [`Section`] that applies to files matching the specified pattern.
pub fn new(pattern: &str) -> Section {
Section {
pattern: Glob::new(pattern),
props: crate::Properties::new(),
}
}
/// Returns a shared reference to the internal [`Properties`] map.
pub fn props(&self) -> &Properties {
&self.props
}
/// Returns a mutable reference to the internal [`Properties`] map.
pub fn props_mut(&mut self) -> &mut Properties {
&mut self.props
}
/// Extracts the [`Properties`] map from `self`.
pub fn into_props(self) -> Properties {
self.props
}
/// Adds a property with the specified key, lowercasing the key.
pub fn insert(&mut self, key: impl AsRef<str>, val: impl Into<RawValue>) {
self.props
.insert_raw_for_key(key.as_ref().to_lowercase(), val);
}
/// Returns true if and only if this section applies to a file at the specified path.
pub fn applies_to(&self, path: impl AsRef<Path>) -> bool {
self.pattern.matches(path.as_ref())
}
}
impl crate::PropertiesSource for &Section {
/// Adds this section's properties to a [`Properties`].
///
/// This implementation is infallible.
fn apply_to(
self,
props: &mut Properties,
path: impl AsRef<std::path::Path>,
) -> Result<(), crate::Error> {
let path_ref = path.as_ref();
if self.applies_to(path_ref) {
let _ = self.props.apply_to(props, path_ref);
}
Ok(())
}
}