11use anyhow:: { Context , Result } ;
2- use std:: { collections:: HashMap , path:: Path } ;
3- use std:: fs;
42use bstr:: BStr ;
53use gix_submodule:: config:: { Branch , FetchRecurse , Ignore , Update } ;
6- use toml_edit:: { DocumentMut , Item , Table , Array , value} ;
74use serde:: { Deserialize , Serialize } ;
5+ use std:: fs;
6+ use std:: { collections:: HashMap , path:: Path } ;
7+ use toml_edit:: { Array , DocumentMut , Item , Table , value} ;
88
99/**========================================================================
1010 ** Wrappers for Gix Submodule Config
@@ -54,10 +54,9 @@ impl<'de> Deserialize<'de> for SerializableIgnore {
5454 // Convert String to BStr for the TryFrom implementation
5555 let bstr = BStr :: new ( s. as_bytes ( ) ) ;
5656 match Ignore :: try_from ( bstr) {
57- Ok ( ignore) => Ok ( SerializableIgnore ( ignore) ) ,
58- Err ( _) => Err ( serde:: de:: Error :: custom ( format ! (
59- "Invalid ignore value: {}" ,
60- s
57+ Ok ( ignore) => Ok ( Self ( ignore) ) ,
58+ Err ( ( ) ) => Err ( serde:: de:: Error :: custom ( format ! (
59+ "Invalid ignore value: {s}"
6160 ) ) ) ,
6261 }
6362 }
@@ -86,12 +85,11 @@ impl<'de> Deserialize<'de> for SerializableFetchRecurse {
8685 {
8786 let s = String :: deserialize ( deserializer) ?;
8887 match s. as_str ( ) {
89- "on-demand" => Ok ( SerializableFetchRecurse ( FetchRecurse :: OnDemand ) ) ,
90- "always" => Ok ( SerializableFetchRecurse ( FetchRecurse :: Always ) ) ,
91- "never" => Ok ( SerializableFetchRecurse ( FetchRecurse :: Never ) ) ,
88+ "on-demand" => Ok ( Self ( FetchRecurse :: OnDemand ) ) ,
89+ "always" => Ok ( Self ( FetchRecurse :: Always ) ) ,
90+ "never" => Ok ( Self ( FetchRecurse :: Never ) ) ,
9291 _ => Err ( serde:: de:: Error :: custom ( format ! (
93- "Invalid fetch recurse value: {}" ,
94- s
92+ "Invalid fetch recurse value: {s}"
9593 ) ) ) ,
9694 }
9795 }
@@ -120,10 +118,9 @@ impl<'de> Deserialize<'de> for SerializableBranch {
120118 // Convert String to BStr for the TryFrom implementation
121119 let bstr = BStr :: new ( s. as_bytes ( ) ) ;
122120 match Branch :: try_from ( bstr) {
123- Ok ( branch) => Ok ( SerializableBranch ( branch) ) ,
121+ Ok ( branch) => Ok ( Self ( branch) ) ,
124122 Err ( e) => Err ( serde:: de:: Error :: custom ( format ! (
125- "Invalid branch value '{}': {}" ,
126- s, e
123+ "Invalid branch value '{s}': {e}"
127124 ) ) ) ,
128125 }
129126 }
@@ -142,7 +139,7 @@ impl Serialize for SerializableUpdate {
142139 Update :: None => serializer. serialize_str ( "none" ) ,
143140 Update :: Command ( cmd) => {
144141 // Convert BString to String with ! prefix
145- let cmd_str = format ! ( "!{}" , cmd . to_string ( ) ) ;
142+ let cmd_str = format ! ( "!{cmd}" ) ;
146143 serializer. serialize_str ( & cmd_str)
147144 }
148145 }
@@ -159,10 +156,9 @@ impl<'de> Deserialize<'de> for SerializableUpdate {
159156 // Convert String to BStr for the TryFrom implementation
160157 let bstr = BStr :: new ( s. as_bytes ( ) ) ;
161158 match Update :: try_from ( bstr) {
162- Ok ( update) => Ok ( SerializableUpdate ( update) ) ,
163- Err ( _) => Err ( serde:: de:: Error :: custom ( format ! (
164- "Invalid update value: {}" ,
165- s
159+ Ok ( update) => Ok ( Self ( update) ) ,
160+ Err ( ( ) ) => Err ( serde:: de:: Error :: custom ( format ! (
161+ "Invalid update value: {s}"
166162 ) ) ) ,
167163 }
168164 }
@@ -246,6 +242,7 @@ pub struct SubmoduleGitOptions {
246242impl SubmoduleGitOptions {
247243 /// Create a new instance with default git options
248244 #[ allow( dead_code) ]
245+ #[ must_use]
249246 pub fn new ( ) -> Self {
250247 Self {
251248 ignore : Some ( SerializableIgnore ( Ignore :: default ( ) ) ) ,
@@ -262,6 +259,7 @@ pub struct SubmoduleDefaults(pub SubmoduleGitOptions);
262259impl SubmoduleDefaults {
263260 /// Create new default submodule configuration
264261 #[ allow( dead_code) ]
262+ #[ must_use]
265263 pub fn new ( ) -> Self {
266264 Self ( SubmoduleGitOptions :: new ( ) )
267265 }
@@ -286,6 +284,7 @@ pub struct SubmoduleConfig {
286284impl SubmoduleConfig {
287285 /// Create a new submodule configuration with defaults
288286 #[ allow( dead_code) ]
287+ #[ must_use]
289288 pub fn new ( ) -> Self {
290289 Self {
291290 git_options : SubmoduleGitOptions :: new ( ) ,
@@ -298,10 +297,10 @@ impl SubmoduleConfig {
298297 /// Check if our active setting matches what git would report
299298 /// `git_active_state` should be the result of calling git's active check
300299 #[ allow( dead_code) ]
301- pub fn active_setting_matches_git ( & self , git_active_state : bool ) -> bool {
300+ #[ must_use]
301+ pub const fn active_setting_matches_git ( & self , git_active_state : bool ) -> bool {
302302 self . active == git_active_state
303303 }
304-
305304}
306305
307306/// Main configuration structure for the submod tool
@@ -315,12 +314,11 @@ pub struct Config {
315314 pub submodules : HashMap < String , SubmoduleConfig > ,
316315}
317316
318-
319317impl Config {
320318 /// Load configuration from a TOML file
321319 pub fn load ( path : & Path ) -> Result < Self > {
322320 if !path. exists ( ) {
323- return Ok ( Config {
321+ return Ok ( Self {
324322 defaults : SubmoduleDefaults :: default ( ) ,
325323 submodules : HashMap :: new ( ) ,
326324 } ) ;
@@ -329,8 +327,7 @@ impl Config {
329327 let content = fs:: read_to_string ( path)
330328 . with_context ( || format ! ( "Failed to read config file: {}" , path. display( ) ) ) ?;
331329
332- toml:: from_str ( & content)
333- . with_context ( || "Failed to parse TOML config" )
330+ toml:: from_str ( & content) . with_context ( || "Failed to parse TOML config" )
334331 }
335332
336333 /// Save configuration to a TOML file
@@ -343,14 +340,15 @@ impl Config {
343340 let mut doc = if path. exists ( ) {
344341 let content = fs:: read_to_string ( path)
345342 . with_context ( || format ! ( "Failed to read existing config: {}" , path. display( ) ) ) ?;
346- content. parse :: < DocumentMut > ( )
343+ content
344+ . parse :: < DocumentMut > ( )
347345 . with_context ( || "Failed to parse existing TOML document" ) ?
348346 } else {
349347 // Create a beautiful new document with header comment
350348 let mut doc = DocumentMut :: new ( ) ;
351349 doc. insert (
352350 "# Submodule configuration for gitoxide-based submodule manager" ,
353- Item :: None
351+ Item :: None ,
354352 ) ;
355353 doc. insert ( "# Each section [name] defines a submodule" , Item :: None ) ;
356354 doc. insert ( "" , Item :: None ) ; // Empty line for spacing
@@ -386,7 +384,8 @@ impl Config {
386384 }
387385
388386 // Remove existing submodule sections but preserve defaults and comments
389- let keys_to_remove: Vec < String > = doc. iter ( )
387+ let keys_to_remove: Vec < String > = doc
388+ . iter ( )
390389 . filter_map ( |( key, _) | {
391390 if key != "defaults" && self . submodules . contains_key ( key) {
392391 Some ( key. to_string ( ) )
@@ -455,11 +454,11 @@ impl Config {
455454 Ok ( ( ) )
456455 }
457456
458- fn defaults_are_empty ( & self ) -> bool {
459- self . defaults . 0 . ignore . is_none ( ) &&
460- self . defaults . 0 . update . is_none ( ) &&
461- self . defaults . 0 . branch . is_none ( ) &&
462- self . defaults . 0 . fetch_recurse . is_none ( )
457+ const fn defaults_are_empty ( & self ) -> bool {
458+ self . defaults . 0 . ignore . is_none ( )
459+ && self . defaults . 0 . update . is_none ( )
460+ && self . defaults . 0 . branch . is_none ( )
461+ && self . defaults . 0 . fetch_recurse . is_none ( )
463462 }
464463
465464 /// Add a submodule configuration
@@ -473,29 +472,40 @@ impl Config {
473472 }
474473
475474 /// Get the effective setting for a submodule, falling back to defaults
476- pub fn get_effective_setting ( & self , submodule : & SubmoduleConfig , setting : & str ) -> Option < String > {
475+ #[ must_use]
476+ pub fn get_effective_setting (
477+ & self ,
478+ submodule : & SubmoduleConfig ,
479+ setting : & str ,
480+ ) -> Option < String > {
477481 // Check submodule-specific setting first, then fall back to defaults
478482 match setting {
479483 "ignore" => {
480- submodule. git_options . ignore . as_ref ( )
484+ submodule
485+ . git_options
486+ . ignore
487+ . as_ref ( )
481488 . or ( self . defaults . 0 . ignore . as_ref ( ) )
482- . map ( |s| format ! ( "{:?}" , s) ) // Convert to string representation
483- }
484- "update" => {
485- submodule. git_options . update . as_ref ( )
486- . or ( self . defaults . 0 . update . as_ref ( ) )
487- . map ( |s| format ! ( "{:?}" , s) )
488- }
489- "branch" => {
490- submodule. git_options . branch . as_ref ( )
491- . or ( self . defaults . 0 . branch . as_ref ( ) )
492- . map ( |s| format ! ( "{:?}" , s) )
493- }
494- "fetchRecurse" => {
495- submodule. git_options . fetch_recurse . as_ref ( )
496- . or ( self . defaults . 0 . fetch_recurse . as_ref ( ) )
497- . map ( |s| format ! ( "{:?}" , s) )
489+ . map ( |s| format ! ( "{s:?}" ) ) // Convert to string representation
498490 }
491+ "update" => submodule
492+ . git_options
493+ . update
494+ . as_ref ( )
495+ . or ( self . defaults . 0 . update . as_ref ( ) )
496+ . map ( |s| format ! ( "{s:?}" ) ) ,
497+ "branch" => submodule
498+ . git_options
499+ . branch
500+ . as_ref ( )
501+ . or ( self . defaults . 0 . branch . as_ref ( ) )
502+ . map ( |s| format ! ( "{s:?}" ) ) ,
503+ "fetchRecurse" => submodule
504+ . git_options
505+ . fetch_recurse
506+ . as_ref ( )
507+ . or ( self . defaults . 0 . fetch_recurse . as_ref ( ) )
508+ . map ( |s| format ! ( "{s:?}" ) ) ,
499509 _ => None ,
500510 }
501511 }
0 commit comments