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
use std::path::PathBuf;

use logix_vfs::LogixVfs;

use crate::{
    error::Result,
    parser::LogixParser,
    token::{Action, Token},
    type_trait::{LogixTypeDescriptor, Value},
    LogixType,
};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Data<T> {
    ByPath(PathBuf),
    Inline(T),
}

impl<T: LogixType> LogixType for Data<T> {
    fn descriptor() -> &'static LogixTypeDescriptor {
        T::descriptor()
    }

    fn default_value() -> Option<Self> {
        None
    }

    fn logix_parse<FS: LogixVfs>(p: &mut LogixParser<FS>) -> Result<Value<Self>> {
        if let Some(ret) = p.forked(|p| match p.next_token()? {
            (span, Token::Action(Action::Include)) => {
                let file = crate::action::for_include(span, p)?;
                Ok(Some(file.map(|f| Data::ByPath(f.0))))
            }
            _ => Ok(None),
        })? {
            Ok(ret)
        } else {
            T::logix_parse(p).map(|Value { span, value }| Value {
                span,
                value: Self::Inline(value),
            })
        }
    }
}