reactive_graph_table_model/types/
component.rs

1use table_to_html::HtmlTable;
2use tabled::Table;
3use tabled::Tabled;
4use tabled::settings::Modify;
5use tabled::settings::Style;
6use tabled::settings::Width;
7use tabled::settings::object::Columns;
8use tabled::settings::object::Segment;
9
10use crate::container::DefaultTableContainer;
11use crate::container::TableInlineFormat;
12use crate::container::TableInlineFormatSetter;
13use crate::container::TableOptions;
14use crate::styles::modern_inline::modern_inline;
15use crate::types::extension::Extension;
16use crate::types::extension::Extensions;
17use crate::types::extension::display_extensions_html_inline;
18use crate::types::extension::display_extensions_inline_str;
19use crate::types::properties::PropertyType;
20use crate::types::properties::PropertyTypes;
21use crate::types::properties::display_property_types_html_inline;
22use crate::types::properties::display_property_types_inline_str;
23use reactive_graph_graph::NamespacedTypeGetter;
24
25#[derive(Clone, Debug, Tabled)]
26pub struct Component {
27    /// The namespace of the component.
28    pub namespace: String,
29
30    /// The name of the component.
31    pub name: String,
32
33    /// Textual description of the component.
34    // #[tabled(skip)]
35    pub description: String,
36
37    /// The property types.
38    #[tabled(display("display_property_types", self))]
39    pub properties: Vec<PropertyType>,
40
41    /// The extensions.
42    #[tabled(display("display_extensions", self))]
43    pub extensions: Vec<Extension>,
44
45    #[tabled(skip)]
46    inline_format: TableInlineFormat,
47}
48
49fn display_property_types(properties: &[PropertyType], component: &Component) -> String {
50    match component.inline_format {
51        TableInlineFormat::Table => display_property_types_inline_str(properties),
52        TableInlineFormat::Html => display_property_types_html_inline(properties),
53    }
54}
55
56fn display_extensions(extensions: &[Extension], component: &Component) -> String {
57    match component.inline_format {
58        TableInlineFormat::Table => display_extensions_inline_str(extensions),
59        TableInlineFormat::Html => display_extensions_html_inline(extensions),
60    }
61}
62
63impl TableInlineFormatSetter for Component {
64    fn set_table_inline_format(&mut self, table_inline_format: TableInlineFormat) {
65        self.inline_format = table_inline_format;
66    }
67}
68
69impl From<reactive_graph_graph::Component> for Component {
70    fn from(component: reactive_graph_graph::Component) -> Self {
71        Component {
72            namespace: component.namespace(),
73            name: component.type_name(),
74            description: component.description,
75            properties: PropertyTypes::from(component.properties).0,
76            extensions: Extensions::from(component.extensions).0,
77            inline_format: Default::default(),
78        }
79    }
80}
81
82#[derive(Clone, Debug, Tabled)]
83pub struct ComponentTypeId {
84    /// The namespace of the component.
85    pub namespace: String,
86
87    /// The name of the component.
88    pub name: String,
89
90    #[tabled(skip)]
91    inline_format: TableInlineFormat,
92}
93
94impl TableInlineFormatSetter for ComponentTypeId {
95    fn set_table_inline_format(&mut self, table_inline_format: TableInlineFormat) {
96        self.inline_format = table_inline_format;
97    }
98}
99
100impl From<reactive_graph_graph::ComponentTypeId> for ComponentTypeId {
101    fn from(ty: reactive_graph_graph::ComponentTypeId) -> Self {
102        ComponentTypeId {
103            namespace: ty.namespace(),
104            name: ty.type_name(),
105            inline_format: Default::default(),
106        }
107    }
108}
109
110pub fn display_component_type_ids_inline_str(tys: &[ComponentTypeId]) -> String {
111    if tys.is_empty() {
112        String::new()
113    } else {
114        display_component_type_ids_inline(tys).to_string()
115    }
116}
117
118pub fn display_component_type_ids_inline(tys: &[ComponentTypeId]) -> Table {
119    let tys = tys.to_vec();
120    Table::new(tys)
121        .with(modern_inline())
122        .with(Modify::new(Columns::new(0..1)).with(Width::increase(15)))
123        .with(Modify::new(Columns::new(1..2)).with(Width::increase(15)))
124        .to_owned()
125}
126
127pub fn display_component_type_ids_html_inline(tys: &[ComponentTypeId]) -> String {
128    let tys = tys.to_vec();
129    if tys.is_empty() {
130        return String::new();
131    }
132    HtmlTable::with_header(Vec::<Vec<String>>::from(Table::builder(&tys)))
133        .to_string()
134        .split_whitespace()
135        .collect()
136}
137
138#[derive(Clone, Debug, Default)]
139pub struct ComponentTypeIds(pub Vec<ComponentTypeId>);
140
141impl From<reactive_graph_graph::ComponentTypeIds> for ComponentTypeIds {
142    fn from(tys: reactive_graph_graph::ComponentTypeIds) -> Self {
143        ComponentTypeIds(tys.into_iter().map(|ty| ty.into()).collect())
144    }
145}
146
147pub type ComponentTableContainer = DefaultTableContainer<reactive_graph_graph::Component, Component, ComponentTypeIdsTableOptions>;
148
149pub struct ComponentsTableOptions;
150
151impl TableOptions for ComponentsTableOptions {
152    fn options(table: &mut Table) -> &mut Table {
153        table.with(Style::extended()).with(
154            Modify::new(Segment::new(0.., 0..3))
155                .with(Width::increase(22))
156                .with(Width::increase(22))
157                .with(Width::wrap(40).keep_words(true)),
158        )
159    }
160}
161
162pub type ComponentTypeIdTableContainer = DefaultTableContainer<reactive_graph_graph::ComponentTypeId, ComponentTypeId, ComponentTypeIdsTableOptions>;
163
164pub struct ComponentTypeIdsTableOptions;
165
166impl TableOptions for ComponentTypeIdsTableOptions {
167    fn options(table: &mut Table) -> &mut Table {
168        table.with(Style::extended())
169    }
170}