49 lines
1.1 KiB
Rust
49 lines
1.1 KiB
Rust
|
|
||
|
default_derive!{
|
||
|
pub struct GalaxyClusterSource {
|
||
|
name: String
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl TryFrom<&str> for GalaxyClusterSource {
|
||
|
type Error = &'static str;
|
||
|
|
||
|
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||
|
if value.len() > 0 && value.len() <= 255 {
|
||
|
Ok(GalaxyClusterSource { name: value.to_string() })
|
||
|
}
|
||
|
else {
|
||
|
Err("Failed to parse GalaxyClusterSource")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Into<String> for GalaxyClusterSource {
|
||
|
fn into(self) -> String {
|
||
|
self.name
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn empty() {
|
||
|
let id: Result<GalaxyClusterSource, _> = "".try_into();
|
||
|
assert!(id.is_err())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn oversized() {
|
||
|
let id: Result<GalaxyClusterSource, _> = format!("{:>256}", "Test").as_str().try_into();
|
||
|
assert!(id.is_err())
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn valid1() {
|
||
|
let id: Result<GalaxyClusterSource, _> = "version".try_into();
|
||
|
assert_eq!(id, Ok(GalaxyClusterSource { name: "version".to_string() }))
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn valid2() {
|
||
|
let id: Result<GalaxyClusterSource, _> = "1234".try_into();
|
||
|
assert_eq!(id, Ok(GalaxyClusterSource { name: "1234".to_string() }))
|
||
|
}
|