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, } #[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>; // 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 { 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::from_str(s) } } impl From 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);