22#![ deny( rust_2018_idioms) ]
33
44use bstr:: { BStr , BString } ;
5+ use compact_str:: CompactStr ;
6+ use std:: path:: PathBuf ;
57
8+ pub use git_glob as glob;
9+
10+ /// The state an attribute can be in, referencing the value.
11+ ///
12+ /// Note that this doesn't contain the name.
613#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
714#[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
8- pub enum State < ' a > {
15+ pub enum StateRef < ' a > {
916 /// The attribute is listed, or has the special value 'true'
1017 Set ,
1118 /// The attribute has the special value 'false', or was prefixed with a `-` sign.
@@ -18,87 +25,75 @@ pub enum State<'a> {
1825 Unspecified ,
1926}
2027
21- /// A grouping of lists of patterns while possibly keeping associated to their base path .
28+ /// The state an attribute can be in, owning the value .
2229///
23- /// Patterns with base path are queryable relative to that base, otherwise they are relative to the repository root.
30+ /// Note that this doesn't contain the name.
31+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
32+ #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
33+ pub enum State {
34+ /// The attribute is listed, or has the special value 'true'
35+ Set ,
36+ /// The attribute has the special value 'false', or was prefixed with a `-` sign.
37+ Unset ,
38+ /// The attribute is set to the given value, which followed the `=` sign.
39+ /// Note that values can be empty.
40+ Value ( compact_str:: CompactStr ) ,
41+ /// The attribute isn't mentioned with a given path or is explicitly set to `Unspecified` using the `!` sign.
42+ Unspecified ,
43+ }
44+
45+ /// Name an attribute and describe it's assigned state.
2446#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
25- pub struct MatchGroup < T : match_group:: Tag > {
47+ #[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
48+ pub struct Assignment {
49+ /// The name of the attribute.
50+ pub name : CompactStr ,
51+ /// The state of the attribute.
52+ pub state : State ,
53+ }
54+
55+ /// A grouping of lists of patterns while possibly keeping associated to their base path.
56+ ///
57+ /// Pattern lists with base path are queryable relative to that base, otherwise they are relative to the repository root.
58+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Default ) ]
59+ pub struct MatchGroup < T : match_group:: Pattern = Attributes > {
2660 /// A list of pattern lists, each representing a patterns from a file or specified by hand, in the order they were
2761 /// specified in.
2862 ///
2963 /// During matching, this order is reversed.
3064 pub patterns : Vec < PatternList < T > > ,
3165}
3266
33- /// A list of patterns with an optional names, for matching against it.
34- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
35- pub struct PatternList < T : match_group:: Tag > {
36- /// Patterns and their associated data in the order they were loaded in or specified.
67+ /// A list of patterns which optionally know where they were loaded from and what their base is.
68+ ///
69+ /// Knowing their base which is relative to a source directory, it will ignore all path to match against
70+ /// that don't also start with said base.
71+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone , Default ) ]
72+ pub struct PatternList < T : match_group:: Pattern > {
73+ /// Patterns and their associated data in the order they were loaded in or specified,
74+ /// the line number in its source file or its sequence number (_`(pattern, value, line_number)`_).
3775 ///
3876 /// During matching, this order is reversed.
39- pub patterns : Vec < ( git_glob :: Pattern , T :: Value ) > ,
77+ pub patterns : Vec < PatternMapping < T :: Value > > ,
4078
41- /// The path at which the patterns are located in a format suitable for matches, or `None` if the patterns
42- /// are relative to the worktree root.
43- base : Option < BString > ,
44- }
45-
46- mod match_group {
47- use crate :: { MatchGroup , PatternList } ;
48- use std:: ffi:: OsString ;
49- use std:: path:: PathBuf ;
50-
51- /// A marker trait to identify the type of a description.
52- pub trait Tag : Clone + PartialEq + Eq + std:: fmt:: Debug + std:: hash:: Hash + Ord + PartialOrd {
53- /// The value associated with a pattern.
54- type Value : PartialEq + Eq + std:: fmt:: Debug + std:: hash:: Hash + Ord + PartialOrd + Clone ;
55- }
56-
57- /// Identify ignore patterns.
58- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
59- pub struct Ignore ;
60- impl Tag for Ignore {
61- type Value = ( ) ;
62- }
79+ /// The path from which the patterns were read, or `None` if the patterns
80+ /// don't originate in a file on disk.
81+ pub source : Option < PathBuf > ,
6382
64- /// Identify patterns with attributes.
65- #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
66- pub struct Attributes ;
67- impl Tag for Attributes {
68- /// TODO: identify the actual value, should be name/State pairs, but there is the question of storage.
69- type Value = ( ) ;
70- }
71-
72- impl MatchGroup < Ignore > {
73- /// See [PatternList::<Ignore>::from_overrides()] for details.
74- pub fn from_overrides ( patterns : impl IntoIterator < Item = impl Into < OsString > > ) -> Self {
75- MatchGroup {
76- patterns : vec ! [ PatternList :: <Ignore >:: from_overrides( patterns) ] ,
77- }
78- }
79- }
83+ /// The parent directory of source, or `None` if the patterns are _global_ to match against the repository root.
84+ /// It's processed to contain slashes only and to end with a trailing slash, and is relative to the repository root.
85+ pub base : Option < BString > ,
86+ }
8087
81- impl PatternList < Ignore > {
82- /// Parse a list of patterns, using slashes as path separators
83- pub fn from_overrides ( patterns : impl IntoIterator < Item = impl Into < OsString > > ) -> Self {
84- PatternList {
85- patterns : patterns
86- . into_iter ( )
87- . map ( Into :: into)
88- . filter_map ( |pattern| {
89- let pattern = git_features:: path:: into_bytes ( PathBuf :: from ( pattern) ) . ok ( ) ?;
90- git_glob:: parse ( pattern. as_ref ( ) ) . map ( |p| ( p, ( ) ) )
91- } )
92- . collect ( ) ,
93- base : None ,
94- }
95- }
96- }
88+ #[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
89+ pub struct PatternMapping < T > {
90+ pub pattern : git_glob:: Pattern ,
91+ pub value : T ,
92+ pub sequence_number : usize ,
9793}
98- pub use match_group:: { Attributes , Ignore , Tag } ;
9994
100- pub type Files = MatchGroup < Attributes > ;
101- pub type IgnoreFiles = MatchGroup < Ignore > ;
95+ mod match_group ;
96+ pub use match_group :: { Attributes , Ignore , Match , Pattern } ;
10297
10398pub mod parse;
10499
0 commit comments