logix_type/types/
array.rs

1use crate::{
2    error::Result,
3    token::Brace,
4    type_trait::{LogixTypeDescriptor, LogixValueDescriptor, Value},
5    LogixParser, LogixType,
6};
7
8impl<const SIZE: usize, T: LogixType> LogixType for [T; SIZE] {
9    fn descriptor() -> &'static LogixTypeDescriptor {
10        &LogixTypeDescriptor {
11            name: "Array",
12            doc: "a fixed size array",
13            value: LogixValueDescriptor::Native,
14        }
15    }
16
17    fn default_value() -> Option<Self> {
18        None
19    }
20
21    fn logix_parse<FS: logix_vfs::LogixVfs>(p: &mut LogixParser<FS>) -> Result<Value<Self>> {
22        p.req_wrapped("Array", Brace::Square, |p| {
23            let mut ret = Vec::with_capacity(SIZE); // TODO(2023.04.01): Change to array.try_map when stable (rust-lang#79711)
24            let mut it = p.parse_delimited::<T>("Array");
25            for _ in 0..SIZE {
26                ret.push(it.req_next_item()?.value);
27            }
28            it.req_end(Brace::Square)?;
29            Ok(ret.try_into().ok().unwrap())
30        })
31    }
32}
33
34impl<T: LogixType> LogixType for Vec<T> {
35    fn descriptor() -> &'static LogixTypeDescriptor {
36        &LogixTypeDescriptor {
37            name: "list",
38            doc: "a dynamically sized array",
39            value: LogixValueDescriptor::Native,
40        }
41    }
42
43    fn default_value() -> Option<Self> {
44        None
45    }
46
47    fn logix_parse<FS: logix_vfs::LogixVfs>(p: &mut LogixParser<FS>) -> Result<Value<Self>> {
48        p.req_wrapped("list", Brace::Square, |p| {
49            let mut ret = Vec::new();
50            for item in p.parse_delimited::<T>("list") {
51                let Value { value, span: _ } = item?;
52                ret.push(value);
53            }
54            Ok(ret)
55        })
56    }
57}