diff options
| author | pml68 <contact@pml68.dev> | 2025-02-24 19:08:32 +0100 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-02-24 19:08:32 +0100 |
| commit | a806922d47bb7ea3e401a99ed2c918c4ab745973 (patch) | |
| tree | 357a644de1378238664ca49dbcdb7b82c6b54af3 | |
| parent | feat: start working on options backend (`ApplyOptions` trait) (diff) | |
| download | iced-builder-a806922d47bb7ea3e401a99ed2c918c4ab745973.tar.gz | |
feat: create separate `ValueFromStr` struct
Diffstat (limited to '')
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/options.rs | 30 | ||||
| -rwxr-xr-x | src/types/rendered_element.rs | 4 | ||||
| -rw-r--r-- | src/values.rs | 7 | ||||
| -rw-r--r-- | src/values/padding.rs | 37 |
5 files changed, 52 insertions, 27 deletions
diff --git a/src/main.rs b/src/main.rs index 6c4e8d6..20383f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod options; mod panes; mod theme; mod types; +mod values; mod widget; use std::path::PathBuf; diff --git a/src/options.rs b/src/options.rs index 9514dcb..5a3e1fb 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,41 +1,21 @@ use std::collections::BTreeMap; -use std::str::FromStr; use iced::Padding; +#[allow(unused_imports)] use iced::widget::{Button, Column, Container, Image, Row, Svg, Text}; +use crate::values::ValueFromStr; + pub trait ApplyOptions { fn apply_options(self, options: BTreeMap<String, Option<String>>) -> Self; } -impl<'a, Message> ApplyOptions for Button<'a, Message> { +impl<Message> ApplyOptions for Button<'_, Message> { fn apply_options(self, options: BTreeMap<String, Option<String>>) -> Self { let mut button = self; if let Some(padding) = options.get("padding").expect("padding key") { - let padding: Padding = padding - .strip_prefix('[') - .and_then(|s| s.strip_suffix(']')) - .and_then(|s| { - Some( - s.split(',') - .map(|n| f32::from_str(n).unwrap()) - .collect::<Vec<_>>(), - ) - }) - .and_then(|s| { - if s.len() == 4 { - Some(Padding { - top: s[0], - right: s[1], - bottom: s[2], - left: s[3], - }) - } else { - None - } - }) - .unwrap(); + let padding: Padding = Padding::value_from_str(padding).unwrap(); button = button.padding(padding); } diff --git a/src/types/rendered_element.rs b/src/types/rendered_element.rs index 94c55a0..f4ae6f3 100755 --- a/src/types/rendered_element.rs +++ b/src/types/rendered_element.rs @@ -318,14 +318,14 @@ impl<'a> From<RenderedElement> for Element<'a, Message> { let content: Element<'a, Message> = match copy.name { ElementName::Text(s) => { - if &s == "" { + if s.is_empty() { widget::text("New Text").into() } else { widget::text(s).into() } } ElementName::Button(s) => { - if &s == "" { + if s.is_empty() { widget::button(widget::text("New Button")) .apply_options(copy.options) .into() diff --git a/src/values.rs b/src/values.rs new file mode 100644 index 0000000..0ac17cb --- /dev/null +++ b/src/values.rs @@ -0,0 +1,7 @@ +mod padding; + +pub trait ValueFromStr: Sized { + type Err; + + fn value_from_str(s: &str) -> Result<Self, Self::Err>; +} diff --git a/src/values/padding.rs b/src/values/padding.rs new file mode 100644 index 0000000..3cec7a7 --- /dev/null +++ b/src/values/padding.rs @@ -0,0 +1,37 @@ +use std::str::FromStr; + +use iced::Padding; + +use super::ValueFromStr; + +#[derive(Debug)] +pub enum PaddingError { + Nah, +} + +impl ValueFromStr for Padding { + type Err = PaddingError; + + fn value_from_str(s: &str) -> Result<Self, Self::Err> { + s.strip_prefix('[') + .and_then(|s| s.strip_suffix(']')) + .map(|s| { + s.split(',') + .map(|n| f32::from_str(n).unwrap()) + .collect::<Vec<_>>() + }) + .and_then(|s| { + if s.len() == 4 { + Some(Padding { + top: s[0], + right: s[1], + bottom: s[2], + left: s[3], + }) + } else { + None + } + }) + .ok_or(PaddingError::Nah) + } +} |
