java_lang/tree/node/
compilation_unit.rs1use std::fmt::{Display, Formatter, Result as FmtResult};
2use super::{ImportDeclaration, ModuleDeclaration, PackageDeclaration, TopLevelClassOrInterfaceDeclaration};
3
4#[derive(Debug)]
7pub enum CompilationUnitDeclaration<'a> {
8 Ordinary {
11 package: Option<PackageDeclaration<'a>>,
13 imports: Vec<ImportDeclaration<'a>>,
15 top_level_class_or_interfaces: Vec<TopLevelClassOrInterfaceDeclaration<'a>>,
17 },
18 Modular {
21 imports: Vec<ImportDeclaration<'a>>,
23 module: ModuleDeclaration<'a>,
25 },
26}
27
28impl<'a> CompilationUnitDeclaration<'a> {
29 pub fn package(&self) -> Option<&PackageDeclaration> {
31 if let Self::Ordinary {
32 package: Some(package),
33 ..
34 } = self
35 {
36 return Some(package);
37 }
38 None
39 }
40
41 pub fn imports(&self) -> &[ImportDeclaration] {
43 match self {
44 Self::Ordinary { imports, .. } | Self::Modular { imports, .. } => imports,
45 }
46 }
47}
48
49impl<'a> Display for CompilationUnitDeclaration<'a> {
50 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
51 if let Some(package) = self.package() {
52 Display::fmt(package, f)?;
53 }
54 for i in self.imports() {
55 Display::fmt(&i, f)?;
56 write!(f, "\n")?;
57 }
58 if let Self::Ordinary {
59 top_level_class_or_interfaces,
60 ..
61 } = self
62 {
63 for i in top_level_class_or_interfaces {
64 Display::fmt(&i, f)?;
65 write!(f, "\n")?;
66 }
67 }
68 if let Self::Modular { module, .. } = self {
69 Display::fmt(module, f)?;
70 }
71
72 Ok(())
73 }
74}
75