summaryrefslogtreecommitdiff
path: root/src/values
diff options
context:
space:
mode:
authorpml68 <contact@pml68.dev>2025-03-01 02:29:38 +0100
committerpml68 <contact@pml68.dev>2025-03-01 02:29:38 +0100
commit32e3658b10ab70e1300ac1aa8b413765b411acbe (patch)
tree7aa33463f67a748e4b3838c40d69027c8b003624 /src/values
parentrefactor: make config an `Arc<Config>` in `App` (diff)
downloadiced-builder-32e3658b10ab70e1300ac1aa8b413765b411acbe.tar.gz
feat: impl `Value` for `Pixels` and `Alignment`
Diffstat (limited to '')
-rw-r--r--src/values.rs2
-rw-r--r--src/values/alignment.rs65
-rw-r--r--src/values/pixels.rs18
3 files changed, 85 insertions, 0 deletions
diff --git a/src/values.rs b/src/values.rs
index 31ee7bb..a788dab 100644
--- a/src/values.rs
+++ b/src/values.rs
@@ -1,5 +1,7 @@
+mod alignment;
mod length;
mod padding;
+mod pixels;
mod rotation;
pub trait Value: Sized {
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))
+ }
+}
diff --git a/src/values/pixels.rs b/src/values/pixels.rs
new file mode 100644
index 0000000..b2b0047
--- /dev/null
+++ b/src/values/pixels.rs
@@ -0,0 +1,18 @@
+use std::num::ParseFloatError;
+use std::str::FromStr;
+
+use iced::Pixels;
+
+use super::Value;
+
+impl Value for Pixels {
+ type Err = ParseFloatError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Ok(Pixels(f32::from_str(s.trim())?))
+ }
+
+ fn to_string(&self) -> String {
+ self.0.to_string()
+ }
+}