molio 0.4.0

A library for reading chemical file formats
Documentation
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2025 William Bro-Jørgensen
// Copyright (c) 2020 Guillaume Fraux and contributors
//
// See LICENSE at the project root for full text.

use std::collections::HashMap;
use std::ops::{Deref, DerefMut};

use nalgebra::Matrix3;

use crate::error::CError;

const EPSILON: f64 = 1e-12;

#[derive(PartialEq, Clone, Debug)]
pub enum PropertyKind {
    Bool,
    Double,
    String,
    Vector3D,
    Matrix3x3,
    VectorXD,
}

#[derive(Debug, Clone)]
pub enum Property {
    Bool(bool),
    Double(f64),
    String(String),
    Vector3D([f64; 3]),
    Matrix3x3(Matrix3<f64>),
    VectorXD(Vec<f64>),
}

impl Default for Property {
    fn default() -> Self {
        Property::Bool(false)
    }
}

/// Returns `true` if `a` and `b` are both finite and within `epsilon` of each other.
/// Any `NaN` or infinite value always compares as `false`.
fn almost_eq(a: f64, b: f64, epsilon: f64) -> bool {
    // Reject NaN outright
    if a.is_nan() || b.is_nan() {
        return false;
    }
    // Reject infinities outright
    if a.is_infinite() || b.is_infinite() {
        return false;
    }
    // Both are finite: compare absolute difference
    (a - b).abs() <= epsilon
}

impl PartialEq for Property {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Property::Bool(a), Property::Bool(b)) => a == b,

            (Property::Double(a), Property::Double(b)) => almost_eq(*a, *b, EPSILON),

            (Property::String(a), Property::String(b)) => a == b,

            (Property::Vector3D(a), Property::Vector3D(b)) => a
                .iter()
                .zip(b.iter())
                .all(|(x, y)| almost_eq(*x, *y, EPSILON)),

            // nalgebra's Matrix3<f64>: compare each entry
            (Property::Matrix3x3(a), Property::Matrix3x3(b)) => {
                for i in 0..3 {
                    for j in 0..3 {
                        if !almost_eq(a[(i, j)], b[(i, j)], EPSILON) {
                            return false;
                        }
                    }
                }
                true
            }

            // variable-length vector of f64: same pattern
            (Property::VectorXD(a), Property::VectorXD(b)) => {
                if a.len() != b.len() {
                    return false;
                }
                a.iter()
                    .zip(b.iter())
                    .all(|(x, y)| almost_eq(*x, *y, EPSILON))
            }

            // different variants are never equal
            _ => false,
        }
    }
}

impl Eq for Property {}

#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct Properties(HashMap<String, Property>);

impl Properties {
    #[must_use]
    pub fn new() -> Self {
        Properties(HashMap::new())
    }
}

impl Deref for Properties {
    type Target = HashMap<String, Property>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Properties {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a> IntoIterator for &'a Properties {
    type Item = (&'a String, &'a Property);
    type IntoIter = <&'a HashMap<String, Property> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl<'a> IntoIterator for &'a mut Properties {
    type Item = (&'a String, &'a mut Property);
    type IntoIter = <&'a mut HashMap<String, Property> as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.0.iter_mut()
    }
}

impl Property {
    /// Returns the kind of this property.
    #[must_use]
    pub fn kind(&self) -> PropertyKind {
        match self {
            Property::Bool(_) => PropertyKind::Bool,
            Property::Double(_) => PropertyKind::Double,
            Property::String(_) => PropertyKind::String,
            Property::Vector3D(_) => PropertyKind::Vector3D,
            Property::Matrix3x3(_) => PropertyKind::Matrix3x3,
            Property::VectorXD(_) => PropertyKind::VectorXD,
        }
    }

    /// Returns the boolean value if this is a [`Property::Bool`] property, or `None` otherwise.
    #[must_use]
    pub fn as_bool(&self) -> Option<bool> {
        if let Property::Bool(b) = *self {
            Some(b)
        } else {
            None
        }
    }

    /// Returns the boolean value.
    ///
    /// # Panics
    ///
    /// Panics if the property is not [`Property::Bool`].
    #[must_use]
    pub fn expect_bool(&self) -> bool {
        match *self {
            Property::Bool(b) => b,
            ref other => panic!("expected Bool, found {other:?}"),
        }
    }

    /// Returns the double value if this is a [`Property::Double`] property, or `None` otherwise.
    #[must_use]
    pub fn as_double(&self) -> Option<f64> {
        if let Property::Double(x) = *self {
            Some(x)
        } else {
            None
        }
    }

    /// Returns the double value.
    ///
    /// # Panics
    ///
    /// Panics if the property is not [`Property::Double`].
    #[must_use]
    pub fn expect_double(&self) -> f64 {
        match *self {
            Property::Double(d) => d,
            ref other => panic!("expected Double, found {other:?}"),
        }
    }

    /// Returns the string slice if this is a [`Property::String`] property, or `None` otherwise.
    #[must_use]
    pub fn as_string(&self) -> Option<&str> {
        if let Property::String(ref s) = *self {
            Some(s)
        } else {
            None
        }
    }

    /// Returns the string slice.
    ///
    /// # Panics
    ///
    /// Panics if the property is not [`Property::String`].
    #[must_use]
    pub fn expect_string(&self) -> &str {
        match *self {
            Property::String(ref s) => s,
            ref other => panic!("expected String, found {other:?}"),
        }
    }

    /// Returns the 3D vector if this is a [`Property::Vector3D`] property, or `None` otherwise.
    #[must_use]
    pub fn as_vector3d(&self) -> Option<[f64; 3]> {
        if let Property::Vector3D(v) = *self {
            Some(v)
        } else {
            None
        }
    }

    /// Returns the 3D vector.
    ///
    /// # Panics
    ///
    /// Panics if the property is not [`Property::Vector3D`].
    #[must_use]
    pub fn expect_vector3d(&self) -> [f64; 3] {
        match *self {
            Property::Vector3D(v) => v,
            ref other => panic!("expected Vector3D, found {other:?}"),
        }
    }

    /// Returns the 3×3 matrix array if this is a [`Property::Matrix3x3`] property, or `None` otherwise.
    #[must_use]
    pub fn as_matrix3x3(&self) -> Option<[f64; 9]> {
        if let Property::Matrix3x3(m) = *self {
            let mut array = [0.0; 9];
            array.copy_from_slice(m.as_slice());
            Some(array)
        } else {
            None
        }
    }

    /// Returns the 3×3 matrix array.
    ///
    /// # Panics
    ///
    /// Panics if the property is not [`Property::Matrix3x3`].
    #[must_use]
    pub fn expect_matrix3x3(&self) -> [f64; 9] {
        match *self {
            Property::Matrix3x3(m) => {
                let mut array = [0.0; 9];
                array.copy_from_slice(m.as_slice());
                array
            }
            ref other => panic!("expected Matrix3x3, found {other:?}"),
        }
    }

    /// Parses the given string `value` into a [`Property`] of the specified [`PropertyKind`].
    ///
    /// # Errors
    ///
    /// Returns an error if `value` cannot be parsed into the requested kind.
    pub fn parse_value(value: &str, kind: &PropertyKind) -> Result<Property, CError> {
        match kind {
            PropertyKind::String => StringParser::parse(value),
            PropertyKind::Bool => BoolParser::parse(value),
            PropertyKind::Double => DoubleParser::parse(value),
            PropertyKind::Vector3D => Vector3DParser::parse(value),
            PropertyKind::Matrix3x3 => Matrix3x3Parser::parse(value),
            PropertyKind::VectorXD => VectorXDParser::parse(value),
        }
    }
}
/// Helper trait for parsing values into Property
pub trait ValueParser {
    fn parse(value: &str) -> Result<Property, CError>;
}

pub struct StringParser;
pub struct BoolParser;
pub struct DoubleParser;
pub struct Vector3DParser;
pub struct Matrix3x3Parser;
pub struct VectorXDParser;

impl ValueParser for StringParser {
    fn parse(value: &str) -> Result<Property, CError> {
        Ok(Property::String(value.to_string()))
    }
}

impl ValueParser for BoolParser {
    fn parse(value: &str) -> Result<Property, CError> {
        match value.to_lowercase().as_str() {
            "t" | "true" => Ok(Property::Bool(true)),
            "f" | "false" => Ok(Property::Bool(false)),
            _ => Err(CError::GenericError(format!(
                "Invalid boolean value: {value}"
            ))),
        }
    }
}

impl ValueParser for DoubleParser {
    fn parse(value: &str) -> Result<Property, CError> {
        value
            .parse::<f64>()
            .map(Property::Double)
            .map_err(|e| CError::GenericError(format!("Failed to parse number: {e}")))
    }
}

impl ValueParser for Vector3DParser {
    fn parse(value: &str) -> Result<Property, CError> {
        let parts: Vec<&str> = value.split_whitespace().collect();
        if parts.len() != 3 {
            return Err(CError::GenericError(format!(
                "Vector3D requires exactly 3 components, got {}: {parts:?}",
                parts.len()
            )));
        }
        let x = parts[0]
            .parse::<f64>()
            .map_err(|e| CError::GenericError(format!("Failed to parse x component: {e}")))?;
        let y = parts[1]
            .parse::<f64>()
            .map_err(|e| CError::GenericError(format!("Failed to parse y component: {e}")))?;
        let z = parts[2]
            .parse::<f64>()
            .map_err(|e| CError::GenericError(format!("Failed to parse z component: {e}")))?;
        Ok(Property::Vector3D([x, y, z]))
    }
}

impl ValueParser for Matrix3x3Parser {
    fn parse(value: &str) -> Result<Property, CError> {
        let parts: Vec<&str> = value.split_whitespace().collect();
        if parts.len() != 9 {
            return Err(CError::GenericError(format!(
                "Matrix3x3 requires exactly 9 components, got {}: {parts:?}",
                parts.len()
            )));
        }
        let nums: Result<Vec<f64>, _> = parts.iter().map(|s| s.parse::<f64>()).collect();
        nums.map(|n| Property::Matrix3x3(Matrix3::from_iterator(n)))
            .map_err(|e| CError::GenericError(format!("Failed to parse matrix components: {e}")))
    }
}

impl ValueParser for VectorXDParser {
    fn parse(value: &str) -> Result<Property, CError> {
        let parts: Vec<&str> = value.split_whitespace().collect();
        let nums: Result<Vec<f64>, _> = parts.iter().map(|s| s.parse::<f64>()).collect();
        nums.map(Property::VectorXD)
            .map_err(|e| CError::GenericError(format!("Failed to parse vector components: {e}")))
    }
}

#[derive(Debug)]
pub struct ExtendedProperty {
    pub name: String,
    pub kind: PropertyKind,
}

#[cfg(test)]
mod tests {
    use core::f64;

    use assert_approx_eq::assert_approx_eq;

    use super::*;

    #[test]
    fn test_bool_property() {
        let prop = Property::Bool(true);
        assert_eq!(prop.as_bool(), Some(true));
        assert!(prop.expect_bool());
        assert_eq!(prop.as_double(), None);
    }

    #[test]
    fn test_double_property() {
        let prop = Property::Double(f64::consts::PI);
        assert_eq!(prop.as_double(), Some(f64::consts::PI));
        assert_approx_eq!(prop.expect_double(), f64::consts::PI);
        assert_eq!(prop.as_bool(), None);
    }

    #[test]
    fn test_string_property() {
        let prop = Property::String("test".to_string());
        assert_eq!(prop.as_string(), Some("test"));
        assert_eq!(prop.expect_string(), "test");
        assert_eq!(prop.as_double(), None);
    }

    #[test]
    fn test_vector3d_property() {
        let vec = [1.0, 2.0, 3.0];
        let prop = Property::Vector3D(vec);
        assert_eq!(prop.as_vector3d(), Some(vec));
        assert_eq!(prop.expect_vector3d(), vec);
        assert_eq!(prop.as_double(), None);
    }

    #[test]
    fn test_matrix3x3_property() {
        let matrix = Matrix3::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
        let prop = Property::Matrix3x3(matrix);
        let expected = [1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0];

        assert_eq!(
            prop.as_matrix3x3(),
            Some([1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0])
        );

        for (v, e) in expected.iter().zip(prop.expect_matrix3x3()) {
            assert_approx_eq!(v, e);
        }
        assert_eq!(prop.as_double(), None);
    }

    #[test]
    #[should_panic(expected = "expected Bool")]
    fn test_expect_bool_panic() {
        let prop = Property::Double(1.0);
        let _ = prop.expect_bool();
    }

    #[test]
    #[should_panic(expected = "expected Double")]
    fn test_expect_double_panic() {
        let prop = Property::String("test".to_string());
        let _ = prop.expect_double();
    }

    #[test]
    #[should_panic(expected = "expected String")]
    fn test_expect_string_panic() {
        let prop = Property::Double(1.0);
        let _ = prop.expect_string();
    }

    #[test]
    #[should_panic(expected = "expected Vector3D")]
    fn test_expect_vector3d_panic() {
        let prop = Property::Double(1.0);
        let _ = prop.expect_vector3d();
    }

    #[test]
    #[should_panic(expected = "expected Matrix3x3")]
    fn test_expect_matrix3x3_panic() {
        let prop = Property::Double(1.0);
        let _ = prop.expect_matrix3x3();
    }

    #[test]
    fn test_property_equality() {
        // Test basic equality
        assert_eq!(Property::Bool(true), Property::Bool(true));
        assert_eq!(
            Property::String("test".to_string()),
            Property::String("test".to_string())
        );
        assert_eq!(Property::Double(1.0), Property::Double(1.0));
        assert_eq!(
            Property::Vector3D([1.0, 2.0, 3.0]),
            Property::Vector3D([1.0, 2.0, 3.0])
        );

        // Test floating-point approximate equality
        assert_eq!(Property::Double(1.0), Property::Double(1.0 + EPSILON / 2.0));
        assert_ne!(Property::Double(1.0), Property::Double(1.0 + EPSILON * 2.0));

        // Test different types are not equal
        assert_ne!(Property::Bool(true), Property::String("true".to_string()));
        assert_ne!(Property::Double(1.0), Property::Bool(true));

        // Test NaN and infinity handling
        assert_ne!(Property::Double(f64::NAN), Property::Double(f64::NAN));
        assert_ne!(
            Property::Double(f64::INFINITY),
            Property::Double(f64::INFINITY)
        );
        assert_ne!(
            Property::Double(f64::NEG_INFINITY),
            Property::Double(f64::NEG_INFINITY)
        );
    }

    #[test]
    fn test_vector_xd_property() {
        let vec = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let prop = Property::VectorXD(vec.clone());

        match &prop {
            Property::VectorXD(v) => assert_eq!(v, &vec),
            _ => panic!("Expected VectorXD property"),
        }

        assert_ne!(
            Property::VectorXD(vec.clone()),
            Property::VectorXD(vec![1.0, 2.0, 3.0])
        );
    }

    #[test]
    fn test_properties_container() {
        let mut properties = Properties::new();

        // Test insertion
        properties.insert("bool_prop".to_string(), Property::Bool(true));
        properties.insert("double_prop".to_string(), Property::Double(f64::consts::PI));
        properties.insert(
            "string_prop".to_string(),
            Property::String("hello".to_string()),
        );

        // Test retrieval
        assert_eq!(properties.get("bool_prop").unwrap().as_bool(), Some(true));
        assert_approx_eq!(
            properties.get("double_prop").unwrap().expect_double(),
            f64::consts::PI
        );
        assert_eq!(
            properties.get("string_prop").unwrap().expect_string(),
            "hello"
        );

        // Test non-existent key
        assert!(properties.get("nonexistent").is_none());

        // Test update
        properties.insert("bool_prop".to_string(), Property::Bool(false));
        assert_eq!(properties.get("bool_prop").unwrap().as_bool(), Some(false));

        // Test removal
        properties.remove("string_prop");
        assert!(properties.get("string_prop").is_none());

        // Test iteration
        let keys: Vec<_> = properties.keys().cloned().collect();
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"bool_prop".to_string()));
        assert!(keys.contains(&"double_prop".to_string()));
    }

    #[test]
    fn test_parse_bool() {
        assert_eq!(BoolParser::parse("true").unwrap(), Property::Bool(true));
        assert_eq!(BoolParser::parse("True").unwrap(), Property::Bool(true));
        assert_eq!(BoolParser::parse("t").unwrap(), Property::Bool(true));
        assert_eq!(BoolParser::parse("T").unwrap(), Property::Bool(true));

        assert_eq!(BoolParser::parse("false").unwrap(), Property::Bool(false));
        assert_eq!(BoolParser::parse("False").unwrap(), Property::Bool(false));
        assert_eq!(BoolParser::parse("f").unwrap(), Property::Bool(false));
        assert_eq!(BoolParser::parse("F").unwrap(), Property::Bool(false));

        assert!(BoolParser::parse("invalid").is_err());
    }

    #[test]
    fn test_parse_double() {
        let pi = f64::consts::PI;
        assert_approx_eq!(
            DoubleParser::parse(&pi.to_string())
                .unwrap()
                .expect_double(),
            pi
        );
        assert_approx_eq!(DoubleParser::parse("-1.5").unwrap().expect_double(), -1.5);
        assert_approx_eq!(DoubleParser::parse("0").unwrap().expect_double(), 0.0);
        assert_approx_eq!(
            DoubleParser::parse("1e6").unwrap().expect_double(),
            1_000_000.0
        );

        assert!(DoubleParser::parse("not_a_number").is_err());
    }

    #[test]
    fn test_parse_vector3d() {
        let result = Vector3DParser::parse("1.0 2.0 3.0").unwrap();
        assert_eq!(result.as_vector3d().unwrap(), [1.0, 2.0, 3.0]);

        // Not enough components
        assert!(Vector3DParser::parse("1.0 2.0").is_err());

        // Too many components
        assert!(Vector3DParser::parse("1.0 2.0 3.0 4.0").is_err());

        // Invalid components
        assert!(Vector3DParser::parse("1.0 invalid 3.0").is_err());
    }

    #[test]
    fn test_parse_matrix3x3() {
        let result = Matrix3x3Parser::parse("1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0").unwrap();
        let matrix = result.as_matrix3x3().unwrap();

        assert_approx_eq!(matrix[0], 1.0);
        assert_approx_eq!(matrix[1], 2.0);
        assert_approx_eq!(matrix[2], 3.0);
        assert_approx_eq!(matrix[3], 4.0);
        assert_approx_eq!(matrix[4], 5.0);
        assert_approx_eq!(matrix[5], 6.0);
        assert_approx_eq!(matrix[6], 7.0);
        assert_approx_eq!(matrix[7], 8.0);
        assert_approx_eq!(matrix[8], 9.0);

        // Not enough components
        assert!(Matrix3x3Parser::parse("1.0 2.0 3.0 4.0").is_err());

        // Too many components
        assert!(Matrix3x3Parser::parse("1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0").is_err());

        // Invalid components
        assert!(Matrix3x3Parser::parse("1.0 2.0 3.0 4.0 invalid 6.0 7.0 8.0 9.0").is_err());
    }

    #[test]
    fn test_parse_vectorxd() {
        let result = VectorXDParser::parse("1.0 2.0 3.0 4.0 5.0").unwrap();

        match result {
            Property::VectorXD(vec) => {
                assert_eq!(vec.len(), 5);
                assert_approx_eq!(vec[0], 1.0);
                assert_approx_eq!(vec[1], 2.0);
                assert_approx_eq!(vec[2], 3.0);
                assert_approx_eq!(vec[3], 4.0);
                assert_approx_eq!(vec[4], 5.0);
            }
            _ => panic!("Expected VectorXD property"),
        }

        // Empty input should give empty vector
        let empty_result = VectorXDParser::parse("").unwrap();
        match empty_result {
            Property::VectorXD(vec) => assert_eq!(vec.len(), 0),
            _ => panic!("Expected VectorXD property"),
        }

        // Invalid components
        assert!(VectorXDParser::parse("1.0 2.0 invalid 4.0").is_err());
    }

    #[test]
    fn test_parse_value() {
        // Test general parse_value method
        assert_eq!(
            Property::parse_value("true", &PropertyKind::Bool).unwrap(),
            Property::Bool(true)
        );

        let pi = f64::consts::PI;
        assert_approx_eq!(
            Property::parse_value(&pi.to_string(), &PropertyKind::Double)
                .unwrap()
                .expect_double(),
            f64::consts::PI
        );

        assert_eq!(
            Property::parse_value("hello", &PropertyKind::String)
                .unwrap()
                .expect_string(),
            "hello"
        );

        let vector = Property::parse_value("1.0 2.0 3.0", &PropertyKind::Vector3D).unwrap();
        assert_eq!(vector.as_vector3d().unwrap(), [1.0, 2.0, 3.0]);
    }

    #[test]
    fn test_matrix_direct_comparison() {
        let matrix1 = Matrix3::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
        let matrix2 = Matrix3::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);

        let prop1 = Property::Matrix3x3(matrix1);
        let prop2 = Property::Matrix3x3(matrix2);

        // Test that the properties are equal
        assert_eq!(prop1, prop2);

        // Create a slightly different matrix
        let matrix3 = Matrix3::new(1.0, 2.0, 3.0, 4.0, 5.1, 6.0, 7.0, 8.0, 9.0);
        let prop3 = Property::Matrix3x3(matrix3);

        // Should not be equal
        assert_ne!(prop1, prop3);
    }

    #[test]
    fn test_properties_iter() {
        let mut properties = Properties::new();
        properties.insert("bool_prop".to_string(), Property::Bool(true));
        properties.insert("double_prop".to_string(), Property::Double(f64::consts::PI));

        // Test direct iteration over properties
        let mut prop_count = 0;
        for (key, prop) in &properties {
            prop_count += 1;
            match key.as_str() {
                "bool_prop" => assert_eq!(prop.as_bool(), Some(true)),
                "double_prop" => assert_approx_eq!(prop.expect_double(), f64::consts::PI),
                _ => panic!("Unexpected property key: {key}"),
            }
        }
        assert_eq!(prop_count, 2);

        // Test iteration through IntoIterator implementation
        let mut prop_count = 0;
        for (key, _) in &properties {
            prop_count += 1;
            assert!(key == "bool_prop" || key == "double_prop");
        }
        assert_eq!(prop_count, 2);
    }
}