-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rust] Enhance enum supporting fromstr and display and into (#1020)
* [Rust] impl FromStr for generated enum items * [Rust] impl Display for generate enum items * [Rust] impl Into for generated enum items * [Rust] reformat code according to checkstyle failure report * [Rust] split code in generateEnum to adress CI checkstyle method too log failure * [Rust] reformat code according to checkstyle failure * [Rust] fix CodeQL warning by removing unused argument * [Rust] fix CodeQL warning by removing unused argument
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use examples_baseline::{ | ||
boost_type::BoostType, | ||
}; | ||
|
||
#[test] | ||
fn test_boost_type_from_str() -> Result<(), ()> { | ||
assert_eq!("TURBO".parse::<BoostType>()?, BoostType::TURBO, "Parse \"TURBO\" as BoostType"); | ||
assert_eq!("SUPERCHARGER".parse::<BoostType>()?, BoostType::SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType"); | ||
assert_eq!("NITROUS".parse::<BoostType>()?, BoostType::NITROUS, "Parse \"NITROUS\" as BoostType"); | ||
assert_eq!("KERS".parse::<BoostType>()?, BoostType::KERS, "Parse \"KERS\" as BoostType"); | ||
|
||
assert_eq!("Turbo".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Turbo\" as BoostType"); | ||
assert_eq!("Supercharger".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Supercharger\" as BoostType"); | ||
assert_eq!("Nitrous".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Nitrous\" as BoostType"); | ||
assert_eq!("Kers".parse::<BoostType>()?, BoostType::NullVal, "Parse \"Kers\" as BoostType"); | ||
|
||
assert_eq!("AA".parse::<BoostType>()?, BoostType::NullVal, "Parse \"AA\" as BoostType"); | ||
assert_eq!("".parse::<BoostType>().unwrap(), BoostType::NullVal, "Parse \"\" as BoostType"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use examples_baseline::{ | ||
boost_type::BoostType, | ||
}; | ||
|
||
#[test] | ||
fn test_boost_type_from_str() -> Result<(), ()> { | ||
assert_eq!(<BoostType as Into<u8>>::into(BoostType::TURBO), 84_u8, "BoostType::TURBO into value"); | ||
assert_eq!(<BoostType as Into<u8>>::into(BoostType::SUPERCHARGER), 83_u8, "BoostType::SUPERCHARGER into value"); | ||
assert_eq!(<BoostType as Into<u8>>::into(BoostType::NITROUS), 78_u8, "BoostType::NITROUS into value"); | ||
assert_eq!(<BoostType as Into<u8>>::into(BoostType::KERS), 75_u8, "BoostType::KERS into value"); | ||
assert_eq!(<BoostType as Into<u8>>::into(BoostType::NullVal), 0_u8, "BoostType::NullVal into value"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use examples_baseline::{ | ||
boost_type::BoostType, | ||
}; | ||
|
||
#[test] | ||
fn test_boost_type_from_str() -> Result<(), ()> { | ||
assert_eq!(format!("{}", BoostType::TURBO), "TURBO", "Display \"TURBO\""); | ||
assert_eq!(format!("{}", BoostType::SUPERCHARGER), "SUPERCHARGER", "Display \"SUPERCHARGER\""); | ||
assert_eq!(format!("{}", BoostType::NITROUS), "NITROUS", "Display \"NITROUS\""); | ||
assert_eq!(format!("{}", BoostType::KERS), "KERS", "Display \"KERS\""); | ||
assert_eq!(format!("{}", BoostType::NullVal), "NullVal", "Display \"NullVal\""); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters