From bcf3344bf2e69b306c0ee867ea8b67edab20c1a7 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Sat, 18 Jan 2020 18:06:34 +0100 Subject: add properties to entities, cleaner clang data transformation --- src/parser/clang/entities.rs | 155 +++++++++++++++++++++++++------------------ src/types.rs | 7 +- 2 files changed, 96 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/parser/clang/entities.rs b/src/parser/clang/entities.rs index a5b6462..8da85d9 100644 --- a/src/parser/clang/entities.rs +++ b/src/parser/clang/entities.rs @@ -1,6 +1,7 @@ use crate::types::{self, *}; use clang::{EntityKind, Usr}; +use serde_json::{Map, Value as JSONValue}; use thiserror::Error; use std::collections::BTreeMap; @@ -8,15 +9,6 @@ use std::convert::TryFrom; // TODO: factor out hardcoded strings -#[derive(Debug)] -pub(super) struct Children { - namespaces: Option>>, - variables: Option>>, - structs: Option>>, - functions: Option>>, - typedefs: Option>>, -} - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(super) enum ClangEntityKind { Namespace, @@ -102,6 +94,12 @@ impl TryFrom for ClangEntityKind { } } +#[derive(Debug)] +pub(super) struct PartialEntity { + properties: Map, + children: Children, +} + pub(super) trait ClangEntity { fn kind(&self) -> ClangEntityKind; @@ -120,19 +118,6 @@ pub(super) trait ClangEntity { fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap>> { None } - - fn into_children(self) -> Children - where - Self: Sized, - { - Children { - namespaces: None, - variables: None, - structs: None, - functions: None, - typedefs: None, - } - } } /* @@ -198,12 +183,10 @@ fn entity_id(usr: Usr) -> EntityId { // or one of "-_:.", which means Pandoc can't parse libclang's USRs in title ids and related. // // - EntityId( - usr.0.replace("@", "::").replace("#", ".").replace("$", "-") - ) + EntityId(usr.0.replace("@", "::").replace("#", ".").replace("$", "-")) } -fn append_children( +fn append_children>( acc: &mut types::Children, to_insert: BTreeMap>, ) { @@ -216,28 +199,10 @@ fn append_children( } } -impl From> for Entity { +impl> From> for Entity { fn from(entity: Described) -> Self { - let mut children: types::Children = BTreeMap::new(); - let kind = EntityKind(String::from(entity.entity.kind().to_str_singular())); - let clang_children = entity.entity.into_children(); - - if let Some(namespaces) = clang_children.namespaces { - append_children(&mut children, namespaces); - } - - if let Some(variables) = clang_children.variables { - append_children(&mut children, variables); - } - - if let Some(structs) = clang_children.structs { - append_children(&mut children, structs); - } - - if let Some(functions) = clang_children.functions { - append_children(&mut children, functions); - } + let partial_entity: PartialEntity = entity.entity.into(); Entity { name: entity.description.name, @@ -245,7 +210,8 @@ impl From> for Entity { kind, brief_description: entity.description.brief, documentation: entity.description.detailed, - children, + properties: partial_entity.properties, + children: partial_entity.children, } } } @@ -292,16 +258,6 @@ impl ClangEntity for Namespace { fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap>> { Some(&mut self.member_typedefs) } - - fn into_children(self) -> Children { - Children { - namespaces: Some(self.member_namespaces), - variables: Some(self.member_variables), - structs: Some(self.member_structs), - functions: Some(self.member_functions), - typedefs: Some(self.member_typedefs), - } - } } /* @@ -315,6 +271,21 @@ impl FromNamespaceParent for Namespace { } */ +impl From for PartialEntity { + fn from(entity: Namespace) -> Self { + let mut children = Default::default(); + append_children(&mut children, entity.member_namespaces); + append_children(&mut children, entity.member_variables); + append_children(&mut children, entity.member_structs); + append_children(&mut children, entity.member_functions); + append_children(&mut children, entity.member_typedefs); + PartialEntity { + properties: Default::default(), + children, + } + } +} + #[derive(Debug, Clone)] pub(super) struct Variable { pub(super) kind: VariableKind, @@ -327,6 +298,17 @@ impl ClangEntity for Variable { } } +impl From for PartialEntity { + fn from(entity: Variable) -> Self { + let mut properties = Map::new(); + properties.insert(String::from("type"), JSONValue::String(entity.r#type)); + PartialEntity { + properties, + children: Default::default(), + } + } +} + /* impl FromNamespaceParent for Variable { fn from_namespace_parent<'a>( @@ -365,14 +347,19 @@ impl ClangEntity for Struct { fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap>> { Some(&mut self.member_typedefs) } +} + +impl From for PartialEntity { + fn from(entity: Struct) -> Self { + let mut children = Default::default(); + append_children(&mut children, entity.member_variables); + append_children(&mut children, entity.member_structs); + append_children(&mut children, entity.member_functions); + append_children(&mut children, entity.member_typedefs); - fn into_children(self) -> Children { - Children { - namespaces: None, - variables: Some(self.member_variables), - structs: Some(self.member_structs), - functions: Some(self.member_functions), - typedefs: Some(self.member_typedefs), + PartialEntity { + properties: Default::default(), + children, } } } @@ -401,6 +388,25 @@ impl ClangEntity for Function { } } +impl From for PartialEntity { + fn from(entity: Function) -> Self { + let mut properties = Map::with_capacity(2); + properties.insert( + String::from("return-type"), + JSONValue::String(entity.return_type), + ); + properties.insert( + String::from("arguments"), + JSONValue::Array(entity.arguments.into_iter().map(Into::into).collect()), + ); + + PartialEntity { + properties, + children: Default::default(), + } + } +} + /* impl FromNamespaceParent for Function { fn from_namespace_parent<'a>( @@ -418,6 +424,15 @@ pub(super) struct FunctionArgument { pub(super) r#type: String, } +impl From for JSONValue { + fn from(argument: FunctionArgument) -> JSONValue { + let mut object = Map::new(); + object.insert(String::from("type"), JSONValue::String(argument.r#type)); + object.insert(String::from("name"), JSONValue::String(argument.name)); + JSONValue::Object(object) + } +} + #[derive(Debug, Clone)] pub(super) struct Typedef { pub(super) referee: String, @@ -428,3 +443,15 @@ impl ClangEntity for Typedef { ClangEntityKind::Typedef } } + +impl From for PartialEntity { + fn from(entity: Typedef) -> Self { + let mut properties = Map::with_capacity(1); + properties.insert(String::from("referee"), JSONValue::String(entity.referee)); + + PartialEntity { + properties, + children: Default::default(), + } + } +} diff --git a/src/types.rs b/src/types.rs index 1cd1b57..4099714 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,5 @@ use serde_derive::{Deserialize, Serialize}; +use serde_json::{Map, Value}; use thiserror::Error; use std::collections::BTreeMap; @@ -8,13 +9,15 @@ use std::str::FromStr; // Maybe use the builder pattern? #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] #[non_exhaustive] pub struct Poseidoc { pub config: toml::Value, pub entities: BTreeMap, } -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] #[non_exhaustive] pub struct Entity { pub name: String, @@ -23,6 +26,7 @@ pub struct Entity { pub kind: EntityKind, pub brief_description: String, pub documentation: String, + pub properties: Map, pub children: Children, } @@ -79,7 +83,6 @@ pub struct LanguageParseError(()); #[serde(transparent)] pub struct EntityKind(pub String); - /// Plural version of EntityKind #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(transparent)] -- cgit v1.2.3