2
2
#![ deny( rust_2018_idioms) ]
3
3
4
4
use bstr:: { BStr , BString } ;
5
+ use compact_str:: CompactStr ;
6
+ use std:: path:: PathBuf ;
5
7
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.
6
13
#[ derive( PartialEq , Eq , Debug , Hash , Ord , PartialOrd , Clone ) ]
7
14
#[ cfg_attr( feature = "serde1" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
8
- pub enum State < ' a > {
15
+ pub enum StateRef < ' a > {
9
16
/// The attribute is listed, or has the special value 'true'
10
17
Set ,
11
18
/// The attribute has the special value 'false', or was prefixed with a `-` sign.
@@ -18,87 +25,75 @@ pub enum State<'a> {
18
25
Unspecified ,
19
26
}
20
27
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 .
22
29
///
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.
24
46
#[ 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 > {
26
60
/// A list of pattern lists, each representing a patterns from a file or specified by hand, in the order they were
27
61
/// specified in.
28
62
///
29
63
/// During matching, this order is reversed.
30
64
pub patterns : Vec < PatternList < T > > ,
31
65
}
32
66
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)`_).
37
75
///
38
76
/// During matching, this order is reversed.
39
- pub patterns : Vec < ( git_glob :: Pattern , T :: Value ) > ,
77
+ pub patterns : Vec < PatternMapping < T :: Value > > ,
40
78
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 > ,
63
82
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
+ }
80
87
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 ,
97
93
}
98
- pub use match_group:: { Attributes , Ignore , Tag } ;
99
94
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 } ;
102
97
103
98
pub mod parse;
104
99
0 commit comments