reactive_graph_table_model/instances/
properties.rs

1use crate::container::TableInlineFormat;
2use crate::container::TableInlineFormatSetter;
3use crate::container::TableOptions;
4use crate::styles::modern_inline::modern_inline;
5use serde::Serialize;
6use serde_json::Value;
7use std::fmt;
8use std::fmt::Formatter;
9use table_to_html::HtmlTable;
10use tabled::Table;
11use tabled::Tabled;
12use tabled::settings::Style;
13
14#[derive(Clone, Debug, Serialize, Tabled)]
15pub struct PropertyInstance {
16    /// The name of the property.
17    pub name: String,
18
19    /// The value of the property
20    pub value: Value,
21
22    #[tabled(skip)]
23    #[serde(skip)]
24    inline_format: TableInlineFormat,
25}
26
27impl PropertyInstance {
28    pub fn new(name: String, value: Value) -> Self {
29        Self {
30            name,
31            value,
32            inline_format: Default::default(),
33        }
34    }
35}
36
37impl TableInlineFormatSetter for PropertyInstance {
38    fn set_table_inline_format(&mut self, table_inline_format: TableInlineFormat) {
39        self.inline_format = table_inline_format;
40    }
41}
42
43pub fn display_property_instances_inline_str(property_instances: &[PropertyInstance]) -> String {
44    if property_instances.is_empty() {
45        String::new()
46    } else {
47        display_property_instances_inline(property_instances).to_string()
48    }
49}
50
51pub fn display_property_instances_inline(property_instances: &[PropertyInstance]) -> Table {
52    let property_instances = property_instances.to_vec();
53    if property_instances.is_empty() {
54        return Table::new(["No properties"]).with(modern_inline()).to_owned();
55    }
56
57    Table::new(property_instances).with(modern_inline()).to_owned()
58}
59
60pub fn display_property_instances_html_inline(property_instances: &[PropertyInstance]) -> String {
61    let property_instances = property_instances.to_vec();
62    if property_instances.is_empty() {
63        return String::new();
64    }
65    HtmlTable::with_header(Vec::<Vec<String>>::from(Table::builder(&property_instances)))
66        .to_string()
67        .split_whitespace()
68        .collect()
69}
70
71#[derive(Clone, Debug)]
72pub struct PropertyInstances(pub Vec<PropertyInstance>);
73
74impl From<PropertyInstances> for reactive_graph_graph::PropertyInstances {
75    fn from(property_instances: PropertyInstances) -> Self {
76        property_instances
77            .0
78            .into_iter()
79            .map(|property_instance| (property_instance.name, property_instance.value))
80            .collect()
81    }
82}
83
84impl From<reactive_graph_graph::PropertyInstances> for PropertyInstances {
85    fn from(property_instances: reactive_graph_graph::PropertyInstances) -> Self {
86        PropertyInstances(
87            property_instances
88                .into_iter()
89                .map(|(name, value)| PropertyInstance {
90                    name,
91                    value,
92                    inline_format: Default::default(),
93                })
94                .collect(),
95        )
96    }
97}
98
99impl fmt::Display for PropertyInstances {
100    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
101        writeln!(f)
102    }
103}
104
105pub struct PropertyInstancesTableOptions;
106
107impl TableOptions for PropertyInstancesTableOptions {
108    fn options(table: &mut Table) -> &mut Table {
109        table.with(Style::extended())
110    }
111}