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::extension::Extension;
6use crate::types::extension::Extensions;
7use crate::types::extension::display_extensions_html_inline;
8use crate::types::extension::display_extensions_inline_str;
9use reactive_graph_graph::DataType;
10use reactive_graph_graph::Mutability;
11use reactive_graph_graph::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
63// impl From<PropertyType> for reactive_graph_graph::PropertyType {
64//     fn from(property_type: PropertyType) -> Self {
65//         let extensions: Extensions = property_type.extensions.iter().map(|e| e.).collect();
66//         reactive_graph_graph::PropertyType {
67//             name: property_type.name,
68//             description: property_type.description,
69//             data_type: property_type.data_type.into(),
70//             socket_type: property_type.socket_type.into(),
71//             mutability: property_type.mutability.into(),
72//             extensions, // : Extensions(property_type.extensions).into()
73//         }
74//     }
75// }
76
77impl From<reactive_graph_graph::PropertyType> for PropertyType {
78    fn from(property_type: reactive_graph_graph::PropertyType) -> Self {
79        PropertyType {
80            name: property_type.name,
81            description: property_type.description,
82            data_type: property_type.data_type.into(),
83            socket_type: property_type.socket_type.into(),
84            mutability: property_type.mutability.into(),
85            extensions: Extensions::from(property_type.extensions).into(),
86            inline_format: Default::default(),
87        }
88    }
89}
90
91pub fn display_property_types_inline_str(property_types: &[PropertyType]) -> String {
92    if property_types.is_empty() {
93        String::new()
94    } else {
95        display_property_types_inline(property_types).to_string()
96    }
97}
98
99pub fn display_property_types_inline(property_types: &[PropertyType]) -> Table {
100    let property_types = property_types.to_vec();
101    Table::new(property_types)
102        .with(modern_inline())
103        .with(Modify::new(Columns::new(0..1)).with(Width::increase(35)))
104        .with(Modify::new(Columns::new(1..2)).with(Width::increase(9)))
105        .with(Modify::new(Columns::new(2..3)).with(Width::increase(11)))
106        .with(Modify::new(Columns::new(3..4)).with(Width::increase(10)))
107        .to_owned()
108}
109
110pub fn display_property_types_html_inline(property_types: &[PropertyType]) -> String {
111    let property_types = property_types.to_vec();
112    if property_types.is_empty() {
113        return String::new();
114    }
115    HtmlTable::with_header(Vec::<Vec<String>>::from(Table::builder(&property_types)))
116        .to_string()
117        .split_whitespace()
118        .collect()
119}
120
121#[derive(Clone, Debug)]
122pub struct PropertyTypes(pub Vec<PropertyType>);
123
124// impl From<PropertyTypes> for reactive_graph_graph::PropertyTypes {
125//     fn from(property_types: PropertyTypes) -> Self {
126//         property_types.0.into_iter().map(|property_type| property_type.into()).collect()
127//     }
128// }
129
130impl From<reactive_graph_graph::PropertyTypes> for PropertyTypes {
131    fn from(property_types: reactive_graph_graph::PropertyTypes) -> Self {
132        PropertyTypes(property_types.into_iter().map(|(_property_name, property_type)| property_type.into()).collect())
133    }
134}
135
136impl fmt::Display for PropertyTypes {
137    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
138        writeln!(f)
139    }
140}
141
142pub struct PropertyTypesTableOptions;
143
144impl TableOptions for PropertyTypesTableOptions {
145    fn options(table: &mut Table) -> &mut Table {
146        table.with(Style::extended())
147    }
148}