1
use core::fmt;
2
use std::borrow::Cow;
3
use std::path::{Path, PathBuf};
4

            
5
use serde::{Deserialize, Serialize};
6
use transient::Transient;
7

            
8
use crate::config_file::modules::ModulePartition;
9
use crate::domain::translation_unit::TranslationUnit;
10
use crate::impl_translation_unit_for;
11

            
12
4
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
13
pub struct ModulesModel<'a> {
14
1
    pub base_ifcs_dir: Cow<'a, Path>,
15
1
    pub interfaces: Vec<ModuleInterfaceModel<'a>>,
16
1
    pub base_impls_dir: Cow<'a, Path>,
17
1
    pub implementations: Vec<ModuleImplementationModel<'a>>,
18
1
    pub sys_modules: Vec<SystemModule<'a>>,
19
}
20

            
21
6
#[derive(Debug, PartialEq, Eq, Clone, Transient, Serialize, Deserialize, Default)]
22
pub struct ModuleInterfaceModel<'a> {
23
2
    pub path: PathBuf,
24
2
    pub file_stem: Cow<'a, str>,
25
2
    pub extension: Cow<'a, str>,
26
2
    pub module_name: Cow<'a, str>,
27
2
    pub partition: Option<ModulePartitionModel<'a>>,
28
2
    pub dependencies: Vec<Cow<'a, str>>,
29
}
30

            
31
impl_translation_unit_for!(ModuleInterfaceModel<'a>);
32

            
33
impl<'a> fmt::Display for ModuleInterfaceModel<'a> {
34
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35
        write!(
36
            f,
37
            "({:?}, {:?}, {:?}, {:?})",
38
            self.path(),
39
            self.module_name,
40
            self.dependencies,
41
            self.partition
42
        )
43
    }
44
}
45

            
46
#[derive(Debug, PartialEq, Eq, Transient, Clone, Serialize, Deserialize, Default)]
47
pub struct ModulePartitionModel<'a> {
48
    pub module: Cow<'a, str>,
49
    pub partition_name: Cow<'a, str>,
50
    pub is_internal_partition: bool,
51
}
52

            
53
impl<'a> From<ModulePartition<'a>> for ModulePartitionModel<'a> {
54
8
    fn from(value: ModulePartition<'a>) -> Self {
55
8
        Self {
56
8
            module: Cow::Borrowed(value.module),
57
8
            partition_name: Cow::Borrowed(value.partition_name.unwrap_or_default()),
58
8
            is_internal_partition: value.is_internal_partition.unwrap_or_default(),
59
        }
60
8
    }
61
}
62

            
63
8
#[derive(Debug, PartialEq, Eq, Transient, Serialize, Deserialize, Default)]
64
pub struct ModuleImplementationModel<'a> {
65
2
    pub path: PathBuf,
66
2
    pub file_stem: Cow<'a, str>,
67
2
    pub extension: Cow<'a, str>,
68
2
    pub dependencies: Vec<Cow<'a, str>>,
69
}
70

            
71
impl_translation_unit_for!(ModuleImplementationModel<'a>);
72

            
73
impl<'a> fmt::Display for ModuleImplementationModel<'a> {
74
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75
        write!(f, "({:?}, {:?})", self.path(), self.dependencies)
76
    }
77
}
78

            
79
/// Holds the fs information about the `C++` system headers, which they can be built as
80
/// binary module interface for certain compilers, while allowing to import those system headers
81
/// as modules
82
9
#[derive(Debug, PartialEq, Eq, Transient, Serialize, Deserialize, Default)]
83
pub struct SystemModule<'a> {
84
4
    pub path: PathBuf,
85
4
    pub file_stem: Cow<'a, str>,
86
4
    pub extension: Cow<'a, str>,
87
}
88

            
89
impl_translation_unit_for!(SystemModule<'a>);
90

            
91
impl<'a> fmt::Display for SystemModule<'a> {
92
1
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93
1
        write!(f, "{:?}", self.path())
94
1
    }
95
}