55
66use std:: borrow:: Cow ;
77
8- use crate :: utf8;
8+ use crate :: { utf8, Action } ;
99
1010use anyhow:: Result ;
1111
@@ -134,11 +134,17 @@ impl<'a> Cmdline<'a> {
134134
135135 /// Add or modify a parameter to the command line
136136 ///
137- /// Returns `true` if the parameter was added or modified.
137+ /// Returns `Action::Added` if the parameter did not exist before
138+ /// and was added.
138139 ///
139- /// Returns `false` if the parameter already existed with the same
140- /// content.
141- pub fn add_or_modify ( & mut self , param : & Parameter ) -> bool {
140+ /// Returns `Action::Modified` if the parameter existed before,
141+ /// but contained a different value. The value was updated to the
142+ /// newly-requested value.
143+ ///
144+ /// Returns `Action::Existed` if the parameter existed before, and
145+ /// contained the same value as the newly-requested value. No
146+ /// modification was made.
147+ pub fn add_or_modify ( & mut self , param : & Parameter ) -> Action {
142148 let mut new_params = Vec :: new ( ) ;
143149 let mut modified = false ;
144150 let mut seen_key = false ;
@@ -170,14 +176,14 @@ impl<'a> Cmdline<'a> {
170176 self_mut. push ( b' ' ) ;
171177 }
172178 self_mut. extend_from_slice ( param. parameter ) ;
173- return true ;
179+ return Action :: Added ;
174180 }
175181 if modified {
176182 self . 0 = Cow :: Owned ( new_params. join ( b" " . as_slice ( ) ) ) ;
177- true
183+ Action :: Modified
178184 } else {
179185 // The parameter already existed with the same content, and there were no duplicates.
180- false
186+ Action :: Existed
181187 }
182188 }
183189
@@ -654,22 +660,28 @@ mod tests {
654660 let mut kargs = Cmdline :: from ( b"foo=bar" ) ;
655661
656662 // add new
657- assert ! ( kargs. add_or_modify( & param( "baz" ) ) ) ;
663+ assert ! ( matches! ( kargs. add_or_modify( & param( "baz" ) ) , Action :: Added ) ) ;
658664 let mut iter = kargs. iter ( ) ;
659665 assert_eq ! ( iter. next( ) , Some ( param( "foo=bar" ) ) ) ;
660666 assert_eq ! ( iter. next( ) , Some ( param( "baz" ) ) ) ;
661667 assert_eq ! ( iter. next( ) , None ) ;
662668
663669 // modify existing
664- assert ! ( kargs. add_or_modify( & param( "foo=fuz" ) ) ) ;
670+ assert ! ( matches!(
671+ kargs. add_or_modify( & param( "foo=fuz" ) ) ,
672+ Action :: Modified
673+ ) ) ;
665674 iter = kargs. iter ( ) ;
666675 assert_eq ! ( iter. next( ) , Some ( param( "foo=fuz" ) ) ) ;
667676 assert_eq ! ( iter. next( ) , Some ( param( "baz" ) ) ) ;
668677 assert_eq ! ( iter. next( ) , None ) ;
669678
670679 // already exists with same value returns false and doesn't
671680 // modify anything
672- assert ! ( !kargs. add_or_modify( & param( "foo=fuz" ) ) ) ;
681+ assert ! ( matches!(
682+ kargs. add_or_modify( & param( "foo=fuz" ) ) ,
683+ Action :: Existed
684+ ) ) ;
673685 iter = kargs. iter ( ) ;
674686 assert_eq ! ( iter. next( ) , Some ( param( "foo=fuz" ) ) ) ;
675687 assert_eq ! ( iter. next( ) , Some ( param( "baz" ) ) ) ;
@@ -679,14 +691,17 @@ mod tests {
679691 #[ test]
680692 fn test_add_or_modify_empty_cmdline ( ) {
681693 let mut kargs = Cmdline :: from ( b"" ) ;
682- assert ! ( kargs. add_or_modify( & param( "foo" ) ) ) ;
694+ assert ! ( matches! ( kargs. add_or_modify( & param( "foo" ) ) , Action :: Added ) ) ;
683695 assert_eq ! ( kargs. 0 , b"foo" . as_slice( ) ) ;
684696 }
685697
686698 #[ test]
687699 fn test_add_or_modify_duplicate_parameters ( ) {
688700 let mut kargs = Cmdline :: from ( b"a=1 a=2" ) ;
689- assert ! ( kargs. add_or_modify( & param( "a=3" ) ) ) ;
701+ assert ! ( matches!(
702+ kargs. add_or_modify( & param( "a=3" ) ) ,
703+ Action :: Modified
704+ ) ) ;
690705 let mut iter = kargs. iter ( ) ;
691706 assert_eq ! ( iter. next( ) , Some ( param( "a=3" ) ) ) ;
692707 assert_eq ! ( iter. next( ) , None ) ;
0 commit comments