55//! - shortcuts to open/write to the file
66use crate :: ConfigName ;
77use camino:: { Utf8Path , Utf8PathBuf } ;
8- use enumflags2:: { BitFlags , bitflags} ;
9- use smallvec:: SmallVec ;
108use std:: borrow:: Cow ;
119use std:: cmp:: Ordering ;
1210use std:: fmt:: { Debug , Formatter } ;
1311use std:: fs:: read_to_string;
1412use std:: hash:: Hash ;
15- use std:: ops:: DerefMut ;
1613use std:: path:: { Path , PathBuf } ;
1714use std:: { fs:: File , io, io:: Write , ops:: Deref } ;
1815
1916/// The priority of the file
17+ // NOTE: The order of the variants is important, the one on the top has the highest priority
2018#[ derive( Debug , Clone , Copy , Default , PartialEq , Eq , Ord , PartialOrd , Hash ) ]
21- #[ repr( u8 ) ]
22- #[ bitflags]
2319#[ cfg_attr(
2420 feature = "serde" ,
2521 derive( serde:: Serialize , serde:: Deserialize ) ,
2622 serde( rename_all = "camelCase" )
2723) ]
2824#[ cfg_attr( feature = "schema" , derive( schemars:: JsonSchema ) ) ]
29- // NOTE: The order of the variants is important, the one on the top has the highest priority
30- pub enum FileKind {
25+ pub enum FileKinds {
3126 /// A configuration file has the highest priority. It's usually `biome.json` and `biome.jsonc`
3227 ///
3328 /// Other third-party configuration files might be added in the future
@@ -47,71 +42,8 @@ pub enum FileKind {
4742 Handleable ,
4843}
4944
50- #[ derive( Clone , Hash , Ord , PartialOrd , Eq , PartialEq , Default ) ]
51- #[ cfg_attr(
52- feature = "serde" ,
53- derive( serde:: Serialize , serde:: Deserialize ) ,
54- serde(
55- from = "smallvec::SmallVec<[FileKind; 5]>" ,
56- into = "smallvec::SmallVec<[FileKind; 5]>"
57- )
58- ) ]
59- pub struct FileKinds ( BitFlags < FileKind > ) ;
60-
61- impl From < SmallVec < [ FileKind ; 5 ] > > for FileKinds {
62- fn from ( value : SmallVec < [ FileKind ; 5 ] > ) -> Self {
63- value. into_iter ( ) . fold ( Self :: default ( ) , |mut acc, kind| {
64- acc. insert ( kind) ;
65- acc
66- } )
67- }
68- }
69-
70- impl Debug for FileKinds {
71- fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
72- let mut list = f. debug_list ( ) ;
73- for kind in self . iter ( ) {
74- match kind {
75- FileKind :: Config => list. entry ( & "Config" ) ,
76- FileKind :: Manifest => list. entry ( & "Manifest" ) ,
77- FileKind :: Ignore => list. entry ( & "Ignore" ) ,
78- FileKind :: Inspectable => list. entry ( & "Inspectable" ) ,
79- FileKind :: Handleable => list. entry ( & "Handleable" ) ,
80- FileKind :: Directory => list. entry ( & "Directory" ) ,
81- } ;
82- }
83- list. finish ( )
84- }
85- }
86-
87- impl From < FileKinds > for SmallVec < [ FileKind ; 5 ] > {
88- fn from ( value : FileKinds ) -> Self {
89- value. iter ( ) . collect ( )
90- }
91- }
92-
93- impl Deref for FileKinds {
94- type Target = BitFlags < FileKind > ;
95-
96- fn deref ( & self ) -> & Self :: Target {
97- & self . 0
98- }
99- }
100-
101- impl DerefMut for FileKinds {
102- fn deref_mut ( & mut self ) -> & mut Self :: Target {
103- & mut self . 0
104- }
105- }
106-
107- impl From < FileKind > for FileKinds {
108- fn from ( flag : FileKind ) -> Self {
109- Self ( BitFlags :: from ( flag) )
110- }
111- }
112-
11345/// This is an internal representation of a path inside the Biome daemon.
114- /// This type has its own [Ord] implementation driven by its [FileKind ], where certain files must be inspected
46+ /// This type has its own [Ord] implementation driven by its [FileKinds ], where certain files must be inspected
11547/// before others. For example, configuration files and ignore files must have priority over other files.
11648#[ derive( Debug , Clone , Eq , PartialEq , Hash , Default ) ]
11749pub struct BiomePath {
@@ -127,7 +59,7 @@ impl BiomePath {
12759 pub fn new ( path_to_file : impl Into < Utf8PathBuf > ) -> Self {
12860 let path = path_to_file. into ( ) ;
12961 let kind = if path. is_dir ( ) {
130- FileKind :: Directory . into ( )
62+ FileKinds :: Directory
13163 } else {
13264 path. file_name ( ) . map ( Self :: priority) . unwrap_or_default ( )
13365 } ;
@@ -152,7 +84,7 @@ impl BiomePath {
15284 pub fn to_written ( & self ) -> Self {
15385 Self {
15486 path : self . path . clone ( ) ,
155- kind : self . kind . clone ( ) ,
87+ kind : self . kind ,
15688 was_written : true ,
15789 }
15890 }
@@ -189,41 +121,41 @@ impl BiomePath {
189121 /// - Other files are considered as files to handle
190122 fn priority ( file_name : & str ) -> FileKinds {
191123 if file_name == ConfigName :: biome_json ( ) || file_name == ConfigName :: biome_jsonc ( ) {
192- FileKind :: Config . into ( )
124+ FileKinds :: Config
193125 } else if matches ! (
194126 file_name,
195127 "package.json" | "tsconfig.json" | "jsconfig.json"
196128 ) {
197- FileKind :: Manifest . into ( )
129+ FileKinds :: Manifest
198130 } else if matches ! ( file_name, ".gitignore" | ".ignore" ) {
199- FileKind :: Ignore . into ( )
131+ FileKinds :: Ignore
200132 } else {
201- FileKind :: Handleable . into ( )
133+ FileKinds :: Handleable
202134 }
203135 }
204136
205137 #[ inline( always) ]
206138 pub fn is_config ( & self ) -> bool {
207- self . kind . contains ( FileKind :: Config )
139+ matches ! ( self . kind, FileKinds :: Config )
208140 }
209141
210142 #[ inline( always) ]
211143 pub fn is_manifest ( & self ) -> bool {
212- self . kind . contains ( FileKind :: Manifest )
144+ matches ! ( self . kind, FileKinds :: Manifest )
213145 }
214146
215147 #[ inline( always) ]
216148 pub fn is_ignore ( & self ) -> bool {
217- self . kind . contains ( FileKind :: Ignore )
149+ matches ! ( self . kind, FileKinds :: Ignore )
218150 }
219151
220152 #[ inline( always) ]
221153 pub fn is_dir ( & self ) -> bool {
222- self . kind . contains ( FileKind :: Directory )
154+ matches ! ( self . kind, FileKinds :: Directory )
223155 }
224156 #[ inline( always) ]
225157 pub fn is_handleable ( & self ) -> bool {
226- self . kind . contains ( FileKind :: Handleable )
158+ matches ! ( self . kind, FileKinds :: Handleable )
227159 }
228160
229161 /// Returns `true` if the path is inside `node_modules`
@@ -323,17 +255,6 @@ impl schemars::JsonSchema for BiomePath {
323255 }
324256}
325257
326- #[ cfg( feature = "schema" ) ]
327- impl schemars:: JsonSchema for FileKinds {
328- fn schema_name ( ) -> String {
329- String :: from ( "FileKind" )
330- }
331-
332- fn json_schema ( generator : & mut schemars:: r#gen:: SchemaGenerator ) -> schemars:: schema:: Schema {
333- <Vec < FileKind > >:: json_schema ( generator)
334- }
335- }
336-
337258impl From < BiomePath > for Utf8PathBuf {
338259 fn from ( path : BiomePath ) -> Self {
339260 path. path
@@ -357,7 +278,7 @@ impl Ord for BiomePath {
357278
358279#[ cfg( test) ]
359280mod test {
360- use crate :: path:: { FileKind , FileKinds } ;
281+ use crate :: path:: FileKinds ;
361282
362283 #[ test]
363284 fn test_biome_paths ( ) {
@@ -367,14 +288,11 @@ mod test {
367288 let path = Utf8PathBuf :: from ( "src/package.json" ) ;
368289 let biome_path = BiomePath :: new ( path) ;
369290 assert_eq ! ( biome_path. file_name( ) , Some ( "package.json" ) ) ;
370- assert_eq ! (
371- BiomePath :: priority( "package.json" ) ,
372- FileKind :: Manifest . into( )
373- ) ;
374- assert_eq ! ( BiomePath :: priority( "biome.json" ) , FileKind :: Config . into( ) ) ;
375- assert_eq ! ( BiomePath :: priority( "biome.jsonc" ) , FileKind :: Config . into( ) ) ;
376- assert_eq ! ( BiomePath :: priority( ".gitignore" ) , FileKind :: Ignore . into( ) ) ;
377- assert_eq ! ( BiomePath :: priority( ".ignore" ) , FileKind :: Ignore . into( ) ) ;
291+ assert_eq ! ( BiomePath :: priority( "package.json" ) , FileKinds :: Manifest ) ;
292+ assert_eq ! ( BiomePath :: priority( "biome.json" ) , FileKinds :: Config ) ;
293+ assert_eq ! ( BiomePath :: priority( "biome.jsonc" ) , FileKinds :: Config ) ;
294+ assert_eq ! ( BiomePath :: priority( ".gitignore" ) , FileKinds :: Ignore ) ;
295+ assert_eq ! ( BiomePath :: priority( ".ignore" ) , FileKinds :: Ignore ) ;
378296 }
379297
380298 #[ test]
@@ -434,13 +352,13 @@ mod test {
434352 let result = serde_json:: from_str :: < FileKinds > ( "[\" config\" ]" ) ;
435353 assert ! ( result. is_ok( ) ) ;
436354 let file_kinds = result. unwrap ( ) ;
437- assert ! ( file_kinds. contains ( FileKind :: Config ) ) ;
355+ assert_eq ! ( file_kinds, FileKinds :: Config ) ;
438356 }
439357
440358 #[ test]
441359 #[ cfg( feature = "serde" ) ]
442360 fn serialize_file_kind_into_vec ( ) {
443- let file_kinds = FileKinds :: from ( FileKind :: Config ) ;
361+ let file_kinds = FileKinds :: Config ;
444362 let result = serde_json:: to_string ( & file_kinds) ;
445363 assert ! ( result. is_ok( ) ) ;
446364 assert_eq ! ( result. unwrap( ) , "[\" config\" ]" ) ;
0 commit comments