@@ -402,13 +402,14 @@ impl Serialize for SerializableBranch {
402402}
403403
404404impl < ' de > Deserialize < ' de > for SerializableBranch {
405- /// Deserialize from a plain string using the same logic as [`FromStr`] .
405+ /// Deserialize from a plain string, delegating to [`from_gitmodules`](GitmodulesConvert::from_gitmodules) .
406406 /// Accepts `"."`, `"current"`, `"current-in-super-project"`, `"superproject"`, or `"super"`
407- /// as [`CurrentInSuperproject`](SerializableBranch::CurrentInSuperproject); all other strings
408- /// become [`Name`](SerializableBranch::Name).
407+ /// as [`CurrentInSuperproject`](SerializableBranch::CurrentInSuperproject); all other
408+ /// non-empty, non-whitespace strings become [`Name`](SerializableBranch::Name).
409+ /// Empty or whitespace-only strings are rejected with a deserialization error.
409410 fn deserialize < D : Deserializer < ' de > > ( deserializer : D ) -> Result < Self , D :: Error > {
410411 let s = String :: deserialize ( deserializer) ?;
411- SerializableBranch :: from_str ( & s)
412+ SerializableBranch :: from_gitmodules ( & s)
412413 . map_err ( |_| serde:: de:: Error :: custom ( format ! ( "invalid branch value: {s}" ) ) )
413414 }
414415}
@@ -446,7 +447,10 @@ impl GitmodulesConvert for SerializableBranch {
446447 || options == "current-in-super-project"
447448 || options == "superproject"
448449 || options == "super"
449- || options == SerializableBranch :: current_in_superproject ( ) . unwrap_or_default ( )
450+ || SerializableBranch :: current_in_superproject ( )
451+ . ok ( )
452+ . as_deref ( )
453+ . map_or ( false , |cur| cur == options)
450454 {
451455 return Ok ( SerializableBranch :: CurrentInSuperproject ) ;
452456 }
@@ -464,6 +468,34 @@ impl GitmodulesConvert for SerializableBranch {
464468 }
465469}
466470
471+ #[ cfg( test) ]
472+ mod tests {
473+ use super :: SerializableBranch ;
474+
475+ #[ test]
476+ fn test_branch_deserialize_from_toml_rejects_empty_and_whitespace ( ) {
477+ // Empty string should be rejected
478+ let res_empty: Result < SerializableBranch , toml:: de:: Error > =
479+ toml:: from_str ( r#"branch = """"# ) ;
480+ assert ! ( res_empty. is_err( ) , "expected error for empty branch value" ) ;
481+ let err_empty = res_empty. unwrap_err ( ) . to_string ( ) ;
482+ assert ! (
483+ err_empty. contains( "invalid branch value" ) ,
484+ "error for empty branch value should contain context, got: {err_empty}"
485+ ) ;
486+
487+ // Whitespace-only string should be rejected
488+ let res_ws: Result < SerializableBranch , toml:: de:: Error > =
489+ toml:: from_str ( r#"branch = " ""# ) ;
490+ assert ! ( res_ws. is_err( ) , "expected error for whitespace-only branch value" ) ;
491+ let err_ws = res_ws. unwrap_err ( ) . to_string ( ) ;
492+ assert ! (
493+ err_ws. contains( "invalid branch value" ) ,
494+ "error for whitespace-only branch value should contain context, got: {err_ws}"
495+ ) ;
496+ }
497+ }
498+
467499impl TryFrom < Branch > for SerializableBranch {
468500 type Error = ( ) ;
469501
0 commit comments