reactive_graph_table_model/types/
relations.rs

1use tabled::Table;
2use tabled::Tabled;
3use tabled::settings::Modify;
4use tabled::settings::Style;
5use tabled::settings::Width;
6use tabled::settings::object::Segment;
7
8use crate::container::TableInlineFormat;
9use crate::container::TableInlineFormatSetter;
10use crate::container::TableOptions;
11use crate::types::component::ComponentTypeId;
12use crate::types::component::ComponentTypeIds;
13use crate::types::component::display_component_type_ids_html_inline;
14use crate::types::component::display_component_type_ids_inline_str;
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 RelationType {
27    /// The outbound type.
28    pub outbound_type: String,
29
30    /// The namespace of the relation type.
31    pub namespace: String,
32
33    /// The name of the relation type.
34    pub name: String,
35
36    /// The inbound type.
37    pub inbound_type: String,
38
39    /// Textual description of the relation type.
40    // #[tabled(skip)]
41    pub description: String,
42
43    /// The components.
44    #[tabled(display("display_component_type_ids", self))]
45    pub components: Vec<ComponentTypeId>,
46
47    /// The property types.
48    #[tabled(display("display_property_types", self))]
49    pub properties: Vec<PropertyType>,
50
51    /// The extensions.
52    #[tabled(display("display_extensions", self))]
53    pub extensions: Vec<Extension>,
54
55    #[tabled(skip)]
56    inline_format: TableInlineFormat,
57}
58
59fn display_component_type_ids(components: &[ComponentTypeId], relation_type: &RelationType) -> String {
60    match relation_type.inline_format {
61        TableInlineFormat::Table => display_component_type_ids_inline_str(components),
62        TableInlineFormat::Html => display_component_type_ids_html_inline(components),
63    }
64}
65
66fn display_property_types(properties: &[PropertyType], relation_type: &RelationType) -> String {
67    match relation_type.inline_format {
68        TableInlineFormat::Table => display_property_types_inline_str(properties),
69        TableInlineFormat::Html => display_property_types_html_inline(properties),
70    }
71}
72
73fn display_extensions(extensions: &[Extension], relation_type: &RelationType) -> String {
74    match relation_type.inline_format {
75        TableInlineFormat::Table => display_extensions_inline_str(extensions),
76        TableInlineFormat::Html => display_extensions_html_inline(extensions),
77    }
78}
79
80impl TableInlineFormatSetter for RelationType {
81    fn set_table_inline_format(&mut self, table_inline_format: TableInlineFormat) {
82        self.inline_format = table_inline_format;
83    }
84}
85
86impl From<reactive_graph_graph::RelationType> for RelationType {
87    fn from(relation_type: reactive_graph_graph::RelationType) -> Self {
88        RelationType {
89            outbound_type: relation_type.outbound_type.to_string(),
90            namespace: relation_type.path().to_string(),
91            name: relation_type.type_name().to_string(),
92            inbound_type: relation_type.inbound_type.to_string(),
93            description: relation_type.description,
94            components: ComponentTypeIds::from(relation_type.components).0,
95            properties: PropertyTypes::from(relation_type.properties).0,
96            extensions: Extensions::from(relation_type.extensions).0,
97            inline_format: Default::default(),
98        }
99    }
100}
101
102pub struct RelationTypesTableOptions;
103
104impl TableOptions for RelationTypesTableOptions {
105    fn options(table: &mut Table) -> &mut Table {
106        table.with(Style::extended()).with(
107            Modify::new(Segment::new(0.., 0..3))
108                .with(Width::increase(22))
109                .with(Width::increase(22))
110                .with(Width::wrap(40).keep_words(true)),
111        )
112    }
113}