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::{Brace, Token},
    type_trait::{LogixTypeDescriptor, Value},
    LogixType,
};

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

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

    fn logix_parse<FS: LogixVfs>(p: &mut LogixParser<FS>) -> Result<Value<Self>> {
        if let Some(ret) = p.forked(|p| match p.next_token()? {
            (_, Token::Ident("ByPath")) => p
                .req_wrapped("ByPath", Brace::Paren, PathBuf::logix_parse)
                .map(|Value { span, value }| {
                    Some(Value {
                        span,
                        value: Self::ByPath(value),
                    })
                }),
            _ => Ok(None),
        })? {
            Ok(ret)
        } else {
            T::logix_parse(p).map(|Value { span, value }| Value {
                span,
                value: Self::Inline(value),
            })
        }
    }
}