on d9f163f help sign in
bat/src/syntax_mapping.rs
126 lines 3.4 kB history raw
1use std::path::Path;
3use crate::error::Result;
5use globset::{Candidate, GlobBuilder, GlobMatcher};
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub enum MappingTarget<'a> {
9 MapTo(&'a str),
10 MapToUnknown,
13#[derive(Debug, Clone, Default)]
14pub struct SyntaxMapping<'a> {
15 mappings: Vec<(GlobMatcher, MappingTarget<'a>)>,
18impl<'a> SyntaxMapping<'a> {
19 pub fn empty() -> SyntaxMapping<'a> {
20 Default::default()
21 }
23 pub fn builtin() -> SyntaxMapping<'a> {
24 let mut mapping = Self::empty();
25 mapping.insert("*.h", MappingTarget::MapTo("C++")).unwrap();
26 mapping
27 .insert("build", MappingTarget::MapToUnknown)
28 .unwrap();
29 mapping
30 .insert("**/.ssh/config", MappingTarget::MapTo("SSH Config"))
31 .unwrap();
32 mapping
33 .insert(
34 "**/bat/config",
35 MappingTarget::MapTo("Bourne Again Shell (bash)"),
36 )
37 .unwrap();
38 mapping
39 .insert(
40 "/etc/profile",
41 MappingTarget::MapTo("Bourne Again Shell (bash)"),
42 )
43 .unwrap();
45 // See #1008
46 mapping
47 .insert("rails", MappingTarget::MapToUnknown)
48 .unwrap();
50 mapping
51 }
53 pub fn insert(&mut self, from: &str, to: MappingTarget<'a>) -> Result<()> {
54 let glob = GlobBuilder::new(from)
55 .case_insensitive(false)
56 .literal_separator(true)
57 .build()?;
58 self.mappings.push((glob.compile_matcher(), to));
59 Ok(())
60 }
62 pub fn mappings(&self) -> &[(GlobMatcher, MappingTarget<'a>)] {
63 &self.mappings
64 }
66 pub(crate) fn get_syntax_for(&self, path: impl AsRef<Path>) -> Option<MappingTarget<'a>> {
67 let candidate = Candidate::new(path.as_ref());
68 let canddidate_filename = path.as_ref().file_name().map(Candidate::new);
69 for (ref glob, ref syntax) in self.mappings.iter().rev() {
70 if glob.is_match_candidate(&candidate)
71 || canddidate_filename
72 .as_ref()
73 .map_or(false, |filename| glob.is_match_candidate(filename))
74 {
75 return Some(*syntax);
76 }
77 }
78 None
79 }
82#[test]
83fn basic() {
84 let mut map = SyntaxMapping::empty();
85 map.insert("/path/to/Cargo.lock", MappingTarget::MapTo("TOML"))
86 .ok();
87 map.insert("/path/to/.ignore", MappingTarget::MapTo("Git Ignore"))
88 .ok();
90 assert_eq!(
91 map.get_syntax_for("/path/to/Cargo.lock"),
92 Some(MappingTarget::MapTo("TOML"))
93 );
94 assert_eq!(map.get_syntax_for("/path/to/other.lock"), None);
96 assert_eq!(
97 map.get_syntax_for("/path/to/.ignore"),
98 Some(MappingTarget::MapTo("Git Ignore"))
99 );
102#[test]
103fn user_can_override_builtin_mappings() {
104 let mut map = SyntaxMapping::builtin();
106 assert_eq!(
107 map.get_syntax_for("/etc/profile"),
108 Some(MappingTarget::MapTo("Bourne Again Shell (bash)"))
109 );
110 map.insert("/etc/profile", MappingTarget::MapTo("My Syntax"))
111 .ok();
112 assert_eq!(
113 map.get_syntax_for("/etc/profile"),
114 Some(MappingTarget::MapTo("My Syntax"))
115 );
118#[test]
119fn builtin_mappings() {
120 let map = SyntaxMapping::builtin();
122 assert_eq!(
123 map.get_syntax_for("/path/to/build"),
124 Some(MappingTarget::MapToUnknown)
125 );