diff options
| author | Polesznyák Márk László <116908301+pml68@users.noreply.github.com> | 2025-03-23 02:49:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-23 02:49:57 +0100 |
| commit | 3076889c00116b22f022792471253e7188c6e93e (patch) | |
| tree | bff0cda7a9152e9f94d3176bbf5acaf879394f5f /src/values/alignment.rs | |
| parent | feat: update to Rust 2024 (diff) | |
| parent | feat: finish `ApplyOptions` impls (diff) | |
| download | iced-builder-3076889c00116b22f022792471253e7188c6e93e.tar.gz | |
Merge pull request #7 from pml68/feat/options-backend
Options backend done (for now)
Diffstat (limited to '')
| -rw-r--r-- | src/values/alignment.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/values/alignment.rs b/src/values/alignment.rs new file mode 100644 index 0000000..7e9fcab --- /dev/null +++ b/src/values/alignment.rs @@ -0,0 +1,65 @@ +use iced::Alignment; + +use super::Value; + +#[derive(Debug, thiserror::Error, Clone, PartialEq)] +pub enum ParseAlignmentError { + #[error("cannot parse rotation from empty string")] + Empty, + #[error("invalid variant")] + InvalidVariant, +} + +impl Value for Alignment { + type Err = ParseAlignmentError; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + let s = s.trim(); + + if s.is_empty() { + return Err(ParseAlignmentError::Empty); + } + + match s { + "start" => Ok(Self::Start), + "center" => Ok(Self::Center), + "end" => Ok(Self::End), + _ => Err(ParseAlignmentError::InvalidVariant), + } + } + + fn to_string(&self) -> String { + match self { + Self::Start => String::from("start"), + Self::Center => String::from("center"), + Self::End => String::from("end"), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_parse_with_spaces() { + assert_eq!(Alignment::from_str(" start"), Ok(Alignment::Start)); + + assert_eq!(Alignment::from_str(" center "), Ok(Alignment::Center)); + + assert_eq!(Alignment::from_str("end "), Ok(Alignment::End)) + } + + #[test] + fn cant_parse_invalid_variant() { + assert_eq!( + Alignment::from_str("middle"), + Err(ParseAlignmentError::InvalidVariant) + ) + } + + #[test] + fn cant_parse_empty_string() { + assert_eq!(Alignment::from_str(" "), Err(ParseAlignmentError::Empty)) + } +} |
