2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
/// The top-level type produced by the parser. Not all successful parses result
|
2021-07-23 20:26:27 -07:00
|
|
|
/// in valid justfiles, so additional consistency checks and name resolution
|
2021-07-26 01:26:06 -07:00
|
|
|
/// are performed by the `Analyzer`, which produces a `Justfile` from an `Ast`.
|
2021-06-08 01:01:27 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2021-07-23 20:26:27 -07:00
|
|
|
pub(crate) struct Ast<'src> {
|
2019-11-07 10:55:15 -08:00
|
|
|
/// Items in the justfile
|
2021-09-16 06:44:40 -07:00
|
|
|
pub(crate) items: Vec<Item<'src>>,
|
2019-11-07 10:55:15 -08:00
|
|
|
/// Non-fatal warnings encountered during parsing
|
2021-03-28 23:39:23 -07:00
|
|
|
pub(crate) warnings: Vec<Warning>,
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
2021-06-08 01:01:27 -07:00
|
|
|
|
2021-07-23 20:26:27 -07:00
|
|
|
impl<'src> Display for Ast<'src> {
|
2021-06-08 01:01:27 -07:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
|
|
|
|
let mut iter = self.items.iter().peekable();
|
|
|
|
|
|
|
|
while let Some(item) = iter.next() {
|
2022-12-15 16:53:21 -08:00
|
|
|
writeln!(f, "{item}")?;
|
2021-06-08 01:01:27 -07:00
|
|
|
|
|
|
|
if let Some(next_item) = iter.peek() {
|
|
|
|
if matches!(item, Item::Recipe(_))
|
|
|
|
|| mem::discriminant(item) != mem::discriminant(next_item)
|
|
|
|
{
|
|
|
|
writeln!(f)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|