rune/parse/
parse.rs
1use crate::alloc::{Box, Vec};
2use crate::compile;
3use crate::parse::{Parser, Peek};
4
5pub use rune_macros::Parse;
7
8pub trait Parse
10where
11 Self: Sized,
12{
13 fn parse(p: &mut Parser<'_>) -> compile::Result<Self>;
15}
16
17impl<A, B> Parse for (A, B)
18where
19 A: Parse + Peek,
20 B: Parse,
21{
22 #[inline]
23 fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
24 Ok((parser.parse()?, parser.parse()?))
25 }
26}
27
28impl<T> Parse for Option<T>
30where
31 T: Parse + Peek,
32{
33 #[inline]
34 fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
35 Ok(if parser.peek::<T>()? {
36 Some(parser.parse()?)
37 } else {
38 None
39 })
40 }
41}
42
43impl<T> Parse for Box<T>
45where
46 T: Parse,
47{
48 #[inline]
49 fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
50 Ok(Box::try_new(parser.parse()?)?)
51 }
52}
53
54impl<T> Parse for Vec<T>
56where
57 T: Parse + Peek,
58{
59 #[inline]
60 fn parse(parser: &mut Parser<'_>) -> compile::Result<Self> {
61 let mut output = Vec::new();
62
63 while parser.peek::<T>()? {
64 output.try_push(parser.parse()?)?;
65 }
66
67 Ok(output)
68 }
69}