ferroui_controls/
column_definitions.rs1use crate::{ColumnDefinition, DefinitionList, GridLength};
2use ferroui_base::utilities::FormatError;
3use ferroui_base::Ref;
4use std::fmt;
5use std::ops::Deref;
6use std::str::FromStr;
7
8#[derive(Clone, PartialEq, Default)]
13pub struct ColumnDefinitions {
14 base: DefinitionList<ColumnDefinition>,
15}
16
17impl ColumnDefinitions {
18 pub fn new() -> Self {
20 Self {
21 base: DefinitionList::new(),
22 }
23 }
24
25 pub fn from_items(items: impl IntoIterator<Item = Ref<ColumnDefinition>>) -> Self {
27 let result = Self::new();
28 result.add_range(items);
29 result
30 }
31
32 pub fn parse(s: &str) -> Result<Self, FormatError> {
34 let lengths = GridLength::parse_lengths(s)?;
35 Ok(Self::from_items(lengths.into_iter().map(ColumnDefinition::with_width)))
36 }
37}
38
39impl Deref for ColumnDefinitions {
40 type Target = DefinitionList<ColumnDefinition>;
41
42 #[inline]
43 fn deref(&self) -> &Self::Target {
44 &self.base
45 }
46}
47
48impl fmt::Display for ColumnDefinitions {
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.width().fmt(f)?;
55 }
56 Ok(())
57 }
58}
59
60impl FromStr for ColumnDefinitions {
61 type Err = FormatError;
62
63 fn from_str(s: &str) -> Result<Self, Self::Err> {
64 Self::parse(s)
65 }
66}