1use chumsky::prelude::*;
7use vihaco_abi::traits::FromText;
8use vihaco_bytecode::SstHeader;
9use vihaco_parser::Parse;
10
11#[derive(Debug, Clone, PartialEq)]
20pub struct HeaderDeclarations<D>(pub Vec<D>);
21
22impl<D> FromText for HeaderDeclarations<D>
23where
24 for<'src> D: Parse<'src>,
25{
26 fn from_text(text: &str) -> eyre::Result<Self> {
27 D::parser()
28 .padded_by(crate::skip())
29 .then_ignore(just(';').padded_by(crate::skip()))
30 .repeated()
31 .collect::<Vec<_>>()
32 .then_ignore(crate::skip())
33 .then_ignore(end())
34 .parse(text)
35 .into_result()
36 .map(Self)
37 .map_err(|errors| eyre::eyre!("invalid header declarations: {errors:?}"))
38 }
39}
40
41impl<D> SstHeader for HeaderDeclarations<D> where for<'src> D: Parse<'src> {}
42
43impl<D> IntoIterator for HeaderDeclarations<D> {
44 type Item = D;
45 type IntoIter = std::vec::IntoIter<D>;
46
47 fn into_iter(self) -> Self::IntoIter {
48 self.0.into_iter()
49 }
50}
51
52#[cfg(test)]
53mod tests;