2023-11-04 18:29:21 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use globset::{Candidate, GlobBuilder, GlobMatcher};
|
2020-03-22 09:55:13 +01:00
|
|
|
|
2020-04-22 21:45:47 +02:00
|
|
|
use crate::error::Result;
|
2023-11-04 18:41:39 +01:00
|
|
|
use builtin::BUILTIN_MAPPINGS;
|
2021-11-19 17:05:23 +01:00
|
|
|
use ignored_suffixes::IgnoredSuffixes;
|
2020-03-22 09:55:13 +01:00
|
|
|
|
2023-11-04 18:29:21 +01:00
|
|
|
mod builtin;
|
2021-11-19 17:05:23 +01:00
|
|
|
pub mod ignored_suffixes;
|
|
|
|
|
2023-11-04 18:29:21 +01:00
|
|
|
fn make_glob_matcher(from: &str) -> Result<GlobMatcher> {
|
|
|
|
let matcher = GlobBuilder::new(from)
|
|
|
|
.case_insensitive(true)
|
|
|
|
.literal_separator(true)
|
|
|
|
.build()?
|
|
|
|
.compile_matcher();
|
|
|
|
Ok(matcher)
|
2023-11-02 12:53:04 +01:00
|
|
|
}
|
|
|
|
|
2022-09-04 00:02:08 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2022-01-02 21:46:15 +01:00
|
|
|
#[non_exhaustive]
|
2020-03-22 09:55:13 +01:00
|
|
|
pub enum MappingTarget<'a> {
|
2021-10-25 17:59:12 +02:00
|
|
|
/// For mapping a path to a specific syntax.
|
2020-03-22 09:55:13 +01:00
|
|
|
MapTo(&'a str),
|
2021-10-25 17:59:12 +02:00
|
|
|
|
|
|
|
/// For mapping a path (typically an extension-less file name) to an unknown
|
|
|
|
/// syntax. This typically means later using the contents of the first line
|
|
|
|
/// of the file to determine what syntax to use.
|
2020-03-22 09:55:13 +01:00
|
|
|
MapToUnknown,
|
2021-10-25 17:59:12 +02:00
|
|
|
|
|
|
|
/// For mapping a file extension (e.g. `*.conf`) to an unknown syntax. This
|
|
|
|
/// typically means later using the contents of the first line of the file
|
|
|
|
/// to determine what syntax to use. However, if a syntax handles a file
|
|
|
|
/// name that happens to have the given file extension (e.g. `resolv.conf`),
|
|
|
|
/// then that association will have higher precedence, and the mapping will
|
|
|
|
/// be ignored.
|
|
|
|
MapExtensionToUnknown,
|
2020-03-22 09:55:13 +01:00
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2019-03-08 11:46:49 +01:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-03-22 09:55:13 +01:00
|
|
|
pub struct SyntaxMapping<'a> {
|
2023-11-04 18:41:39 +01:00
|
|
|
/// User-defined mappings at run time.
|
|
|
|
custom_mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
|
2021-11-19 17:05:23 +01:00
|
|
|
pub(crate) ignored_suffixes: IgnoredSuffixes<'a>,
|
2020-03-22 09:55:13 +01:00
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
impl<'a> SyntaxMapping<'a> {
|
2023-11-04 18:41:39 +01:00
|
|
|
pub fn new() -> SyntaxMapping<'a> {
|
2019-03-08 11:46:49 +01:00
|
|
|
Default::default()
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
|
2023-11-02 12:53:04 +01:00
|
|
|
let matcher = make_glob_matcher(from)?;
|
2023-11-04 18:41:39 +01:00
|
|
|
self.custom_mappings.push((matcher, to));
|
2020-03-22 09:55:13 +01:00
|
|
|
Ok(())
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 19:46:32 +01:00
|
|
|
/// Returns an iterator over all mappings. User-defined mappings are listed
|
|
|
|
/// before builtin mappings; mappings in front have higher precedence.
|
|
|
|
///
|
|
|
|
/// Builtin mappings' `GlobMatcher`s are lazily compiled.
|
2023-11-04 18:41:39 +01:00
|
|
|
///
|
|
|
|
/// Note that this function ignores builtin mappings that are invalid under
|
|
|
|
/// the current environment (i.e. their rules require an environment
|
|
|
|
/// variable that is unset).
|
2023-11-04 19:46:32 +01:00
|
|
|
pub fn all_mappings(&self) -> impl Iterator<Item = (&GlobMatcher, &MappingTarget<'a>)> {
|
2023-11-04 18:41:39 +01:00
|
|
|
self.custom_mappings()
|
|
|
|
.iter()
|
|
|
|
.map(|(matcher, target)| (matcher, target)) // as_ref
|
|
|
|
.chain(self.builtin_mappings())
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:46:32 +01:00
|
|
|
// IMPRV: ideally `Item` should be `(&'static GlobMatcher, &'static MappingTarget<'static>)`
|
|
|
|
// but `Iterator::chain` (used in `SyntaxMapping::all_mappings`) asserts `Item = Self::Item`
|
|
|
|
// so we need a lifetime downcast, which I'm not sure how to perform
|
|
|
|
/// Returns an iterator over all valid builtin mappings. Mappings in front
|
|
|
|
/// have higher precedence.
|
|
|
|
///
|
|
|
|
/// The `GlabMatcher`s are lazily compiled.
|
2023-11-04 18:41:39 +01:00
|
|
|
///
|
|
|
|
/// If a mapping rule requires an environment variable that is unset, it
|
|
|
|
/// will be ignored.
|
2023-11-04 19:46:32 +01:00
|
|
|
pub fn builtin_mappings(&self) -> impl Iterator<Item = (&GlobMatcher, &MappingTarget<'a>)> {
|
2023-11-04 18:41:39 +01:00
|
|
|
BUILTIN_MAPPINGS
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(matcher, target)| matcher.as_ref().map(|glob| (glob, target)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns all user-defined mappings.
|
|
|
|
pub fn custom_mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
|
|
|
|
&self.custom_mappings
|
2020-05-30 03:53:31 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 14:42:17 +01:00
|
|
|
pub fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
2022-08-16 22:42:15 +02:00
|
|
|
// Try matching on the file name as-is.
|
2021-09-10 21:58:46 +02:00
|
|
|
let candidate = Candidate::new(&path);
|
2020-08-06 09:20:33 +02:00
|
|
|
let candidate_filename = path.as_ref().file_name().map(Candidate::new);
|
2023-11-04 19:46:32 +01:00
|
|
|
for (glob, syntax) in self.all_mappings() {
|
2020-03-22 09:55:13 +01:00
|
|
|
if glob.is_match_candidate(&candidate)
|
2020-08-06 09:20:33 +02:00
|
|
|
|| candidate_filename
|
2020-03-22 09:55:13 +01:00
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |filename| glob.is_match_candidate(filename))
|
|
|
|
{
|
|
|
|
return Some(*syntax);
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
2022-08-16 22:42:15 +02:00
|
|
|
// Try matching on the file name after removing an ignored suffix.
|
|
|
|
let file_name = path.as_ref().file_name()?;
|
|
|
|
self.ignored_suffixes
|
|
|
|
.try_with_stripped_suffix(file_name, |stripped_file_name| {
|
|
|
|
Ok(self.get_syntax_for(stripped_file_name))
|
|
|
|
})
|
|
|
|
.ok()?
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
2021-11-19 17:05:23 +01:00
|
|
|
|
|
|
|
pub fn insert_ignored_suffix(&mut self, suffix: &'a str) {
|
|
|
|
self.ignored_suffixes.add_suffix(suffix);
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-01 21:11:41 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2023-11-04 18:41:39 +01:00
|
|
|
let mut map = SyntaxMapping::new();
|
2023-09-01 21:11:41 +02:00
|
|
|
map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
|
|
|
|
.ok();
|
|
|
|
map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/Cargo.lock"),
|
|
|
|
Some(MappingTarget::MapTo("TOML"))
|
|
|
|
);
|
|
|
|
assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/.ignore"),
|
|
|
|
Some(MappingTarget::MapTo("Git Ignore"))
|
|
|
|
);
|
|
|
|
}
|
2020-03-22 09:55:13 +01:00
|
|
|
|
2023-09-01 21:11:41 +02:00
|
|
|
#[test]
|
|
|
|
fn user_can_override_builtin_mappings() {
|
2023-11-04 18:41:39 +01:00
|
|
|
let mut map = SyntaxMapping::new();
|
2023-09-01 21:11:41 +02:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/etc/profile"),
|
|
|
|
Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
|
|
|
|
);
|
|
|
|
map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
|
|
|
|
.ok();
|
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/etc/profile"),
|
|
|
|
Some(MappingTarget::MapTo("My Syntax"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn builtin_mappings() {
|
2023-11-04 18:41:39 +01:00
|
|
|
let map = SyntaxMapping::new();
|
2020-03-22 10:37:35 +01:00
|
|
|
|
2023-09-01 21:11:41 +02:00
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/build"),
|
|
|
|
Some(MappingTarget::MapToUnknown)
|
|
|
|
);
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|