summaryrefslogtreecommitdiffstats
path: root/src/types.rs
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2019-12-21 12:13:21 +0100
committerMinijackson <minijackson@riseup.net>2019-12-21 12:13:21 +0100
commite773caea8010b87726ea524d31798fb2e43e12f4 (patch)
tree9034295831afb25f4bfea5a05d9f83d03e69e86c /src/types.rs
parentde896baff7e97fac4dde79078c9a2fa1c652576b (diff)
downloadposeidoc-e773caea8010b87726ea524d31798fb2e43e12f4.tar.gz
poseidoc-e773caea8010b87726ea524d31798fb2e43e12f4.zip
newtype in types, more generator config, parsing -> parser
Diffstat (limited to 'src/types.rs')
-rw-r--r--src/types.rs70
1 files changed, 64 insertions, 6 deletions
diff --git a/src/types.rs b/src/types.rs
index 82f9c65..f378f8b 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1,22 +1,80 @@
1use serde_derive::{Serialize, Deserialize}; 1use serde_derive::{Serialize, Deserialize};
2use thiserror::Error;
2 3
3use std::collections::BTreeMap; 4use std::collections::BTreeMap;
5use std::str::FromStr;
4 6
5#[derive(Debug, Clone, Serialize, Deserialize)] 7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct Poseidoc { 8pub struct Poseidoc {
7 pub config: toml::Value, 9 pub config: toml::Value,
8 pub entities: BTreeMap<String, Entity>, 10 pub entities: BTreeMap<String, Entity>,
9} 11}
10 12
11#[derive(Debug, Clone, Serialize, Deserialize)] 13#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub struct Entity { 14pub struct Entity {
13 pub name: String, 15 pub name: String,
14 //location: SourceLocation 16 //location: SourceLocation
15 pub language: String, 17 pub language: Language,
16 pub kind: String, 18 pub kind: EntityKind,
17 pub brief_description: String, 19 pub brief_description: String,
18 pub documentation: String, 20 pub documentation: String,
19 pub children: BTreeMap<String, BTreeMap<String, Entity>>, 21 pub children: BTreeMap<ChildrenKind, BTreeMap<EntityId, Entity>>,
20} 22}
21 23
22// TODO: use newtype for language, entity kind, entity ID 24// TODO: use newtype for entity kind, entity ID
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
27#[serde(try_from = "&str", into = "&'static str")]
28pub enum Language {
29 Clang,
30}
31
32impl std::fmt::Display for Language {
33 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
34 write!(f, "{}", <&'static str>::from(*self))
35 }
36}
37
38impl FromStr for Language {
39 type Err = LanguageParseError;
40
41 fn from_str(s: &str) -> Result<Self, Self::Err> {
42 match s {
43 "clang" => Ok(Language::Clang),
44 _ => Err(LanguageParseError(())),
45 }
46 }
47}
48
49impl std::convert::TryFrom<&str> for Language {
50 type Error = LanguageParseError;
51 fn try_from(s: &str) -> Result<Language, Self::Error> {
52 Language::from_str(s)
53 }
54}
55
56impl From<Language> for &'static str {
57 fn from(lang: Language) -> &'static str {
58 match lang {
59 Language::Clang => "clang",
60 }
61 }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
65#[error("invalid language")]
66pub struct LanguageParseError(());
67
68#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
69#[serde(transparent)]
70pub struct EntityKind(pub String);
71
72// Plural version of EntityKind
73
74#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
75#[serde(transparent)]
76pub struct ChildrenKind(pub String);
77
78#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
79#[serde(transparent)]
80pub struct EntityId(pub String);