summaryrefslogtreecommitdiffstats
path: root/src/types.rs
blob: 1cd1b57a3ac42077b2d35eff6e7ed79b81756fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use serde_derive::{Deserialize, Serialize};
use thiserror::Error;

use std::collections::BTreeMap;
use std::str::FromStr;

// TODO: maybe not public attributes after all, we want to be able to add attributes in the future.
// Maybe use the builder pattern?

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Poseidoc {
    pub config: toml::Value,
    pub entities: BTreeMap<String, Entity>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Entity {
    pub name: String,
    //location: SourceLocation
    pub language: Language,
    pub kind: EntityKind,
    pub brief_description: String,
    pub documentation: String,
    pub children: Children,
}

pub type Children = BTreeMap<ChildrenKind, BTreeMap<EntityId, Entity>>;

// TODO: use newtype for entity kind, entity ID

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "&str", into = "&'static str")]
#[non_exhaustive]
pub enum Language {
    Clang,
}

impl std::fmt::Display for Language {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", <&'static str>::from(*self))
    }
}

impl FromStr for Language {
    type Err = LanguageParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "clang" => Ok(Language::Clang),
            _ => Err(LanguageParseError(())),
        }
    }
}

impl std::convert::TryFrom<&str> for Language {
    type Error = LanguageParseError;
    fn try_from(s: &str) -> Result<Language, Self::Error> {
        Language::from_str(s)
    }
}

impl From<Language> for &'static str {
    fn from(lang: Language) -> &'static str {
        match lang {
            Language::Clang => "clang",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
#[error("invalid language")]
pub struct LanguageParseError(());

// TODO: maybe provide methods instead of struct with public member for NewTypes

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct EntityKind(pub String);


/// Plural version of EntityKind
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ChildrenKind(pub String);

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct EntityId(pub String);