Skip to main content

vihaco_syntax/
header.rs

1// SPDX-FileCopyrightText: 2026 The vihaco Authors
2// SPDX-License-Identifier: MIT
3
4//! Section headers as collections of consumer-defined declarations.
5
6use vihaco_abi::traits::FromText;
7use vihaco_bytecode::SSTHeader;
8use vihaco_parser::Parse;
9
10/// Declarations parsed from one section header, one per line.
11///
12/// Available with `program-image-execution`. `D` selects the declarations that
13/// the application accepts, such as device directives and program metadata.
14/// Each declaration ends at the end of its line and continues onto the next
15/// line only inside an open `(`, `[`, or `{`; there is no terminator. Blank
16/// lines and `;` comments may separate declarations; empty headers are valid.
17/// Consumers apply each declaration to their own preparation context
18/// and decide which declarations are required and when to resolve dependencies.
19/// This collection imposes no lowering or image-construction policy.
20#[derive(Debug, Clone, PartialEq)]
21pub struct HeaderDeclarations<D>(pub Vec<D>);
22
23impl<D> FromText for HeaderDeclarations<D>
24where
25    for<'src> D: Parse<'src>,
26{
27    fn from_text(text: &str) -> eyre::Result<Self> {
28        let parsed = crate::spanned::parse_declarations::<D>(text);
29        if let Some(first) = parsed.errors.first() {
30            let (line, column) = crate::spanned::line_column(text, first.span.start);
31            eyre::bail!(
32                "invalid header declarations: header line {line}, column {column}: {first}"
33            );
34        }
35        Ok(Self(
36            parsed
37                .declarations
38                .into_iter()
39                .map(|declaration| declaration.value)
40                .collect(),
41        ))
42    }
43}
44
45impl<D> SSTHeader for HeaderDeclarations<D> where for<'src> D: Parse<'src> {}
46
47impl<D> IntoIterator for HeaderDeclarations<D> {
48    type Item = D;
49    type IntoIter = std::vec::IntoIter<D>;
50
51    fn into_iter(self) -> Self::IntoIter {
52        self.0.into_iter()
53    }
54}
55
56#[cfg(test)]
57mod tests;