reactive_graph_table_model/types/
extension.rs

1use std::fmt;
2use std::fmt::Formatter;
3use std::ops::Deref;
4use tabled::derive::display;
5
6use serde_json::Value;
7use table_to_html::HtmlTable;
8use tabled::Table;
9use tabled::Tabled;
10use tabled::settings::Modify;
11use tabled::settings::Style;
12use tabled::settings::Width;
13use tabled::settings::object::Columns;
14
15use crate::container::TableInlineFormat;
16use crate::container::TableInlineFormatSetter;
17use crate::container::TableOptions;
18use crate::styles::modern_inline::modern_inline;
19use crate::types::json_value::pretty_json;
20use reactive_graph_graph::NamespacedTypeGetter;
21
22#[derive(Clone, Debug, Tabled)]
23pub struct ExtensionTypeId {
24    pub namespace: String,
25    pub name: String,
26}
27
28impl From<reactive_graph_graph::ExtensionTypeId> for ExtensionTypeId {
29    fn from(ty: reactive_graph_graph::ExtensionTypeId) -> Self {
30        ExtensionTypeId {
31            namespace: ty.path().to_string(),
32            name: ty.type_name().to_string(),
33        }
34    }
35}
36
37pub struct ExtensionDefinition {
38    pub type_: ExtensionTypeId,
39    pub description: String,
40    pub extension: Value,
41}
42
43impl From<reactive_graph_graph::Extension> for ExtensionDefinition {
44    fn from(extension: reactive_graph_graph::Extension) -> Self {
45        ExtensionDefinition {
46            type_: extension.ty.into(),
47            description: extension.description,
48            extension: extension.extension,
49        }
50    }
51}
52
53pub fn display_extensions_inline_str(extensions: &[Extension]) -> String {
54    if extensions.is_empty() {
55        String::new()
56    } else {
57        display_extensions_inline(extensions).to_string()
58    }
59}
60
61pub fn display_extensions_inline(extensions: &[Extension]) -> Table {
62    let extensions = extensions.to_vec();
63    Table::new(extensions)
64        .with(modern_inline())
65        .with(Modify::new(Columns::new(0..1)).with(Width::increase(22)))
66        .with(Modify::new(Columns::new(1..2)).with(Width::increase(22)))
67        .with(Modify::new(Columns::new(2..3)).with(Width::wrap(40)))
68        .with(Modify::new(Columns::new(3..4)).with(Width::wrap(80)))
69        .to_owned()
70}
71
72pub fn display_extensions_html_inline(extensions: &[Extension]) -> String {
73    let extensions = extensions.to_vec();
74    if extensions.is_empty() {
75        return String::new();
76    }
77    HtmlTable::with_header(Vec::<Vec<String>>::from(Table::builder(&extensions)))
78        .to_string()
79        .split_whitespace()
80        .collect()
81}
82
83pub struct ExtensionDefinitions(pub Vec<ExtensionDefinition>);
84
85impl From<ExtensionDefinitions> for Vec<ExtensionDefinition> {
86    fn from(extensions: ExtensionDefinitions) -> Self {
87        extensions.0.into_iter().collect()
88    }
89}
90
91impl From<reactive_graph_graph::Extensions> for ExtensionDefinitions {
92    fn from(extensions: reactive_graph_graph::Extensions) -> Self {
93        ExtensionDefinitions(extensions.into_iter().map(|(_extension_ty, extension)| extension.into()).collect())
94    }
95}
96
97#[derive(Clone, Debug, Tabled)]
98pub struct Extension {
99    /// The namespace of the extension.
100    #[tabled(rename = "Namespace")]
101    pub namespace: String,
102
103    /// The type name of the extension.
104    #[tabled(rename = "Type Name")]
105    pub name: String,
106
107    /// The namespace of the entity type.
108    #[tabled(rename = "Entity Namespace", display("display::option", ""))]
109    pub entity_namespace: Option<String>,
110
111    /// The type name of the entity type.
112    #[tabled(rename = "Entity Type Name", display("display::option", ""))]
113    pub entity_type_name: Option<String>,
114
115    /// Textual description of the extension.
116    #[tabled(rename = "Description")]
117    pub description: String,
118
119    /// The extension as JSON representation.
120    #[tabled(rename = "Extension", display("pretty_json"))]
121    pub extension: Value,
122
123    #[tabled(skip)]
124    inline_format: TableInlineFormat,
125}
126
127// impl Extension {
128//     pub fn entity_ty(&self) -> Option<EntityTypeId> {
129//         if let (Some(namespace), Some(type_name)) = (&self.entity_namespace, &self.entity_type_name) {
130//             Some(reactive_graph_graph::EntityTypeId::try_from(namespace, type_name))
131//         } else {
132//             None
133//         }
134//     }
135// }
136
137impl TableInlineFormatSetter for Extension {
138    fn set_table_inline_format(&mut self, table_inline_format: TableInlineFormat) {
139        self.inline_format = table_inline_format;
140    }
141}
142
143// impl From<Extension> for reactive_graph_graph::Extension {
144//     fn from(extension: Extension) -> Self {
145//         let entity_ty = extension.entity_ty();
146//         let ty = reactive_graph_graph::ExtensionTypeId::new_from_type(extension.namespace, extension.name);
147//         reactive_graph_graph::Extension {
148//             ty,
149//             entity_ty,
150//             description: extension.description,
151//             extension: extension.extension,
152//         }
153//     }
154// }
155
156impl From<reactive_graph_graph::Extension> for Extension {
157    fn from(extension: reactive_graph_graph::Extension) -> Self {
158        Extension {
159            namespace: extension.path().to_string(),
160            name: extension.type_name().to_string(),
161            entity_namespace: extension.entity_ty.clone().map(|entity_ty| entity_ty.path().to_string()),
162            entity_type_name: extension.entity_ty.map(|entity_ty| entity_ty.type_name().to_string()),
163            description: extension.description,
164            extension: extension.extension,
165            inline_format: Default::default(),
166        }
167    }
168}
169
170#[derive(Clone, Debug)]
171pub struct Extensions(pub Vec<Extension>);
172
173impl Deref for Extensions {
174    type Target = Vec<Extension>;
175
176    fn deref(&self) -> &Self::Target {
177        &self.0
178    }
179}
180
181// impl From<Extensions> for reactive_graph_graph::Extensions {
182//     fn from(extensions: Extensions) -> Self {
183//         extensions.0.into_iter().map(|extension| extension.into()).collect()
184//     }
185// }
186
187impl From<Extensions> for Vec<Extension> {
188    fn from(extensions: Extensions) -> Self {
189        extensions.0
190    }
191}
192
193impl From<reactive_graph_graph::Extensions> for Extensions {
194    fn from(extensions: reactive_graph_graph::Extensions) -> Self {
195        Extensions(extensions.into_iter().map(|(_extension_ty, extension)| extension.into()).collect())
196    }
197}
198
199impl fmt::Display for Extensions {
200    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
201        writeln!(f, "{}", Table::new(self.0.iter().cloned()))
202    }
203}
204
205pub struct ExtensionsTableOptions;
206
207impl TableOptions for ExtensionsTableOptions {
208    fn options(table: &mut Table) -> &mut Table {
209        table.with(Style::extended())
210    }
211}