simparse 0.3.4__tar.gz → 0.3.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of simparse might be problematic. Click here for more details.

Files changed (34) hide show
  1. {simparse-0.3.4 → simparse-0.3.5}/Cargo.lock +5 -4
  2. {simparse-0.3.4 → simparse-0.3.5}/Cargo.toml +1 -1
  3. {simparse-0.3.4 → simparse-0.3.5}/PKG-INFO +2 -1
  4. {simparse-0.3.4 → simparse-0.3.5}/README.md +1 -0
  5. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/Cargo.toml +1 -0
  6. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/dispatch.rs +4 -1
  7. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/ir.rs +6 -0
  8. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/lib.rs +1 -0
  9. simparse-0.3.5/crates/simparse-core/src/parsers/matfile.rs +499 -0
  10. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/mod.rs +1 -0
  11. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/schema.rs +5 -0
  12. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/summary.rs +53 -1
  13. simparse-0.3.5/crates/simparse-core/tests/matfile.rs +296 -0
  14. {simparse-0.3.4 → simparse-0.3.5}/pyproject.toml +1 -1
  15. {simparse-0.3.4 → simparse-0.3.5}/LICENSE +0 -0
  16. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/examples/ir_asset_probe.rs +0 -0
  17. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/error.rs +0 -0
  18. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/abaqus.rs +0 -0
  19. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/comsol.rs +0 -0
  20. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/flotherm.rs +0 -0
  21. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/hfss.rs +0 -0
  22. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/icepak.rs +0 -0
  23. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/simulink.rs +0 -0
  24. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/src/parsers/step.rs +0 -0
  25. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/tests/simulink.rs +0 -0
  26. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/tests/step_ir.rs +0 -0
  27. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-core/tests/synthetic.rs +0 -0
  28. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-hdf5/Cargo.toml +0 -0
  29. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-hdf5/src/lib.rs +0 -0
  30. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-py/Cargo.toml +0 -0
  31. {simparse-0.3.4 → simparse-0.3.5}/crates/simparse-py/src/lib.rs +0 -0
  32. {simparse-0.3.4 → simparse-0.3.5}/python/simparse/__init__.py +0 -0
  33. {simparse-0.3.4 → simparse-0.3.5}/python/simparse/__init__.pyi +0 -0
  34. {simparse-0.3.4 → simparse-0.3.5}/python/simparse/py.typed +0 -0
@@ -826,7 +826,7 @@ checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214"
826
826
 
827
827
  [[package]]
828
828
  name = "simparse-cli"
829
- version = "0.3.4"
829
+ version = "0.3.5"
830
830
  dependencies = [
831
831
  "anyhow",
832
832
  "clap",
@@ -837,9 +837,10 @@ dependencies = [
837
837
 
838
838
  [[package]]
839
839
  name = "simparse-core"
840
- version = "0.3.4"
840
+ version = "0.3.5"
841
841
  dependencies = [
842
842
  "flate2",
843
+ "hdf5-pure",
843
844
  "quick-xml",
844
845
  "schemars",
845
846
  "serde",
@@ -855,7 +856,7 @@ dependencies = [
855
856
 
856
857
  [[package]]
857
858
  name = "simparse-hdf5"
858
- version = "0.3.4"
859
+ version = "0.3.5"
859
860
  dependencies = [
860
861
  "flate2",
861
862
  "hdf5-pure",
@@ -868,7 +869,7 @@ dependencies = [
868
869
 
869
870
  [[package]]
870
871
  name = "simparse-py"
871
- version = "0.3.4"
872
+ version = "0.3.5"
872
873
  dependencies = [
873
874
  "pyo3",
874
875
  "serde",
@@ -3,7 +3,7 @@ members = ["crates/simparse-core", "crates/simparse-hdf5", "crates/simparse-py"]
3
3
  resolver = "2"
4
4
 
5
5
  [workspace.package]
6
- version = "0.3.4"
6
+ version = "0.3.5"
7
7
  edition = "2024"
8
8
  license = "Apache-2.0"
9
9
  repository = "https://github.com/svd-ai-lab/simparse"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: simparse
3
- Version: 0.3.4
3
+ Version: 0.3.5
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: License :: OSI Approved :: Apache Software License
6
6
  Classifier: Programming Language :: Python :: 3
@@ -31,6 +31,7 @@ case metadata.
31
31
  | Icepak Classic | `.tzr` |
32
32
  | Simcenter FloTHERM | `.pack`, `.xml`, `.floxml` |
33
33
  | Simulink | `.slx`, `.mdl` |
34
+ | MATLAB data | `.mat` |
34
35
 
35
36
  ## Design
36
37
 
@@ -16,6 +16,7 @@ case metadata.
16
16
  | Icepak Classic | `.tzr` |
17
17
  | Simcenter FloTHERM | `.pack`, `.xml`, `.floxml` |
18
18
  | Simulink | `.slx`, `.mdl` |
19
+ | MATLAB data | `.mat` |
19
20
 
20
21
  ## Design
21
22
 
@@ -8,6 +8,7 @@ rust-version.workspace = true
8
8
 
9
9
  [dependencies]
10
10
  flate2.workspace = true
11
+ hdf5-pure.workspace = true
11
12
  quick-xml.workspace = true
12
13
  schemars.workspace = true
13
14
  serde.workspace = true
@@ -4,7 +4,7 @@ use walkdir::WalkDir;
4
4
 
5
5
  use crate::parsers::{
6
6
  abaqus, comsol, flotherm, hfss, icepak, inspect_fluent_hdf5, inspect_mechanical_mechdb,
7
- simulink,
7
+ matfile, simulink,
8
8
  };
9
9
  use crate::{
10
10
  FormatSummary, InspectOptions, Result, ScanOptions, SimFormat, SimparseError, SimparseResult,
@@ -36,6 +36,8 @@ pub fn detect_format(path: &Path) -> Option<SimFormat> {
36
36
  Some(SimFormat::SimulinkSlx)
37
37
  } else if name.ends_with(".mdl") && simulink::looks_like_mdl(path) {
38
38
  Some(SimFormat::SimulinkMdl)
39
+ } else if name.ends_with(".mat") && matfile::looks_like_mat(path) {
40
+ Some(SimFormat::MatlabMat)
39
41
  } else {
40
42
  None
41
43
  }
@@ -72,6 +74,7 @@ pub fn inspect_path(path: impl AsRef<Path>, options: InspectOptions) -> Result<S
72
74
  path,
73
75
  options.max_text_bytes,
74
76
  )?),
77
+ SimFormat::MatlabMat => FormatSummary::MatFile(Box::new(matfile::inspect_mat_file(path)?)),
75
78
  SimFormat::SimulinkSlx | SimFormat::SimulinkMdl => FormatSummary::Simulink(Box::new(
76
79
  crate::parsers::simulink::inspect_simulink_model(path)?,
77
80
  )),
@@ -180,6 +180,12 @@ pub(crate) fn project(result: &SimparseResult) -> Value {
180
180
  attrs.insert("archive_entry_count".into(), json!(data.entry_count));
181
181
  attrs.insert("source_truncated".into(), json!(data.truncated));
182
182
  }
183
+ FormatSummary::MatFile(data) => {
184
+ namespace = "mathworks.mat";
185
+ attrs.insert("variable_count".into(), json!(data.variable_count));
186
+ attrs.insert("format_version".into(), json!(data.version));
187
+ attrs.insert("source_truncated".into(), json!(data.truncated));
188
+ }
183
189
  FormatSummary::Simulink(data) => {
184
190
  namespace = "mathworks.simulink";
185
191
  features.names("physics", "physics", &data.physical_modeling_products);
@@ -15,6 +15,7 @@ pub use parsers::hfss::inspect_hfss_aedt;
15
15
  pub use parsers::icepak::inspect_icepak_tzr;
16
16
  pub use parsers::inspect_fluent_hdf5;
17
17
  pub use parsers::inspect_mechanical_mechdb;
18
+ pub use parsers::matfile::{MatFileSummary, MatVariable, inspect_mat_file};
18
19
  pub use parsers::simulink::{
19
20
  SimulinkModelSummary, SimulinkSolver, StateflowCounts, inspect_simulink_model,
20
21
  };
@@ -0,0 +1,499 @@
1
+ //! MATLAB MAT-file inventory.
2
+ use std::collections::HashMap;
3
+ use std::io::{BufReader, Read, Seek, SeekFrom};
4
+ use std::path::Path;
5
+
6
+ use flate2::read::ZlibDecoder;
7
+ use hdf5_pure::{AttrValue, DType, File};
8
+ use schemars::JsonSchema;
9
+ use serde::{Deserialize, Serialize};
10
+
11
+ use crate::{Result, SimparseError};
12
+
13
+ const MAX_VARIABLES: usize = 10_000;
14
+ const MAX_HEADER_BYTES: u64 = 1 << 20;
15
+ const MAX_HDF5_BYTES: u64 = 1 << 30;
16
+
17
+ #[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
18
+ pub struct MatFileSummary {
19
+ /// `4`, `5` (v5, v6, v7) or `7.3`.
20
+ pub version: String,
21
+ pub platform: Option<String>,
22
+ pub created: Option<String>,
23
+ pub endian: Option<String>,
24
+ pub compressed: Option<bool>,
25
+ pub subsystem_data: bool,
26
+ pub variable_count: usize,
27
+ pub variables: Vec<MatVariable>,
28
+ pub truncated: bool,
29
+ }
30
+
31
+ #[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
32
+ pub struct MatVariable {
33
+ pub name: String,
34
+ /// Object variables report their stored class name.
35
+ pub class: String,
36
+ /// Empty when unknown.
37
+ pub dims: Vec<u64>,
38
+ pub complex: bool,
39
+ pub sparse: bool,
40
+ pub global: bool,
41
+ pub fields: Vec<String>,
42
+ pub stored_bytes: u64,
43
+ }
44
+
45
+ pub fn looks_like_mat(path: &Path) -> bool {
46
+ let mut head = [0u8; 19];
47
+ let signed = std::fs::File::open(path)
48
+ .and_then(|mut f| f.read_exact(&mut head))
49
+ .is_ok()
50
+ && (head.starts_with(b"MATLAB 5.0 MAT-file") || head.starts_with(b"MATLAB 7.3 MAT-file"));
51
+ signed || read_v4(path).is_some()
52
+ }
53
+
54
+ pub fn inspect_mat_file(path: &Path) -> Result<MatFileSummary> {
55
+ let mut file = std::fs::File::open(path)?;
56
+ let len = file.metadata()?.len();
57
+ let mut header = [0u8; 128];
58
+ let full = file.read_exact(&mut header).is_ok();
59
+ if !full || !header.starts_with(b"MATLAB ") {
60
+ let variables =
61
+ read_v4(path).ok_or_else(|| SimparseError::Parse("not a MAT file".into()))?;
62
+ return Ok(MatFileSummary {
63
+ version: "4".into(),
64
+ variable_count: variables.len(),
65
+ variables,
66
+ ..Default::default()
67
+ });
68
+ }
69
+ let text = String::from_utf8_lossy(&header[..116]);
70
+ let text = text.trim_end_matches(['\0', ' ']);
71
+ let mut out = MatFileSummary {
72
+ platform: between(text, "Platform: ", ","),
73
+ created: between(text, "Created on: ", " HDF5").or_else(|| {
74
+ text.split("Created on: ")
75
+ .nth(1)
76
+ .map(|s| s.trim().to_string())
77
+ }),
78
+ ..Default::default()
79
+ };
80
+ if text.starts_with("MATLAB 7.3") {
81
+ out.version = "7.3".into();
82
+ inspect_hdf5(path, len, &mut out)?;
83
+ } else {
84
+ out.version = "5".into();
85
+ inspect_v5(file, len, &header, &mut out)?;
86
+ }
87
+ out.variable_count = out.variables.len();
88
+ Ok(out)
89
+ }
90
+
91
+ fn read_v4(path: &Path) -> Option<Vec<MatVariable>> {
92
+ let mut file = std::fs::File::open(path).ok()?;
93
+ let len = file.metadata().ok()?.len();
94
+ let mut pos = 0u64;
95
+ let mut out = Vec::new();
96
+ while pos < len {
97
+ let mut head = [0u8; 20];
98
+ file.seek(SeekFrom::Start(pos)).ok()?;
99
+ file.read_exact(&mut head).ok()?;
100
+ let mut fields = [0u32; 5];
101
+ let endian = [Endian::Little, Endian::Big].into_iter().find(|e| {
102
+ let t = e.u32(&head[..4]);
103
+ t < 2000 && (t / 100) % 10 == 0 && (t / 10) % 10 <= 5 && t % 10 <= 2
104
+ })?;
105
+ for (i, f) in fields.iter_mut().enumerate() {
106
+ *f = endian.u32(&head[i * 4..]);
107
+ }
108
+ let [kind, rows, cols, imag, name_len] = fields;
109
+ if imag > 1 || name_len == 0 || name_len > 4096 || (kind % 10 == 1 && imag == 1) {
110
+ return None;
111
+ }
112
+ let width = [8u64, 4, 4, 2, 2, 1][((kind / 10) % 10) as usize];
113
+ let data = u64::from(rows) * u64::from(cols) * width * (1 + u64::from(imag));
114
+ let mut name = vec![0u8; name_len as usize];
115
+ file.read_exact(&mut name).ok()?;
116
+ if name.last() != Some(&0) || !name[..name.len() - 1].iter().all(|b| b.is_ascii_graphic()) {
117
+ return None;
118
+ }
119
+ let data_start = pos + 20 + u64::from(name_len);
120
+ let mut v = MatVariable {
121
+ name: String::from_utf8_lossy(&name[..name.len() - 1]).into(),
122
+ class: if kind % 10 == 1 { "char" } else { "double" }.into(),
123
+ dims: vec![u64::from(rows), u64::from(cols)],
124
+ complex: imag == 1,
125
+ sparse: kind % 10 == 2,
126
+ stored_bytes: 20 + u64::from(name_len) + data,
127
+ ..Default::default()
128
+ };
129
+ if v.sparse && width == 8 && rows > 0 && cols >= 2 {
130
+ let mut cell = |col: u64| -> Option<u64> {
131
+ let mut b = [0u8; 8];
132
+ file.seek(SeekFrom::Start(
133
+ data_start + (col * u64::from(rows) + u64::from(rows) - 1) * 8,
134
+ ))
135
+ .ok()?;
136
+ file.read_exact(&mut b).ok()?;
137
+ let value = match endian {
138
+ Endian::Little => f64::from_le_bytes(b),
139
+ Endian::Big => f64::from_be_bytes(b),
140
+ };
141
+ (value >= 0.0).then_some(value as u64)
142
+ };
143
+ v.dims = vec![cell(0)?, cell(1)?];
144
+ }
145
+ out.push(v);
146
+ pos = data_start + data;
147
+ }
148
+ (pos == len && !out.is_empty()).then_some(out)
149
+ }
150
+
151
+ fn between(text: &str, start: &str, end: &str) -> Option<String> {
152
+ let rest = text.split_once(start)?.1;
153
+ let value = rest.split_once(end).map_or(rest, |(v, _)| v).trim();
154
+ (!value.is_empty()).then(|| value.to_string())
155
+ }
156
+
157
+ #[derive(Clone, Copy)]
158
+ enum Endian {
159
+ Little,
160
+ Big,
161
+ }
162
+
163
+ impl Endian {
164
+ fn u32(self, b: &[u8]) -> u32 {
165
+ let a = [b[0], b[1], b[2], b[3]];
166
+ match self {
167
+ Self::Little => u32::from_le_bytes(a),
168
+ Self::Big => u32::from_be_bytes(a),
169
+ }
170
+ }
171
+
172
+ fn u64(self, b: &[u8]) -> u64 {
173
+ let a = [b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]];
174
+ match self {
175
+ Self::Little => u64::from_le_bytes(a),
176
+ Self::Big => u64::from_be_bytes(a),
177
+ }
178
+ }
179
+ }
180
+
181
+ const MI_INT8: u32 = 1;
182
+ const MI_INT32: u32 = 5;
183
+ const MI_UINT32: u32 = 6;
184
+ const MI_MATRIX: u32 = 14;
185
+ const MI_COMPRESSED: u32 = 15;
186
+
187
+ fn inspect_v5(
188
+ file: std::fs::File,
189
+ len: u64,
190
+ header: &[u8; 128],
191
+ out: &mut MatFileSummary,
192
+ ) -> Result<()> {
193
+ let endian = match &header[126..128] {
194
+ b"IM" => Endian::Little,
195
+ b"MI" => Endian::Big,
196
+ _ => return Err(SimparseError::Parse("unknown MAT endian indicator".into())),
197
+ };
198
+ out.endian = Some(match endian {
199
+ Endian::Little => "little".into(),
200
+ Endian::Big => "big".into(),
201
+ });
202
+ let raw = &header[116..124];
203
+ let subsystem = (!raw.iter().all(|b| *b == 0 || *b == b' ')).then(|| endian.u64(raw));
204
+ let mut compressed = false;
205
+ let mut reader = BufReader::new(file);
206
+ let mut pos = 128u64;
207
+ while pos + 8 <= len {
208
+ reader.seek(SeekFrom::Start(pos))?;
209
+ let mut tag = [0u8; 8];
210
+ reader.read_exact(&mut tag)?;
211
+ let first = endian.u32(&tag[..4]);
212
+ let (kind, size, total) = if first >> 16 != 0 {
213
+ (first & 0xffff, u64::from(first >> 16), 8)
214
+ } else {
215
+ let size = u64::from(endian.u32(&tag[4..]));
216
+ let total = if first == MI_COMPRESSED {
217
+ 8 + size
218
+ } else {
219
+ 8 + size.div_ceil(8) * 8
220
+ };
221
+ (first, size, total)
222
+ };
223
+ if pos + total > len + 7 {
224
+ out.truncated = true;
225
+ break;
226
+ }
227
+ if Some(pos) == subsystem {
228
+ out.subsystem_data = true;
229
+ } else if kind == MI_MATRIX || kind == MI_COMPRESSED {
230
+ let mut buf = Vec::new();
231
+ let limited = (&mut reader).take(size.min(MAX_HEADER_BYTES));
232
+ if kind == MI_COMPRESSED {
233
+ compressed = true;
234
+ let _ = ZlibDecoder::new(limited)
235
+ .take(MAX_HEADER_BYTES)
236
+ .read_to_end(&mut buf);
237
+ buf = strip_matrix_tag(&buf, endian).unwrap_or_default();
238
+ } else {
239
+ limited.take(MAX_HEADER_BYTES).read_to_end(&mut buf)?;
240
+ }
241
+ match parse_matrix(&buf, endian) {
242
+ Some(mut variable) if out.variables.len() < MAX_VARIABLES => {
243
+ variable.stored_bytes = size;
244
+ out.variables.push(variable);
245
+ }
246
+ Some(_) => out.truncated = true,
247
+ None => out.truncated = true,
248
+ }
249
+ }
250
+ pos += total;
251
+ }
252
+ out.compressed = Some(compressed);
253
+ Ok(())
254
+ }
255
+
256
+ fn strip_matrix_tag(buf: &[u8], endian: Endian) -> Option<Vec<u8>> {
257
+ (buf.len() >= 8 && endian.u32(&buf[..4]) == MI_MATRIX).then(|| buf[8..].to_vec())
258
+ }
259
+
260
+ struct Cursor<'a> {
261
+ buf: &'a [u8],
262
+ pos: usize,
263
+ endian: Endian,
264
+ }
265
+
266
+ impl<'a> Cursor<'a> {
267
+ fn next(&mut self) -> Option<(u32, &'a [u8])> {
268
+ let tag = self.buf.get(self.pos..self.pos + 8)?;
269
+ let first = self.endian.u32(&tag[..4]);
270
+ if first >> 16 != 0 {
271
+ let size = (first >> 16) as usize;
272
+ let data = self.buf.get(self.pos + 4..self.pos + 4 + size.min(4))?;
273
+ self.pos += 8;
274
+ return Some((first & 0xffff, data));
275
+ }
276
+ let size = self.endian.u32(&tag[4..]) as usize;
277
+ let data = self.buf.get(self.pos + 8..self.pos + 8 + size)?;
278
+ self.pos += 8 + size.div_ceil(8) * 8;
279
+ Some((first, data))
280
+ }
281
+
282
+ fn text(&mut self) -> Option<String> {
283
+ let (kind, data) = self.next()?;
284
+ (kind == MI_INT8).then(|| {
285
+ String::from_utf8_lossy(data)
286
+ .trim_end_matches('\0')
287
+ .to_string()
288
+ })
289
+ }
290
+ }
291
+
292
+ fn parse_matrix(buf: &[u8], endian: Endian) -> Option<MatVariable> {
293
+ let mut c = Cursor {
294
+ buf,
295
+ pos: 0,
296
+ endian,
297
+ };
298
+ let (kind, flags) = c.next()?;
299
+ if kind != MI_UINT32 || flags.len() < 4 {
300
+ return None;
301
+ }
302
+ let word = endian.u32(flags);
303
+ let class_id = word & 0xff;
304
+ let bits = (word >> 8) & 0xff;
305
+ let mut v = MatVariable {
306
+ complex: bits & 0x08 != 0,
307
+ global: bits & 0x04 != 0,
308
+ sparse: class_id == 5,
309
+ ..Default::default()
310
+ };
311
+ let logical = bits & 0x02 != 0;
312
+ if class_id == 17 {
313
+ v.name = c.text()?;
314
+ let _type_system = c.text()?;
315
+ v.class = c.text()?;
316
+ return Some(v);
317
+ }
318
+ let (kind, dims) = c.next()?;
319
+ if kind != MI_INT32 {
320
+ return None;
321
+ }
322
+ v.dims = dims
323
+ .chunks_exact(4)
324
+ .map(|d| u64::from(endian.u32(d)))
325
+ .collect();
326
+ v.name = c.text()?;
327
+ v.class = match class_id {
328
+ 1 => "cell".into(),
329
+ 2 => "struct".into(),
330
+ 3 => c.text().unwrap_or_default(),
331
+ 4 => "char".into(),
332
+ 5 | 9 if logical => "logical".into(),
333
+ 5 | 6 => "double".into(),
334
+ 7 => "single".into(),
335
+ 8 => "int8".into(),
336
+ 9 => "uint8".into(),
337
+ 10 => "int16".into(),
338
+ 11 => "uint16".into(),
339
+ 12 => "int32".into(),
340
+ 13 => "uint32".into(),
341
+ 14 => "int64".into(),
342
+ 15 => "uint64".into(),
343
+ 16 => "function_handle".into(),
344
+ other => format!("class-{other}"),
345
+ };
346
+ if matches!(class_id, 2 | 3)
347
+ && let Some((MI_INT32, width)) = c.next()
348
+ && width.len() >= 4
349
+ && let Some((MI_INT8, names)) = c.next()
350
+ {
351
+ let width = endian.u32(width) as usize;
352
+ if width > 0 {
353
+ v.fields = names
354
+ .chunks(width)
355
+ .map(|n| {
356
+ String::from_utf8_lossy(n)
357
+ .trim_end_matches('\0')
358
+ .to_string()
359
+ })
360
+ .filter(|n| !n.is_empty())
361
+ .collect();
362
+ }
363
+ }
364
+ Some(v)
365
+ }
366
+
367
+ fn inspect_hdf5(path: &Path, len: u64, out: &mut MatFileSummary) -> Result<()> {
368
+ if len > MAX_HDF5_BYTES {
369
+ out.truncated = true;
370
+ return Ok(());
371
+ }
372
+ let file = File::open(path).map_err(hdf5_err)?;
373
+ let root = file.root();
374
+ let mut groups = root.groups().map_err(hdf5_err)?;
375
+ let datasets = root.datasets().map_err(hdf5_err)?;
376
+ out.subsystem_data = groups.iter().any(|g| g == "#subsystem#");
377
+ groups.retain(|g| !g.starts_with('#'));
378
+ let mut names: Vec<(String, bool)> = datasets
379
+ .into_iter()
380
+ .filter(|d| !d.starts_with('#'))
381
+ .map(|d| (d, true))
382
+ .chain(groups.into_iter().map(|g| (g, false)))
383
+ .collect();
384
+ names.sort();
385
+ for (name, is_dataset) in names {
386
+ if out.variables.len() >= MAX_VARIABLES {
387
+ out.truncated = true;
388
+ break;
389
+ }
390
+ let variable = if is_dataset {
391
+ hdf5_dataset(&file, &name)
392
+ } else {
393
+ hdf5_group(&file, &name)
394
+ };
395
+ match variable {
396
+ Ok(v) => out.variables.push(v),
397
+ Err(_) => out.truncated = true,
398
+ }
399
+ }
400
+ Ok(())
401
+ }
402
+
403
+ fn hdf5_err(error: hdf5_pure::Error) -> SimparseError {
404
+ SimparseError::Hdf5(error.to_string())
405
+ }
406
+
407
+ fn attr_text(attrs: &HashMap<String, AttrValue>, key: &str) -> Option<String> {
408
+ match attrs.get(key)? {
409
+ AttrValue::String(s) | AttrValue::AsciiString(s) => Some(s.trim_end_matches('\0').into()),
410
+ _ => None,
411
+ }
412
+ }
413
+
414
+ fn attr_u64(attrs: &HashMap<String, AttrValue>, key: &str) -> Option<u64> {
415
+ match attrs.get(key)? {
416
+ AttrValue::U32(v) => Some(u64::from(*v)),
417
+ AttrValue::U64(v) => Some(*v),
418
+ AttrValue::I32(v) => u64::try_from(*v).ok(),
419
+ AttrValue::I64(v) => u64::try_from(*v).ok(),
420
+ AttrValue::F64(v) if *v >= 0.0 => Some(*v as u64),
421
+ _ => None,
422
+ }
423
+ }
424
+
425
+ fn attr_list(attrs: &HashMap<String, AttrValue>, key: &str) -> Vec<String> {
426
+ match attrs.get(key) {
427
+ Some(
428
+ AttrValue::StringArray(v)
429
+ | AttrValue::AsciiStringArray(v)
430
+ | AttrValue::VarLenAsciiArray(v),
431
+ ) => v.clone(),
432
+ _ => Vec::new(),
433
+ }
434
+ }
435
+
436
+ fn hdf5_dataset(file: &File, name: &str) -> std::result::Result<MatVariable, hdf5_pure::Error> {
437
+ let ds = file.dataset(name)?;
438
+ let attrs = ds.attrs()?;
439
+ let class = attr_text(&attrs, "MATLAB_class").unwrap_or_else(|| "unknown".into());
440
+ let mut dims: Vec<u64> = ds.shape()?.into_iter().rev().collect();
441
+ if attr_u64(&attrs, "MATLAB_empty") == Some(1) {
442
+ dims = ds.read_u64().unwrap_or_default();
443
+ }
444
+ if attrs.contains_key("MATLAB_object_decode") {
445
+ dims.clear();
446
+ }
447
+ let complex = matches!(ds.dtype()?, DType::Compound(f) if f.iter().any(|(n, _)| n == "imag"));
448
+ Ok(MatVariable {
449
+ name: name.into(),
450
+ class,
451
+ dims,
452
+ complex,
453
+ global: attr_u64(&attrs, "MATLAB_global") == Some(1),
454
+ ..Default::default()
455
+ })
456
+ }
457
+
458
+ fn hdf5_group(file: &File, name: &str) -> std::result::Result<MatVariable, hdf5_pure::Error> {
459
+ let group = file.group(name)?;
460
+ let attrs = group.attrs()?;
461
+ let class = attr_text(&attrs, "MATLAB_class").unwrap_or_else(|| "struct".into());
462
+ let mut v = MatVariable {
463
+ name: name.into(),
464
+ class,
465
+ global: attr_u64(&attrs, "MATLAB_global") == Some(1),
466
+ ..Default::default()
467
+ };
468
+ if let Some(rows) = attr_u64(&attrs, "MATLAB_sparse") {
469
+ v.sparse = true;
470
+ let cols = group
471
+ .dataset("jc")
472
+ .and_then(|jc| jc.shape())
473
+ .map(|s| s.iter().product::<u64>().saturating_sub(1))
474
+ .unwrap_or(0);
475
+ v.dims = vec![rows, cols];
476
+ v.complex = group
477
+ .dataset("data")
478
+ .and_then(|d| d.dtype())
479
+ .is_ok_and(|t| matches!(t, DType::Compound(f) if f.iter().any(|(n, _)| n == "imag")));
480
+ return Ok(v);
481
+ }
482
+ let mut members = group.datasets()?;
483
+ members.extend(group.groups()?);
484
+ members.sort();
485
+ v.fields = attr_list(&attrs, "MATLAB_fields");
486
+ if v.fields.is_empty() {
487
+ v.fields = members.clone();
488
+ }
489
+ v.dims = members
490
+ .iter()
491
+ .find_map(|m| {
492
+ let ds = group.dataset(m).ok()?;
493
+ matches!(ds.dtype().ok()?, DType::ObjectReference)
494
+ .then(|| ds.shape().ok().map(|s| s.into_iter().rev().collect()))
495
+ .flatten()
496
+ })
497
+ .unwrap_or_else(|| vec![1, 1]);
498
+ Ok(v)
499
+ }
@@ -3,6 +3,7 @@ pub mod comsol;
3
3
  pub mod flotherm;
4
4
  pub mod hfss;
5
5
  pub mod icepak;
6
+ pub mod matfile;
6
7
  pub mod simulink;
7
8
  pub mod step;
8
9
 
@@ -18,6 +18,7 @@ pub enum SimFormat {
18
18
  FlothermPack,
19
19
  SimulinkSlx,
20
20
  SimulinkMdl,
21
+ MatlabMat,
21
22
  }
22
23
 
23
24
  impl FromStr for SimFormat {
@@ -37,6 +38,7 @@ impl FromStr for SimFormat {
37
38
  "flotherm-pack" | "pack" => Ok(Self::FlothermPack),
38
39
  "simulink-slx" | "slx" => Ok(Self::SimulinkSlx),
39
40
  "simulink-mdl" | "mdl" => Ok(Self::SimulinkMdl),
41
+ "matlab-mat" | "mat" => Ok(Self::MatlabMat),
40
42
  other => Err(format!("unknown format: {other}")),
41
43
  }
42
44
  }
@@ -56,6 +58,7 @@ impl std::fmt::Display for SimFormat {
56
58
  Self::FlothermPack => "flotherm-pack",
57
59
  Self::SimulinkSlx => "simulink-slx",
58
60
  Self::SimulinkMdl => "simulink-mdl",
61
+ Self::MatlabMat => "matlab-mat",
59
62
  };
60
63
  f.write_str(text)
61
64
  }
@@ -109,6 +112,7 @@ impl Default for ScanOptions {
109
112
  "*.pack".into(),
110
113
  "*.slx".into(),
111
114
  "*.mdl".into(),
115
+ "*.mat".into(),
112
116
  ],
113
117
  inspect: InspectOptions::default(),
114
118
  }
@@ -141,6 +145,7 @@ pub enum FormatSummary {
141
145
  FlothermFloxml(FlothermFloxmlSummary),
142
146
  FlothermPack(FlothermPackSummary),
143
147
  Simulink(Box<crate::parsers::simulink::SimulinkModelSummary>),
148
+ MatFile(Box<crate::parsers::matfile::MatFileSummary>),
144
149
  }
145
150
 
146
151
  #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
@@ -4,7 +4,8 @@ use crate::{
4
4
  AbaqusInpSummary, FlothermBoundary, FlothermEntity, FlothermFloxmlSummary, FlothermGridAxis,
5
5
  FlothermPackEntry, FlothermPackSummary, FlothermSource, FormatSummary, HfssAedtSummary,
6
6
  HfssDesign, IcepakAedtSummary, IcepakBoundary, IcepakDesign, IcepakTzrEntry, IcepakTzrSummary,
7
- NamedCount, NamedValue, SimparseResult, SimulinkModelSummary, SimulinkSolver, StateflowCounts,
7
+ MatFileSummary, NamedCount, NamedValue, SimparseResult, SimulinkModelSummary, SimulinkSolver,
8
+ StateflowCounts,
8
9
  };
9
10
  use simparse_hdf5::{FluentHdf5Summary, Hdf5AttributeInfo, Hdf5DatasetInfo, Hdf5GroupInfo};
10
11
  use simparse_hdf5::{MechanicalMechdbSummary, MechanicalObjectTypeCount, MechanicalStreamInfo};
@@ -99,6 +100,10 @@ pub fn summarize_result(result: &SimparseResult) -> SimparseSummaryResult {
99
100
  CompactFormatSummary::Simulink(Box::new(compact_simulink(value))),
100
101
  vec!["Model structure and stored configuration only; parameters are not evaluated.".into()],
101
102
  ),
103
+ FormatSummary::MatFile(value) => (
104
+ CompactFormatSummary::MatFile(Box::new(compact_mat(value))),
105
+ vec!["Variable inventory only; data are not loaded.".into()],
106
+ ),
102
107
  };
103
108
 
104
109
  SimparseSummaryResult {
@@ -429,6 +434,28 @@ fn compact_simulink(value: &SimulinkModelSummary) -> CompactSimulinkModelSummary
429
434
  }
430
435
  }
431
436
 
437
+ fn compact_mat(value: &MatFileSummary) -> CompactMatFileSummary {
438
+ CompactMatFileSummary {
439
+ version: value.version.clone(),
440
+ platform: option_text(value.platform.as_deref()),
441
+ created: option_text(value.created.as_deref()),
442
+ endian: value.endian.clone(),
443
+ compressed: value.compressed,
444
+ subsystem_data: value.subsystem_data,
445
+ variables: bounded_map(&value.variables, |v| CompactMatVariable {
446
+ name: bounded_text(&v.name),
447
+ class: bounded_text(&v.class),
448
+ dims: v.dims.iter().take(SUMMARY_SAMPLE_LIMIT).copied().collect(),
449
+ complex: v.complex,
450
+ sparse: v.sparse,
451
+ global: v.global,
452
+ fields: bounded_texts(&v.fields),
453
+ stored_bytes: v.stored_bytes,
454
+ }),
455
+ truncated: value.truncated,
456
+ }
457
+ }
458
+
432
459
  fn bounded_texts(values: &[String]) -> BoundedList<String> {
433
460
  bounded_map(values, |value| bounded_text(value))
434
461
  }
@@ -505,6 +532,7 @@ pub enum CompactFormatSummary {
505
532
  FlothermFloxml(CompactFlothermFloxmlSummary),
506
533
  FlothermPack(CompactFlothermPackSummary),
507
534
  Simulink(Box<CompactSimulinkModelSummary>),
535
+ MatFile(Box<CompactMatFileSummary>),
508
536
  }
509
537
 
510
538
  #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
@@ -683,6 +711,30 @@ pub struct CompactFlothermFloxmlSummary {
683
711
  pub source_truncated: bool,
684
712
  }
685
713
 
714
+ #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
715
+ pub struct CompactMatFileSummary {
716
+ pub version: String,
717
+ pub platform: Option<String>,
718
+ pub created: Option<String>,
719
+ pub endian: Option<String>,
720
+ pub compressed: Option<bool>,
721
+ pub subsystem_data: bool,
722
+ pub variables: BoundedList<CompactMatVariable>,
723
+ pub truncated: bool,
724
+ }
725
+
726
+ #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
727
+ pub struct CompactMatVariable {
728
+ pub name: String,
729
+ pub class: String,
730
+ pub dims: Vec<u64>,
731
+ pub complex: bool,
732
+ pub sparse: bool,
733
+ pub global: bool,
734
+ pub fields: BoundedList<String>,
735
+ pub stored_bytes: u64,
736
+ }
737
+
686
738
  #[derive(Debug, Clone, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
687
739
  pub struct CompactSimulinkModelSummary {
688
740
  pub container: String,
@@ -0,0 +1,296 @@
1
+ use std::io::Write;
2
+
3
+ use flate2::Compression;
4
+ use flate2::write::ZlibEncoder;
5
+ use hdf5_pure::{AttrValue, FileBuilder};
6
+ use simparse_core::{
7
+ CompactFormatSummary, FormatSummary, InspectOptions, ScanOptions, SimFormat, detect_format,
8
+ inspect_path, scan_paths, summarize_result,
9
+ };
10
+ use tempfile::tempdir;
11
+
12
+ #[derive(Clone, Copy)]
13
+ enum E {
14
+ Le,
15
+ Be,
16
+ }
17
+
18
+ fn u32b(e: E, v: u32) -> [u8; 4] {
19
+ match e {
20
+ E::Le => v.to_le_bytes(),
21
+ E::Be => v.to_be_bytes(),
22
+ }
23
+ }
24
+
25
+ fn element(e: E, kind: u32, data: &[u8]) -> Vec<u8> {
26
+ let mut out = u32b(e, kind).to_vec();
27
+ out.extend(u32b(e, data.len() as u32));
28
+ out.extend(data);
29
+ out.resize(out.len().div_ceil(8) * 8, 0);
30
+ out
31
+ }
32
+
33
+ fn small(e: E, kind: u32, data: &[u8]) -> Vec<u8> {
34
+ let mut out = u32b(e, ((data.len() as u32) << 16) | kind).to_vec();
35
+ let mut d = data.to_vec();
36
+ d.resize(4, 0);
37
+ out.extend(d);
38
+ out
39
+ }
40
+
41
+ fn matrix(e: E, class: u32, bits: u32, dims: &[u32], name: &str, extra: &[u8]) -> Vec<u8> {
42
+ let mut body = element(e, 6, &[u32b(e, (bits << 8) | class), [0; 4]].concat());
43
+ body.extend(element(
44
+ e,
45
+ 5,
46
+ &dims.iter().flat_map(|d| u32b(e, *d)).collect::<Vec<_>>(),
47
+ ));
48
+ body.extend(element(e, 1, name.as_bytes()));
49
+ body.extend(extra);
50
+ element(e, 14, &body)
51
+ }
52
+
53
+ fn header(e: E, text: &str, subsystem: u64) -> Vec<u8> {
54
+ let mut h = text.as_bytes().to_vec();
55
+ h.resize(116, b' ');
56
+ h.extend(match e {
57
+ E::Le => subsystem.to_le_bytes(),
58
+ E::Be => subsystem.to_be_bytes(),
59
+ });
60
+ h.extend(match e {
61
+ E::Le => [0x00, 0x01, b'I', b'M'],
62
+ E::Be => [0x01, 0x00, b'M', b'I'],
63
+ });
64
+ h
65
+ }
66
+
67
+ fn compressed(e: E, element_bytes: &[u8]) -> Vec<u8> {
68
+ let mut z = ZlibEncoder::new(Vec::new(), Compression::default());
69
+ z.write_all(element_bytes).unwrap();
70
+ let data = z.finish().unwrap();
71
+ let mut out = u32b(e, 15).to_vec();
72
+ out.extend(u32b(e, data.len() as u32));
73
+ out.extend(data);
74
+ out
75
+ }
76
+
77
+ fn v5_file(e: E) -> Vec<u8> {
78
+ let real = element(e, 9, &[0u8; 48]);
79
+ let fields = [
80
+ small(e, 5, &u32b(e, 8)),
81
+ element(e, 1, b"alpha\0\0\0beta\0\0\0\0"),
82
+ ]
83
+ .concat();
84
+ let mut vars = vec![
85
+ matrix(e, 6, 0, &[2, 3], "x", &real),
86
+ matrix(e, 9, 0x02, &[1, 4], "flags", &element(e, 2, &[1, 0, 1, 1])),
87
+ matrix(
88
+ e,
89
+ 4,
90
+ 0,
91
+ &[1, 3],
92
+ "label",
93
+ &element(e, 4, &[97, 0, 98, 0, 99, 0]),
94
+ ),
95
+ matrix(e, 2, 0, &[1, 1], "cfg", &fields),
96
+ matrix(e, 6, 0x08 | 0x04, &[1, 2], "z", &real),
97
+ matrix(e, 5, 0, &[3, 4], "sp", &[]),
98
+ matrix(e, 1, 0, &[2, 1], "c", &[]),
99
+ ];
100
+ vars.push(compressed(e, &matrix(e, 13, 0, &[5, 1], "packed", &[])));
101
+ let mut out = header(
102
+ e,
103
+ "MATLAB 5.0 MAT-file, Platform: TEST64, Created on: Thu Jan 1 00:00:00 2099",
104
+ 0,
105
+ );
106
+ for v in vars {
107
+ out.extend(v);
108
+ }
109
+ let subsystem = out.len() as u64;
110
+ out.extend(matrix(e, 9, 0, &[1, 8], "", &[]));
111
+ out[116..124].copy_from_slice(&match e {
112
+ E::Le => subsystem.to_le_bytes(),
113
+ E::Be => subsystem.to_be_bytes(),
114
+ });
115
+ out
116
+ }
117
+
118
+ fn check_v5(path: &std::path::Path, endian: &str) {
119
+ assert_eq!(detect_format(path), Some(SimFormat::MatlabMat));
120
+ let result = inspect_path(path, InspectOptions::default()).unwrap();
121
+ let FormatSummary::MatFile(s) = &result.summary else {
122
+ panic!("expected MAT summary");
123
+ };
124
+ assert_eq!(s.version, "5");
125
+ assert_eq!(s.platform.as_deref(), Some("TEST64"));
126
+ assert_eq!(s.endian.as_deref(), Some(endian));
127
+ assert_eq!(s.compressed, Some(true));
128
+ assert!(s.subsystem_data);
129
+ assert!(!s.truncated);
130
+ let names: Vec<_> = s.variables.iter().map(|v| v.name.as_str()).collect();
131
+ assert_eq!(
132
+ names,
133
+ ["x", "flags", "label", "cfg", "z", "sp", "c", "packed"]
134
+ );
135
+ let v = |n: &str| s.variables.iter().find(|v| v.name == n).unwrap();
136
+ assert_eq!(
137
+ (v("x").class.as_str(), v("x").dims.clone()),
138
+ ("double", vec![2, 3])
139
+ );
140
+ assert_eq!(v("flags").class, "logical");
141
+ assert_eq!(v("label").class, "char");
142
+ assert_eq!(v("cfg").class, "struct");
143
+ assert_eq!(v("cfg").fields, ["alpha", "beta"]);
144
+ assert!(v("z").complex && v("z").global);
145
+ assert!(v("sp").sparse);
146
+ assert_eq!(v("c").class, "cell");
147
+ assert_eq!(
148
+ (v("packed").class.as_str(), v("packed").dims.clone()),
149
+ ("uint32", vec![5, 1])
150
+ );
151
+
152
+ let summary = summarize_result(&result);
153
+ let CompactFormatSummary::MatFile(c) = &summary.summary else {
154
+ panic!("expected compact MAT summary");
155
+ };
156
+ assert_eq!(c.variables.total, 8);
157
+ assert!(!c.variables.truncated);
158
+ let attrs = &result.ir.as_ref().unwrap()["dialects"]["cae"]["extensions"]["mathworks.mat"]["attributes"];
159
+ assert_eq!(attrs["variable_count"], 8);
160
+ }
161
+
162
+ #[test]
163
+ fn inspects_v5_little_and_big_endian() {
164
+ let tmp = tempdir().unwrap();
165
+ for (e, name, endian) in [(E::Le, "le.mat", "little"), (E::Be, "be.mat", "big")] {
166
+ let path = tmp.path().join(name);
167
+ std::fs::write(&path, v5_file(e)).unwrap();
168
+ check_v5(&path, endian);
169
+ }
170
+ }
171
+
172
+ #[test]
173
+ fn inspects_v73_hdf5() {
174
+ let tmp = tempdir().unwrap();
175
+ let path = tmp.path().join("big.mat");
176
+ let mut b = FileBuilder::new();
177
+ b.with_userblock(512);
178
+ b.create_dataset("x")
179
+ .with_f64_data(&[0.0; 6])
180
+ .with_shape(&[3, 2])
181
+ .set_attr("MATLAB_class", AttrValue::AsciiString("double".into()));
182
+ b.create_dataset("e")
183
+ .with_u64_data(&[0, 3])
184
+ .set_attr("MATLAB_class", AttrValue::AsciiString("double".into()))
185
+ .set_attr("MATLAB_empty", AttrValue::U32(1));
186
+ b.create_dataset("g")
187
+ .with_f64_data(&[1.0])
188
+ .set_attr("MATLAB_class", AttrValue::AsciiString("double".into()))
189
+ .set_attr("MATLAB_global", AttrValue::U32(1));
190
+ let mut st = b.create_group("cfg");
191
+ st.set_attr("MATLAB_class", AttrValue::AsciiString("struct".into()));
192
+ st.set_attr(
193
+ "MATLAB_fields",
194
+ AttrValue::AsciiStringArray(vec!["a".into(), "b".into()]),
195
+ );
196
+ st.create_dataset("a").with_f64_data(&[1.0]);
197
+ st.create_dataset("b").with_f64_data(&[2.0]);
198
+ b.add_group(st.finish());
199
+ let refs = b.create_group("#refs#");
200
+ b.add_group(refs.finish());
201
+ let sub = b.create_group("#subsystem#");
202
+ b.add_group(sub.finish());
203
+ b.write(&path).unwrap();
204
+ let mut bytes = std::fs::read(&path).unwrap();
205
+ let head = header(
206
+ E::Le,
207
+ "MATLAB 7.3 MAT-file, Platform: TEST64, Created on: Thu Jan 1 00:00:00 2099 HDF5 schema 1.00 .",
208
+ 0,
209
+ );
210
+ bytes[..128].copy_from_slice(&head);
211
+ std::fs::write(&path, bytes).unwrap();
212
+
213
+ let result = inspect_path(&path, InspectOptions::default()).unwrap();
214
+ let FormatSummary::MatFile(s) = &result.summary else {
215
+ panic!("expected MAT summary");
216
+ };
217
+ assert_eq!(s.version, "7.3");
218
+ assert_eq!(s.created.as_deref(), Some("Thu Jan 1 00:00:00 2099"));
219
+ assert!(s.subsystem_data);
220
+ let names: Vec<_> = s.variables.iter().map(|v| v.name.as_str()).collect();
221
+ assert_eq!(names, ["cfg", "e", "g", "x"]);
222
+ let v = |n: &str| s.variables.iter().find(|v| v.name == n).unwrap();
223
+ assert_eq!(v("x").dims, [2, 3]);
224
+ assert_eq!(v("e").dims, [0, 3]);
225
+ assert!(v("g").global);
226
+ assert_eq!(v("cfg").class, "struct");
227
+ assert_eq!(v("cfg").fields, ["a", "b"]);
228
+ }
229
+
230
+ #[test]
231
+ fn skips_files_without_a_mat_header() {
232
+ let tmp = tempdir().unwrap();
233
+ std::fs::write(tmp.path().join("ok.mat"), v5_file(E::Le)).unwrap();
234
+ std::fs::write(tmp.path().join("empty.mat"), b"").unwrap();
235
+ std::fs::write(tmp.path().join("other.mat"), b"not a MAT file at all").unwrap();
236
+ let results = scan_paths(&[tmp.path().to_path_buf()], ScanOptions::default()).unwrap();
237
+ assert_eq!(results.len(), 1);
238
+ assert_eq!(results[0].format, SimFormat::MatlabMat);
239
+ }
240
+
241
+ fn v4_matrix(kind: u32, rows: u32, cols: u32, imag: u32, name: &str, data: &[f64]) -> Vec<u8> {
242
+ let mut out: Vec<u8> = [kind, rows, cols, imag, name.len() as u32 + 1]
243
+ .iter()
244
+ .flat_map(|v| v.to_le_bytes())
245
+ .collect();
246
+ out.extend(name.as_bytes());
247
+ out.push(0);
248
+ out.extend(data.iter().flat_map(|v| v.to_le_bytes()));
249
+ out
250
+ }
251
+
252
+ #[test]
253
+ fn inspects_level4_files() {
254
+ let tmp = tempdir().unwrap();
255
+ let path = tmp.path().join("old.mat");
256
+ let bytes = [
257
+ v4_matrix(0, 2, 3, 0, "a", &[0.0; 6]),
258
+ v4_matrix(0, 1, 2, 1, "z", &[0.0; 4]),
259
+ v4_matrix(1, 1, 2, 0, "t", &[104.0, 105.0]),
260
+ v4_matrix(2, 2, 3, 0, "s", &[1.0, 5.0, 2.0, 7.0, 3.0, 0.0]),
261
+ ]
262
+ .concat();
263
+ std::fs::write(&path, bytes).unwrap();
264
+ assert_eq!(detect_format(&path), Some(SimFormat::MatlabMat));
265
+ let result = inspect_path(&path, InspectOptions::default()).unwrap();
266
+ let FormatSummary::MatFile(s) = &result.summary else {
267
+ panic!("expected MAT summary");
268
+ };
269
+ assert_eq!(s.version, "4");
270
+ let got: Vec<_> = s
271
+ .variables
272
+ .iter()
273
+ .map(|v| {
274
+ (
275
+ v.name.as_str(),
276
+ v.class.as_str(),
277
+ v.dims.clone(),
278
+ v.complex,
279
+ v.sparse,
280
+ )
281
+ })
282
+ .collect();
283
+ assert_eq!(
284
+ got,
285
+ [
286
+ ("a", "double", vec![2, 3], false, false),
287
+ ("z", "double", vec![1, 2], true, false),
288
+ ("t", "char", vec![1, 2], false, false),
289
+ ("s", "double", vec![5, 7], false, true),
290
+ ]
291
+ );
292
+
293
+ let junk = tmp.path().join("junk.mat");
294
+ std::fs::write(&junk, [0u8, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]).unwrap();
295
+ assert_eq!(detect_format(&junk), None);
296
+ }
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "simparse"
7
- version = "0.3.4"
7
+ version = "0.3.5"
8
8
  description = "Lightweight metadata scanner for simulation project and case files."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
File without changes