1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// This is free and unencumbered software released into the public domain.

use crate::ParsedBlock;
use sysml_model::{prelude::{String, ToString, Vec}, Element, Namespace, Package};

#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ParsedPackage {
    pub(crate) name: String,
    pub(crate) short_name: Option<String>,
    pub(crate) blocks: Vec<ParsedBlock>,
}

impl ParsedPackage {
    pub fn new(name: impl ToString) -> Self {
        Self::with_blocks(name, Vec::new())
    }

    pub fn with_blocks(name: impl ToString, blocks: Vec<ParsedBlock>) -> Self {
        Self { name: name.to_string(), short_name: None, blocks }
    }

    pub fn blocks(&self) -> &Vec<ParsedBlock> {
        &self.blocks
    }

    pub fn add_block(&mut self, block: ParsedBlock) {
        self.blocks.push(block);
    }
}

impl Element for ParsedPackage {
    fn name(&self) -> Option<&str> {
        Some(&self.name)
    }

    fn short_name(&self) -> Option<&str> {
        self.short_name.as_deref()
    }
}

impl Namespace for ParsedPackage {}
impl Package for ParsedPackage {}

#[cfg(test)]
mod tests {
    extern crate std;
    use super::*;

    #[test]
    fn create_package() {
        assert_eq!(ParsedPackage::new("MyPackage").name, "MyPackage");
    }
}