reactive_graph_table_model/types/
properties.rs

1use crate::container::TableInlineFormat;
2use crate::container::TableInlineFormatSetter;
3use crate::container::TableOptions;
4use crate::styles::modern_inline::modern_inline;
5use crate::types::data_type::DataType;
6use crate::types::extension::Extension;
7use crate::types::extension::Extensions;
8use crate::types::extension::display_extensions_html_inline;
9use crate::types::extension::display_extensions_inline_str;
10use crate::types::mutability::Mutability;
11use crate::types::socket_type::SocketType;
12use std::fmt;
13use std::fmt::Formatter;
14use table_to_html::HtmlTable;
15use tabled::Table;
16use tabled::Tabled;
17use tabled::settings::Modify;
18use tabled::settings::Style;
19use tabled::settings::Width;
20use tabled::settings::object::Columns;
21
22#[derive(Clone, Debug, Tabled)]
23pub struct PropertyType {
24    /// The name of the property.
25    pub name: String,
26
27    /// Textual description of the property.
28    #[tabled(skip)]
29    pub description: String,
30
31    /// Specifies the data type of the property.
32    pub data_type: DataType,
33
34    /// Specifies the type of socket - either input socket or output socket or none.
35    pub socket_type: SocketType,
36
37    /// Specifies if the property is mutable.
38    pub mutability: Mutability,
39
40    /// Property specific extensions.
41    #[tabled(display("display_extensions", self))]
42    #[tabled(skip)]
43    pub extensions: Vec<Extension>,
44
45    #[tabled(skip)]
46    inline_format: TableInlineFormat,
47}
48
49#[allow(dead_code)]
50fn display_extensions(extensions: &[Extension], property_type: &PropertyType) -> String {
51    match property_type.inline_format {
52        TableInlineFormat::Table => display_extensions_inline_str(extensions),
53        TableInlineFormat::Html => display_extensions_html_inline(extensions),
54    }
55}
56
57impl TableInlineFormatSetter for PropertyType {
58    fn set_table_inline_format(&mut self, table_inline_format: TableInlineFormat) {
59        self.inline_format = table_inline_format;
60    }
61}
62
63impl From<PropertyType> for reactive_graph_graph::PropertyType {
64    fn from(property_type: PropertyType) -> Self {
65        reactive_graph_graph::PropertyType {
66            name: property_type.name,
67            description: property_type.description,
68            data_type: property_type.data_type.into(),
69            socket_type: property_type.socket_type.into(),
70            mutability: property_type.mutability.into(),
71            extensions: Extensions(property_type.extensions).into(),
72        }
73    }
74}
75
76impl From<reactive_graph_graph::PropertyType> for PropertyType {
77    fn from(property_type: reactive_graph_graph::PropertyType) -> Self {
78        PropertyType {
79            name: property_type.name,
80            description: property_type.description,
81            data_type: property_type.data_type.into(),
82            socket_type: property_type.socket_type.into(),
83            mutability: property_type.mutability.into(),
84            extensions: Extensions::from(property_type.extensions).into(),
85            inline_format: Default::default(),
86        }
87    }
88}
89
90pub fn display_property_types_inline_str(property_types: &[PropertyType]) -> String {
91    if property_types.is_empty() {
92        String::new()
93    } else {
94        display_property_types_inline(property_types).to_string()
95    }
96}
97
98pub fn display_property_types_inline(property_types: &[PropertyType]) -> Table {
99    let property_types = property_types.to_vec();
100    Table::new(property_types)
101        .with(modern_inline())
102        .with(Modify::new(Columns::new(0..1)).with(Width::increase(35)))
103        .with(Modify::new(Columns::new(1..2)).with(Width::increase(9)))
104        .with(Modify::new(Columns::new(2..3)).with(Width::increase(11)))
105        .with(Modify::new(Columns::new(3..4)).with(Width::increase(10)))
106        .to_owned()
107}
108
109pub fn display_property_types_html_inline(property_types: &[PropertyType]) -> String {
110    let property_types = property_types.to_vec();
111    if property_types.is_empty() {
112        return String::new();
113    }
114    HtmlTable::with_header(Vec::<Vec<String>>::from(Table::builder(&property_types)))
115        .to_string()
116        .split_whitespace()
117        .collect()
118}
119
120#[derive(Clone, Debug)]
121pub struct PropertyTypes(pub Vec<PropertyType>);
122
123impl From<PropertyTypes> for reactive_graph_graph::PropertyTypes {
124    fn from(property_types: PropertyTypes) -> Self {
125        property_types.0.into_iter().map(|property_type| property_type.into()).collect()
126    }
127}
128
129impl From<reactive_graph_graph::PropertyTypes> for PropertyTypes {
130    fn from(property_types: reactive_graph_graph::PropertyTypes) -> Self {
131        PropertyTypes(property_types.into_iter().map(|(_property_name, property_type)| property_type.into()).collect())
132    }
133}
134
135impl fmt::Display for PropertyTypes {
136    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
137        writeln!(f)
138    }
139}
140
141pub struct PropertyTypesTableOptions;
142
143impl TableOptions for PropertyTypesTableOptions {
144    fn options(table: &mut Table) -> &mut Table {
145        table.with(Style::extended())
146    }
147}