Skip to main content

ferroui_controls/
row_definitions.rs

1use crate::{DefinitionList, GridLength, RowDefinition};
2use ferroui_base::utilities::FormatError;
3use ferroui_base::Ref;
4use std::fmt;
5use std::ops::Deref;
6use std::str::FromStr;
7
8/// A collection of [`RowDefinition`]s.
9///
10/// The value is a shared handle: clones refer to the same collection, and
11/// handles compare by identity.
12#[derive(Clone, PartialEq, Default)]
13pub struct RowDefinitions {
14    base: DefinitionList<RowDefinition>,
15}
16
17impl RowDefinitions {
18    /// Creates an empty collection.
19    pub fn new() -> Self {
20        Self {
21            base: DefinitionList::new(),
22        }
23    }
24
25    /// Creates a collection with the initial items.
26    pub fn from_items(items: impl IntoIterator<Item = Ref<RowDefinition>>) -> Self {
27        let result = Self::new();
28        result.add_range(items);
29        result
30    }
31
32    /// Parses a string representation of a row definitions collection.
33    pub fn parse(s: &str) -> Result<Self, FormatError> {
34        let lengths = GridLength::parse_lengths(s)?;
35        Ok(Self::from_items(lengths.into_iter().map(RowDefinition::with_height)))
36    }
37}
38
39impl Deref for RowDefinitions {
40    type Target = DefinitionList<RowDefinition>;
41
42    #[inline]
43    fn deref(&self) -> &Self::Target {
44        &self.base
45    }
46}
47
48impl fmt::Display for RowDefinitions {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        for (index, definition) in self.snapshot().iter().enumerate() {
51            if index > 0 {
52                f.write_str(",")?;
53            }
54            definition.height().fmt(f)?;
55        }
56        Ok(())
57    }
58}
59
60impl FromStr for RowDefinitions {
61    type Err = FormatError;
62
63    fn from_str(s: &str) -> Result<Self, Self::Err> {
64        Self::parse(s)
65    }
66}