cmtool_data/
descriptors.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone, Copy)]
9pub enum CMAExportType {
10 LiquidFlow,
11 GasFlow,
12 GasVolume,
13 LiquidVolume,
14 EnergyDissipation,
15 Kla,
16 Other,
17}
18
19#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone, Copy)]
21pub enum PhaseCM {
22 Liquid,
23 Gas,
24}
25
26impl PhaseCM {
27 pub fn identifier(self) -> String {
28 match self {
29 Self::Liquid => String::from("L"),
30 Self::Gas => String::from("G"),
31 }
32 }
33}
34
35impl From<PhaseCM> for String {
36 fn from(val: PhaseCM) -> Self {
37 match val {
38 PhaseCM::Liquid => String::from("liquid"),
39 PhaseCM::Gas => String::from("gas"),
40 }
41 }
42}
43
44impl std::str::FromStr for PhaseCM {
47 type Err = crate::DataError;
48
49 fn from_str(value: &str) -> Result<Self, Self::Err> {
50 match value.trim().to_ascii_lowercase().as_str() {
51 "liquid" | "l" => Ok(Self::Liquid),
52 "gas" | "g" => Ok(Self::Gas),
53 _ => Err(crate::DataError::BadData),
54 }
55 }
56}
57
58pub enum CMExportType {
60 Flow(PhaseCM),
61 Volume(PhaseCM),
62 EnergyDissipation,
63 Kla,
64 Other(String),
65}
66
67impl From<CMAExportType> for CMExportType {
68 fn from(value: CMAExportType) -> Self {
69 match value {
70 CMAExportType::LiquidFlow => Self::Flow(PhaseCM::Liquid),
71 CMAExportType::GasFlow => Self::Flow(PhaseCM::Gas),
72 CMAExportType::GasVolume => Self::Volume(PhaseCM::Gas),
73 CMAExportType::LiquidVolume => Self::Volume(PhaseCM::Liquid),
74 CMAExportType::EnergyDissipation => Self::EnergyDissipation,
75 CMAExportType::Kla => Self::Kla,
76 CMAExportType::Other => Self::Other("Unnamed".to_string()),
77 }
78 }
79}
80
81impl From<CMExportType> for CMAExportType {
82 fn from(val: CMExportType) -> Self {
83 match val {
84 CMExportType::Flow(PhaseCM::Gas) => CMAExportType::GasFlow,
86 CMExportType::Flow(PhaseCM::Liquid) => CMAExportType::LiquidFlow,
87 CMExportType::Volume(PhaseCM::Gas) => CMAExportType::GasVolume,
88 CMExportType::Volume(PhaseCM::Liquid) => CMAExportType::LiquidVolume,
89 CMExportType::EnergyDissipation => CMAExportType::EnergyDissipation,
90 CMExportType::Kla => CMAExportType::Kla,
91 CMExportType::Other(_) => CMAExportType::Other,
92 }
93 }
94}
95
96impl CMExportType {
97 pub fn default_filename(self) -> String {
98 match self {
99 Self::Flow(g) => {
100 format!("flow{}.raw", g.identifier())
101 }
102 Self::Volume(g) => {
103 format!("vof{}.raw", g.identifier())
104 }
105 Self::EnergyDissipation => "epsturb.raw".to_string(),
106 Self::Kla => "kla.raw".to_string(),
107 Self::Other(name) => {
108 format!("m_{}.raw", name)
109 }
110 }
111 }
112}
113
114impl From<i8> for CMExportType {
115 fn from(value: i8) -> Self {
116 match value {
117 0 => CMExportType::Flow(PhaseCM::Liquid),
118 1 => CMExportType::Flow(PhaseCM::Gas),
119 2 => CMExportType::Volume(PhaseCM::Gas),
120 3 => CMExportType::Volume(PhaseCM::Liquid),
121 4 => CMExportType::EnergyDissipation,
122 5 => CMExportType::Kla,
123 6 => CMExportType::Other(String::from("Unnamed")),
124 _ => panic!("Invalid value for CMAExportType: {}", value),
125 }
126 }
127}
128
129impl From<i8> for CMAExportType {
130 fn from(value: i8) -> Self {
131 match value {
132 0 => CMAExportType::LiquidFlow,
133 1 => CMAExportType::GasFlow,
134 2 => CMAExportType::GasVolume,
135 3 => CMAExportType::LiquidVolume,
136 4 => CMAExportType::EnergyDissipation,
137 5 => CMAExportType::Kla,
138 6 => CMAExportType::Other,
139 _ => panic!("Invalid value for CMAExportType: {}", value),
140 }
141 }
142}
143
144#[cfg(test)]
145mod test {
146 use super::*;
147
148 #[test]
149 fn phase_string_format() {
150 for phase in [PhaseCM::Liquid, PhaseCM::Gas] {
151 let long: String = phase.into();
152 assert_eq!(long.parse::<PhaseCM>().unwrap(), phase);
153 assert_eq!(phase.identifier().parse::<PhaseCM>().unwrap(), phase);
154 }
155
156 assert_eq!(" GAS ".parse::<PhaseCM>().unwrap(), PhaseCM::Gas);
157 assert_eq!("l".parse::<PhaseCM>().unwrap(), PhaseCM::Liquid);
158 assert!("solid".parse::<PhaseCM>().is_err());
159 assert!("".parse::<PhaseCM>().is_err());
160 }
161}