syn_solidity/
file.rs

1use crate::{Item, Spanned};
2use proc_macro2::Span;
3use std::fmt;
4use syn::{
5    parse::{Parse, ParseStream},
6    Attribute, Result,
7};
8
9/// A Solidity file. The root of the AST.
10#[derive(Clone, Debug)]
11pub struct File {
12    /// The inner attributes of the file.
13    pub attrs: Vec<Attribute>,
14    /// The items in the file.
15    pub items: Vec<Item>,
16}
17
18impl fmt::Display for File {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        for (i, item) in self.items.iter().enumerate() {
21            if i > 0 {
22                f.write_str("\n\n")?;
23            }
24            item.fmt(f)?;
25        }
26        Ok(())
27    }
28}
29
30impl Parse for File {
31    fn parse(input: ParseStream<'_>) -> Result<Self> {
32        let attrs = input.call(Attribute::parse_inner)?;
33        let mut items = Vec::new();
34        let mut first = true;
35        while first || !input.is_empty() {
36            first = false;
37            items.push(input.parse()?);
38        }
39        Ok(Self { attrs, items })
40    }
41}
42
43impl Spanned for File {
44    fn span(&self) -> Span {
45        self.items.span()
46    }
47
48    fn set_span(&mut self, span: Span) {
49        self.items.set_span(span);
50    }
51}