diff options
author | Minijackson <minijackson@riseup.net> | 2019-12-21 12:13:21 +0100 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2019-12-21 12:13:21 +0100 |
commit | e773caea8010b87726ea524d31798fb2e43e12f4 (patch) | |
tree | 9034295831afb25f4bfea5a05d9f83d03e69e86c /src/types.rs | |
parent | de896baff7e97fac4dde79078c9a2fa1c652576b (diff) | |
download | poseidoc-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.rs | 70 |
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 @@ | |||
1 | use serde_derive::{Serialize, Deserialize}; | 1 | use serde_derive::{Serialize, Deserialize}; |
2 | use thiserror::Error; | ||
2 | 3 | ||
3 | use std::collections::BTreeMap; | 4 | use std::collections::BTreeMap; |
5 | use std::str::FromStr; | ||
4 | 6 | ||
5 | #[derive(Debug, Clone, Serialize, Deserialize)] | 7 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
6 | pub struct Poseidoc { | 8 | pub 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)] |
12 | pub struct Entity { | 14 | pub 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")] | ||
28 | pub enum Language { | ||
29 | Clang, | ||
30 | } | ||
31 | |||
32 | impl 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 | |||
38 | impl 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 | |||
49 | impl 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 | |||
56 | impl 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")] | ||
66 | pub struct LanguageParseError(()); | ||
67 | |||
68 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] | ||
69 | #[serde(transparent)] | ||
70 | pub 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)] | ||
76 | pub struct ChildrenKind(pub String); | ||
77 | |||
78 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] | ||
79 | #[serde(transparent)] | ||
80 | pub struct EntityId(pub String); | ||