diff options
| author | pml68 <contact@pml68.dev> | 2025-03-04 00:01:42 +0100 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-03-04 00:01:42 +0100 |
| commit | 107998a3a53c1f4c008837a0099fd22bddfb1802 (patch) | |
| tree | 026b4e661db8767c9c2479f40a86ad0a3e0c8adf /src/values/content_fit.rs | |
| parent | refactor: move some `assert_eq`s (diff) | |
| download | iced-builder-107998a3a53c1f4c008837a0099fd22bddfb1802.tar.gz | |
feat: impl `Value` for `LineHeight` and `ContentFit` (all done)
Diffstat (limited to 'src/values/content_fit.rs')
| -rw-r--r-- | src/values/content_fit.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/values/content_fit.rs b/src/values/content_fit.rs new file mode 100644 index 0000000..053431f --- /dev/null +++ b/src/values/content_fit.rs @@ -0,0 +1,69 @@ +use iced::ContentFit; + +use super::Value; + +#[derive(Debug, thiserror::Error, Clone, PartialEq)] +pub enum ParseContentFitError { + #[error("invalid variant")] + InvalidVariant, +} + +impl Value for ContentFit { + type Err = ParseContentFitError; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + let s = s.trim(); + + if s.is_empty() { + Ok(Self::default()) + } else { + match s { + "fill" => Ok(Self::Fill), + "none" => Ok(Self::None), + "cover" => Ok(Self::Cover), + "contain" => Ok(Self::Contain), + "scale_down" => Ok(Self::ScaleDown), + _ => Err(ParseContentFitError::InvalidVariant), + } + } + } + + fn to_string(&self) -> String { + match self { + Self::Fill => String::from("fill"), + Self::None => String::from("none"), + Self::Cover => String::from("cover"), + Self::Contain => String::from("contain"), + Self::ScaleDown => String::from("scale_down"), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_parse_with_spaces() { + assert_eq!(ContentFit::from_str(" fill"), Ok(ContentFit::Fill)); + + assert_eq!(ContentFit::from_str(" none "), Ok(ContentFit::None)); + + assert_eq!(ContentFit::from_str("cover "), Ok(ContentFit::Cover)); + + assert_eq!(ContentFit::from_str("contain"), Ok(ContentFit::Contain)); + + assert_eq!( + ContentFit::from_str("scale_down"), + Ok(ContentFit::ScaleDown) + ) + } + + #[test] + fn cant_parse_invalid_variant() { + assert_eq!( + ContentFit::from_str("clip"), + Err(ParseContentFitError::InvalidVariant) + ) + } +} |
