@@ -892,6 +892,81 @@ pub fn region_from_i32(value: i32) -> Result<inferadb_ledger_types::Region, Stat
892892 inferadb_ledger_types:: Region :: try_from ( proto_region)
893893}
894894
895+ // =============================================================================
896+ // UserStatus conversions
897+ // =============================================================================
898+
899+ impl From < inferadb_ledger_types:: UserStatus > for proto:: UserStatus {
900+ fn from ( status : inferadb_ledger_types:: UserStatus ) -> Self {
901+ use inferadb_ledger_types:: UserStatus as D ;
902+ match status {
903+ D :: Active => proto:: UserStatus :: Active ,
904+ D :: PendingOrg => proto:: UserStatus :: PendingOrg ,
905+ D :: Suspended => proto:: UserStatus :: Suspended ,
906+ D :: Deleting => proto:: UserStatus :: Deleting ,
907+ D :: Deleted => proto:: UserStatus :: Deleted ,
908+ }
909+ }
910+ }
911+
912+ impl TryFrom < proto:: UserStatus > for inferadb_ledger_types:: UserStatus {
913+ type Error = Status ;
914+
915+ fn try_from ( proto : proto:: UserStatus ) -> Result < Self , Status > {
916+ use inferadb_ledger_types:: UserStatus as D ;
917+ match proto {
918+ proto:: UserStatus :: Unspecified => {
919+ Err ( Status :: invalid_argument ( "user status must be specified" ) )
920+ } ,
921+ proto:: UserStatus :: Active => Ok ( D :: Active ) ,
922+ proto:: UserStatus :: PendingOrg => Ok ( D :: PendingOrg ) ,
923+ proto:: UserStatus :: Suspended => Ok ( D :: Suspended ) ,
924+ proto:: UserStatus :: Deleting => Ok ( D :: Deleting ) ,
925+ proto:: UserStatus :: Deleted => Ok ( D :: Deleted ) ,
926+ }
927+ }
928+ }
929+
930+ /// Convert a raw i32 user status value to a domain `UserStatus`.
931+ pub fn user_status_from_i32 ( value : i32 ) -> Result < inferadb_ledger_types:: UserStatus , Status > {
932+ let proto_status = proto:: UserStatus :: try_from ( value)
933+ . map_err ( |_| Status :: invalid_argument ( format ! ( "unknown user status value: {value}" ) ) ) ?;
934+ inferadb_ledger_types:: UserStatus :: try_from ( proto_status)
935+ }
936+
937+ // =============================================================================
938+ // UserRole conversions
939+ // =============================================================================
940+
941+ impl From < inferadb_ledger_types:: UserRole > for proto:: UserRole {
942+ fn from ( role : inferadb_ledger_types:: UserRole ) -> Self {
943+ use inferadb_ledger_types:: UserRole as D ;
944+ match role {
945+ D :: User => proto:: UserRole :: User ,
946+ D :: Admin => proto:: UserRole :: Admin ,
947+ }
948+ }
949+ }
950+
951+ impl TryFrom < proto:: UserRole > for inferadb_ledger_types:: UserRole {
952+ type Error = Status ;
953+
954+ fn try_from ( proto : proto:: UserRole ) -> Result < Self , Status > {
955+ use inferadb_ledger_types:: UserRole as D ;
956+ match proto {
957+ proto:: UserRole :: Unspecified | proto:: UserRole :: User => Ok ( D :: User ) ,
958+ proto:: UserRole :: Admin => Ok ( D :: Admin ) ,
959+ }
960+ }
961+ }
962+
963+ /// Convert a raw i32 user role value to a domain `UserRole`.
964+ pub fn user_role_from_i32 ( value : i32 ) -> Result < inferadb_ledger_types:: UserRole , Status > {
965+ let proto_role = proto:: UserRole :: try_from ( value)
966+ . map_err ( |_| Status :: invalid_argument ( format ! ( "unknown user role value: {value}" ) ) ) ?;
967+ inferadb_ledger_types:: UserRole :: try_from ( proto_role)
968+ }
969+
895970#[ cfg( test) ]
896971mod tests {
897972 #![ allow( clippy:: unwrap_used, clippy:: expect_used, clippy:: panic, clippy:: disallowed_methods) ]
@@ -2172,4 +2247,51 @@ mod tests {
21722247 }
21732248 }
21742249 }
2250+
2251+ // -------------------------------------------------------------------------
2252+ // UserStatus / UserRole conversions
2253+ // -------------------------------------------------------------------------
2254+
2255+ #[ test]
2256+ fn user_status_roundtrip ( ) {
2257+ use inferadb_ledger_types:: UserStatus ;
2258+ let statuses = [
2259+ UserStatus :: Active ,
2260+ UserStatus :: PendingOrg ,
2261+ UserStatus :: Suspended ,
2262+ UserStatus :: Deleting ,
2263+ UserStatus :: Deleted ,
2264+ ] ;
2265+ for status in statuses {
2266+ let proto_val: proto:: UserStatus = status. into ( ) ;
2267+ let i32_val: i32 = proto_val. into ( ) ;
2268+ let back = user_status_from_i32 ( i32_val) . expect ( "valid status" ) ;
2269+ assert_eq ! ( status, back, "roundtrip failed for {status:?}" ) ;
2270+ }
2271+ }
2272+
2273+ #[ test]
2274+ fn user_role_roundtrip ( ) {
2275+ use inferadb_ledger_types:: UserRole ;
2276+ let roles = [ UserRole :: User , UserRole :: Admin ] ;
2277+ for role in roles {
2278+ let proto_val: proto:: UserRole = role. into ( ) ;
2279+ let i32_val: i32 = proto_val. into ( ) ;
2280+ let back = user_role_from_i32 ( i32_val) . expect ( "valid role" ) ;
2281+ assert_eq ! ( role, back, "roundtrip failed for {role:?}" ) ;
2282+ }
2283+ }
2284+
2285+ #[ test]
2286+ fn user_status_unspecified_rejected ( ) {
2287+ let result = user_status_from_i32 ( 0 ) ; // 0 = Unspecified
2288+ assert ! ( result. is_err( ) ) ;
2289+ }
2290+
2291+ #[ test]
2292+ fn user_role_unspecified_treated_as_user ( ) {
2293+ use inferadb_ledger_types:: UserRole ;
2294+ let result = user_role_from_i32 ( 0 ) ; // 0 = Unspecified
2295+ assert_eq ! ( result. expect( "unspecified should map to User" ) , UserRole :: User ) ;
2296+ }
21752297}
0 commit comments