java_lang/tree/node/
package.rs

1use std::{
2    borrow::Cow,
3    fmt::{Display, Formatter,Result as FmtResult}
4};
5use super::{Annotation, DocumentationComment};
6
7/// PackageDeclaration表示Java程序中的包声明。
8/// 它包括包的名称、修饰符和文档注释。
9#[derive(Debug, PartialEq)]
10pub struct PackageDeclaration<'a> {
11    /// 包的名称。
12    pub name: Cow<'a, str>,
13    /// 应用到包声明的修饰符。
14    pub modifiers: Vec<Annotation<'a>>,
15    /// 包声明的文档注释。
16    pub documentation: Option<DocumentationComment<'a>>,
17}
18
19impl<'a> Display for PackageDeclaration<'a> {
20    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
21        if let Some(ref d) = self.documentation {
22            Display::fmt(d, f)?;
23        }
24        for i in &self.modifiers {
25            Display::fmt(&i, f)?;
26        }
27        if !self.name.is_empty() {
28            write!(f, "package {};\n", self.name)?;
29        }
30
31        Ok(())
32    }
33}
34