logix_type/type_trait/
mod.rs

1//! The `LogixType` trait and types used to describe the types
2
3mod impl_trait;
4
5use crate::{error::Result, parser::LogixParser, span::SourceSpan};
6pub use logix_vfs::LogixVfs;
7
8/// Represents a value and the location in the config file
9#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Value<T> {
11    pub value: T,
12    pub span: SourceSpan,
13}
14
15impl<T> Value<T> {
16    pub fn map<R>(self, f: impl FnOnce(T) -> R) -> Value<R> {
17        Value {
18            span: self.span,
19            value: f(self.value),
20        }
21    }
22
23    pub fn join_with_span(mut self, span: SourceSpan) -> Self {
24        self.span = self.span.join(&span);
25        self
26    }
27}
28
29/// Describes a type
30#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
31pub enum LogixValueDescriptor {
32    /// A native type that can be specified by a literal
33    Native,
34    /// Describes a tuple of various types
35    Tuple {
36        members: Vec<&'static LogixTypeDescriptor>,
37    },
38    /// Describes the named members of a struct
39    Struct {
40        members: Vec<(&'static str, &'static LogixTypeDescriptor)>,
41    },
42    /// Describes the variants of an enum
43    Enum { variants: Vec<LogixTypeDescriptor> },
44}
45
46/// Describes a type in the logix config file
47#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
48pub struct LogixTypeDescriptor {
49    /// Name of the type
50    pub name: &'static str,
51    /// Documentation for the type
52    pub doc: &'static str,
53    /// Describes the type itself
54    pub value: LogixValueDescriptor,
55}
56
57/// This trait is used to represent types that can be stored in a logix config.
58pub trait LogixType: Sized {
59    /// A description of the type, intended used for documentation and auto-completion
60    fn descriptor() -> &'static LogixTypeDescriptor;
61    /// If the value is optional, this returns `Some`
62    fn default_value() -> Option<Self>;
63    /// Parse the value from the given parser state
64    fn logix_parse<FS: LogixVfs>(p: &mut LogixParser<FS>) -> Result<Value<Self>>;
65}