2020-03-22 09:55:13 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use crate::errors::Result;
|
|
|
|
|
|
|
|
use globset::{Candidate, GlobBuilder, GlobMatcher};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum MappingTarget<'a> {
|
|
|
|
MapTo(&'a str),
|
|
|
|
MapToUnknown,
|
|
|
|
}
|
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> {
|
|
|
|
mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
impl<'a> SyntaxMapping<'a> {
|
|
|
|
pub fn empty() -> 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 builtin() -> SyntaxMapping<'a> {
|
|
|
|
let mut mapping = Self::empty();
|
|
|
|
mapping
|
|
|
|
.insert("build", MappingTarget::MapToUnknown)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
mapping
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
|
|
|
|
let glob = GlobBuilder::new(from)
|
|
|
|
.case_insensitive(false)
|
|
|
|
.literal_separator(true)
|
|
|
|
.build()?;
|
|
|
|
self.mappings.push((glob.compile_matcher(), to));
|
|
|
|
Ok(())
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
|
|
|
|
let candidate = Candidate::new(path.as_ref());
|
|
|
|
let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
|
|
|
|
for (ref glob, ref syntax) in &self.mappings {
|
|
|
|
if glob.is_match_candidate(&candidate)
|
|
|
|
|| canddidate_filename
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |filename| glob.is_match_candidate(filename))
|
|
|
|
{
|
|
|
|
return Some(*syntax);
|
|
|
|
}
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
2020-03-22 09:55:13 +01:00
|
|
|
None
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
2020-03-22 09:55:13 +01:00
|
|
|
let mut map = SyntaxMapping::empty();
|
|
|
|
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);
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/.ignore"),
|
|
|
|
Some(MappingTarget::MapTo("Git Ignore"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn builtin_mappings() {
|
|
|
|
let map = SyntaxMapping::builtin();
|
2018-10-17 22:30:09 +02:00
|
|
|
|
2020-03-22 09:55:13 +01:00
|
|
|
assert_eq!(
|
|
|
|
map.get_syntax_for("/path/to/build"),
|
|
|
|
Some(MappingTarget::MapToUnknown)
|
|
|
|
);
|
2018-10-17 22:30:09 +02:00
|
|
|
}
|