summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/parser/clang/entities.rs155
-rw-r--r--src/types.rs7
2 files changed, 96 insertions, 66 deletions
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 @@
1use crate::types::{self, *}; 1use crate::types::{self, *};
2 2
3use clang::{EntityKind, Usr}; 3use clang::{EntityKind, Usr};
4use serde_json::{Map, Value as JSONValue};
4use thiserror::Error; 5use thiserror::Error;
5 6
6use std::collections::BTreeMap; 7use std::collections::BTreeMap;
@@ -8,15 +9,6 @@ use std::convert::TryFrom;
8 9
9// TODO: factor out hardcoded strings 10// TODO: factor out hardcoded strings
10 11
11#[derive(Debug)]
12pub(super) struct Children {
13 namespaces: Option<BTreeMap<Usr, Described<Namespace>>>,
14 variables: Option<BTreeMap<Usr, Described<Variable>>>,
15 structs: Option<BTreeMap<Usr, Described<Struct>>>,
16 functions: Option<BTreeMap<Usr, Described<Function>>>,
17 typedefs: Option<BTreeMap<Usr, Described<Typedef>>>,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)] 12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub(super) enum ClangEntityKind { 13pub(super) enum ClangEntityKind {
22 Namespace, 14 Namespace,
@@ -102,6 +94,12 @@ impl TryFrom<EntityKind> for ClangEntityKind {
102 } 94 }
103} 95}
104 96
97#[derive(Debug)]
98pub(super) struct PartialEntity {
99 properties: Map<String, JSONValue>,
100 children: Children,
101}
102
105pub(super) trait ClangEntity { 103pub(super) trait ClangEntity {
106 fn kind(&self) -> ClangEntityKind; 104 fn kind(&self) -> ClangEntityKind;
107 105
@@ -120,19 +118,6 @@ pub(super) trait ClangEntity {
120 fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap<Usr, Described<Typedef>>> { 118 fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap<Usr, Described<Typedef>>> {
121 None 119 None
122 } 120 }
123
124 fn into_children(self) -> Children
125 where
126 Self: Sized,
127 {
128 Children {
129 namespaces: None,
130 variables: None,
131 structs: None,
132 functions: None,
133 typedefs: None,
134 }
135 }
136} 121}
137 122
138/* 123/*
@@ -198,12 +183,10 @@ fn entity_id(usr: Usr) -> EntityId {
198 // or one of "-_:.", which means Pandoc can't parse libclang's USRs in title ids and related. 183 // or one of "-_:.", which means Pandoc can't parse libclang's USRs in title ids and related.
199 // 184 //
200 // <https://github.com/jgm/pandoc/blob/2.9/src/Text/Pandoc/Readers/Markdown.hs#L581> 185 // <https://github.com/jgm/pandoc/blob/2.9/src/Text/Pandoc/Readers/Markdown.hs#L581>
201 EntityId( 186 EntityId(usr.0.replace("@", "::").replace("#", ".").replace("$", "-"))
202 usr.0.replace("@", "::").replace("#", ".").replace("$", "-")
203 )
204} 187}
205 188
206fn append_children<T: ClangEntity>( 189fn append_children<T: ClangEntity + Into<PartialEntity>>(
207 acc: &mut types::Children, 190 acc: &mut types::Children,
208 to_insert: BTreeMap<Usr, Described<T>>, 191 to_insert: BTreeMap<Usr, Described<T>>,
209) { 192) {
@@ -216,28 +199,10 @@ fn append_children<T: ClangEntity>(
216 } 199 }
217} 200}
218 201
219impl<T: ClangEntity> From<Described<T>> for Entity { 202impl<T: ClangEntity + Into<PartialEntity>> From<Described<T>> for Entity {
220 fn from(entity: Described<T>) -> Self { 203 fn from(entity: Described<T>) -> Self {
221 let mut children: types::Children = BTreeMap::new();
222
223 let kind = EntityKind(String::from(entity.entity.kind().to_str_singular())); 204 let kind = EntityKind(String::from(entity.entity.kind().to_str_singular()));
224 let clang_children = entity.entity.into_children(); 205 let partial_entity: PartialEntity = entity.entity.into();
225
226 if let Some(namespaces) = clang_children.namespaces {
227 append_children(&mut children, namespaces);
228 }
229
230 if let Some(variables) = clang_children.variables {
231 append_children(&mut children, variables);
232 }
233
234 if let Some(structs) = clang_children.structs {
235 append_children(&mut children, structs);
236 }
237
238 if let Some(functions) = clang_children.functions {
239 append_children(&mut children, functions);
240 }
241 206
242 Entity { 207 Entity {
243 name: entity.description.name, 208 name: entity.description.name,
@@ -245,7 +210,8 @@ impl<T: ClangEntity> From<Described<T>> for Entity {
245 kind, 210 kind,
246 brief_description: entity.description.brief, 211 brief_description: entity.description.brief,
247 documentation: entity.description.detailed, 212 documentation: entity.description.detailed,
248 children, 213 properties: partial_entity.properties,
214 children: partial_entity.children,
249 } 215 }
250 } 216 }
251} 217}
@@ -292,16 +258,6 @@ impl ClangEntity for Namespace {
292 fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap<Usr, Described<Typedef>>> { 258 fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap<Usr, Described<Typedef>>> {
293 Some(&mut self.member_typedefs) 259 Some(&mut self.member_typedefs)
294 } 260 }
295
296 fn into_children(self) -> Children {
297 Children {
298 namespaces: Some(self.member_namespaces),
299 variables: Some(self.member_variables),
300 structs: Some(self.member_structs),
301 functions: Some(self.member_functions),
302 typedefs: Some(self.member_typedefs),
303 }
304 }
305} 261}
306 262
307/* 263/*
@@ -315,6 +271,21 @@ impl FromNamespaceParent for Namespace {
315} 271}
316*/ 272*/
317 273
274impl From<Namespace> for PartialEntity {
275 fn from(entity: Namespace) -> Self {
276 let mut children = Default::default();
277 append_children(&mut children, entity.member_namespaces);
278 append_children(&mut children, entity.member_variables);
279 append_children(&mut children, entity.member_structs);
280 append_children(&mut children, entity.member_functions);
281 append_children(&mut children, entity.member_typedefs);
282 PartialEntity {
283 properties: Default::default(),
284 children,
285 }
286 }
287}
288
318#[derive(Debug, Clone)] 289#[derive(Debug, Clone)]
319pub(super) struct Variable { 290pub(super) struct Variable {
320 pub(super) kind: VariableKind, 291 pub(super) kind: VariableKind,
@@ -327,6 +298,17 @@ impl ClangEntity for Variable {
327 } 298 }
328} 299}
329 300
301impl From<Variable> for PartialEntity {
302 fn from(entity: Variable) -> Self {
303 let mut properties = Map::new();
304 properties.insert(String::from("type"), JSONValue::String(entity.r#type));
305 PartialEntity {
306 properties,
307 children: Default::default(),
308 }
309 }
310}
311
330/* 312/*
331impl FromNamespaceParent for Variable { 313impl FromNamespaceParent for Variable {
332 fn from_namespace_parent<'a>( 314 fn from_namespace_parent<'a>(
@@ -365,14 +347,19 @@ impl ClangEntity for Struct {
365 fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap<Usr, Described<Typedef>>> { 347 fn get_member_typedefs(&mut self) -> Option<&mut BTreeMap<Usr, Described<Typedef>>> {
366 Some(&mut self.member_typedefs) 348 Some(&mut self.member_typedefs)
367 } 349 }
350}
351
352impl From<Struct> for PartialEntity {
353 fn from(entity: Struct) -> Self {
354 let mut children = Default::default();
355 append_children(&mut children, entity.member_variables);
356 append_children(&mut children, entity.member_structs);
357 append_children(&mut children, entity.member_functions);
358 append_children(&mut children, entity.member_typedefs);
368 359
369 fn into_children(self) -> Children { 360 PartialEntity {
370 Children { 361 properties: Default::default(),
371 namespaces: None, 362 children,
372 variables: Some(self.member_variables),
373 structs: Some(self.member_structs),
374 functions: Some(self.member_functions),
375 typedefs: Some(self.member_typedefs),
376 } 363 }
377 } 364 }
378} 365}
@@ -401,6 +388,25 @@ impl ClangEntity for Function {
401 } 388 }
402} 389}
403 390
391impl From<Function> for PartialEntity {
392 fn from(entity: Function) -> Self {
393 let mut properties = Map::with_capacity(2);
394 properties.insert(
395 String::from("return-type"),
396 JSONValue::String(entity.return_type),
397 );
398 properties.insert(
399 String::from("arguments"),
400 JSONValue::Array(entity.arguments.into_iter().map(Into::into).collect()),
401 );
402
403 PartialEntity {
404 properties,
405 children: Default::default(),
406 }
407 }
408}
409
404/* 410/*
405impl FromNamespaceParent for Function { 411impl FromNamespaceParent for Function {
406 fn from_namespace_parent<'a>( 412 fn from_namespace_parent<'a>(
@@ -418,6 +424,15 @@ pub(super) struct FunctionArgument {
418 pub(super) r#type: String, 424 pub(super) r#type: String,
419} 425}
420 426
427impl From<FunctionArgument> for JSONValue {
428 fn from(argument: FunctionArgument) -> JSONValue {
429 let mut object = Map::new();
430 object.insert(String::from("type"), JSONValue::String(argument.r#type));
431 object.insert(String::from("name"), JSONValue::String(argument.name));
432 JSONValue::Object(object)
433 }
434}
435
421#[derive(Debug, Clone)] 436#[derive(Debug, Clone)]
422pub(super) struct Typedef { 437pub(super) struct Typedef {
423 pub(super) referee: String, 438 pub(super) referee: String,
@@ -428,3 +443,15 @@ impl ClangEntity for Typedef {
428 ClangEntityKind::Typedef 443 ClangEntityKind::Typedef
429 } 444 }
430} 445}
446
447impl From<Typedef> for PartialEntity {
448 fn from(entity: Typedef) -> Self {
449 let mut properties = Map::with_capacity(1);
450 properties.insert(String::from("referee"), JSONValue::String(entity.referee));
451
452 PartialEntity {
453 properties,
454 children: Default::default(),
455 }
456 }
457}
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 @@
1use serde_derive::{Deserialize, Serialize}; 1use serde_derive::{Deserialize, Serialize};
2use serde_json::{Map, Value};
2use thiserror::Error; 3use thiserror::Error;
3 4
4use std::collections::BTreeMap; 5use std::collections::BTreeMap;
@@ -8,13 +9,15 @@ use std::str::FromStr;
8// Maybe use the builder pattern? 9// Maybe use the builder pattern?
9 10
10#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] 11#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
12#[serde(rename_all = "kebab-case")]
11#[non_exhaustive] 13#[non_exhaustive]
12pub struct Poseidoc { 14pub struct Poseidoc {
13 pub config: toml::Value, 15 pub config: toml::Value,
14 pub entities: BTreeMap<String, Entity>, 16 pub entities: BTreeMap<String, Entity>,
15} 17}
16 18
17#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] 19#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
20#[serde(rename_all = "kebab-case")]
18#[non_exhaustive] 21#[non_exhaustive]
19pub struct Entity { 22pub struct Entity {
20 pub name: String, 23 pub name: String,
@@ -23,6 +26,7 @@ pub struct Entity {
23 pub kind: EntityKind, 26 pub kind: EntityKind,
24 pub brief_description: String, 27 pub brief_description: String,
25 pub documentation: String, 28 pub documentation: String,
29 pub properties: Map<String, Value>,
26 pub children: Children, 30 pub children: Children,
27} 31}
28 32
@@ -79,7 +83,6 @@ pub struct LanguageParseError(());
79#[serde(transparent)] 83#[serde(transparent)]
80pub struct EntityKind(pub String); 84pub struct EntityKind(pub String);
81 85
82
83/// Plural version of EntityKind 86/// Plural version of EntityKind
84#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] 87#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
85#[serde(transparent)] 88#[serde(transparent)]