gldf-rs 0.3.4

GLDF (General Lighting Data Format) parser and writer for Rust, specifically for the Rust/WASM target as such designed for JSON format
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! CRUD operations for GldfProduct.
//!
//! This module provides methods for creating, reading, updating, and deleting
//! elements within a GLDF product structure.

use crate::gldf::general_definitions::files::File;
use crate::gldf::general_definitions::geometries::{Geometries, ModelGeometry, SimpleGeometry};
use crate::gldf::general_definitions::lightsources::{
    ChangeableLightSource, Emitter, Emitters, FixedLightSource, LightSources,
};
use crate::gldf::general_definitions::photometries::{Photometries, Photometry};
use crate::gldf::product_definitions::{ProductMetaData, Variant, Variants};
use crate::gldf::GldfProduct;
use anyhow::{anyhow, Result};
use std::collections::HashSet;

impl GldfProduct {
    // ==================== ID Generation ====================

    /// Generates a unique ID with the given prefix.
    ///
    /// Scans all existing IDs in the product and generates a new one
    /// that doesn't conflict.
    pub fn generate_unique_id(&self, prefix: &str) -> String {
        let existing_ids = self.get_all_ids();
        let mut counter = 1;
        loop {
            let candidate = format!("{}_{}", prefix, counter);
            if !existing_ids.contains(&candidate) {
                return candidate;
            }
            counter += 1;
        }
    }

    /// Gets all IDs used in the product.
    pub fn get_all_ids(&self) -> HashSet<String> {
        let mut ids = HashSet::new();

        // File IDs
        for file in &self.general_definitions.files.file {
            ids.insert(file.id.clone());
        }

        // Variant IDs
        if let Some(ref variants) = self.product_definitions.variants {
            for variant in &variants.variant {
                ids.insert(variant.id.clone());
            }
        }

        // Photometry IDs
        if let Some(ref photometries) = self.general_definitions.photometries {
            for photometry in &photometries.photometry {
                ids.insert(photometry.id.clone());
            }
        }

        // Geometry IDs
        if let Some(ref geometries) = self.general_definitions.geometries {
            for geom in &geometries.simple_geometry {
                ids.insert(geom.id.clone());
            }
            for geom in &geometries.model_geometry {
                ids.insert(geom.id.clone());
            }
        }

        // Light source IDs
        if let Some(ref light_sources) = self.general_definitions.light_sources {
            for source in &light_sources.fixed_light_source {
                ids.insert(source.id.clone());
            }
            for source in &light_sources.changeable_light_source {
                ids.insert(source.id.clone());
            }
        }

        // Emitter IDs
        if let Some(ref emitters) = self.general_definitions.emitters {
            for emitter in &emitters.emitter {
                ids.insert(emitter.id.clone());
            }
        }

        ids
    }

    /// Gets all file IDs that are referenced by other elements.
    pub fn get_referenced_file_ids(&self) -> HashSet<String> {
        let mut ids = HashSet::new();

        // Photometry file references
        if let Some(ref photometries) = self.general_definitions.photometries {
            for photometry in &photometries.photometry {
                if let Some(ref file_ref) = photometry.photometry_file_reference {
                    ids.insert(file_ref.file_id.clone());
                }
            }
        }

        // Geometry file references
        if let Some(ref geometries) = self.general_definitions.geometries {
            for geom in &geometries.model_geometry {
                for file_ref in &geom.geometry_file_reference {
                    ids.insert(file_ref.file_id.clone());
                }
            }
        }

        ids
    }

    // ==================== File Operations ====================

    /// Adds a new file definition.
    ///
    /// # Errors
    /// Returns an error if a file with the same ID already exists.
    pub fn add_file(&mut self, file: File) -> Result<()> {
        // Check for duplicate ID
        if self
            .general_definitions
            .files
            .file
            .iter()
            .any(|f| f.id == file.id)
        {
            return Err(anyhow!("File with ID '{}' already exists", file.id));
        }
        self.general_definitions.files.file.push(file);
        Ok(())
    }

    /// Updates an existing file definition.
    ///
    /// # Errors
    /// Returns an error if no file with the given ID exists.
    pub fn update_file(&mut self, id: &str, file: File) -> Result<()> {
        let pos = self
            .general_definitions
            .files
            .file
            .iter()
            .position(|f| f.id == id)
            .ok_or_else(|| anyhow!("File with ID '{}' not found", id))?;

        self.general_definitions.files.file[pos] = file;
        Ok(())
    }

    /// Removes a file definition by ID.
    ///
    /// # Errors
    /// Returns an error if no file with the given ID exists.
    pub fn remove_file(&mut self, id: &str) -> Result<File> {
        let pos = self
            .general_definitions
            .files
            .file
            .iter()
            .position(|f| f.id == id)
            .ok_or_else(|| anyhow!("File with ID '{}' not found", id))?;

        Ok(self.general_definitions.files.file.remove(pos))
    }

    /// Gets a file definition by ID.
    pub fn get_file(&self, id: &str) -> Option<&File> {
        self.general_definitions
            .files
            .file
            .iter()
            .find(|f| f.id == id)
    }

    /// Gets a mutable reference to a file definition by ID.
    pub fn get_file_mut(&mut self, id: &str) -> Option<&mut File> {
        self.general_definitions
            .files
            .file
            .iter_mut()
            .find(|f| f.id == id)
    }

    // ==================== Variant Operations ====================

    /// Adds a new variant.
    ///
    /// # Errors
    /// Returns an error if a variant with the same ID already exists.
    pub fn add_variant(&mut self, variant: Variant) -> Result<()> {
        // Ensure variants container exists
        if self.product_definitions.variants.is_none() {
            self.product_definitions.variants = Some(Variants::default());
        }

        let variants = self.product_definitions.variants.as_mut().unwrap();

        // Check for duplicate ID
        if variants.variant.iter().any(|v| v.id == variant.id) {
            return Err(anyhow!("Variant with ID '{}' already exists", variant.id));
        }

        variants.variant.push(variant);
        Ok(())
    }

    /// Updates an existing variant.
    ///
    /// # Errors
    /// Returns an error if no variant with the given ID exists.
    pub fn update_variant(&mut self, id: &str, variant: Variant) -> Result<()> {
        let variants = self
            .product_definitions
            .variants
            .as_mut()
            .ok_or_else(|| anyhow!("No variants defined"))?;

        let pos = variants
            .variant
            .iter()
            .position(|v| v.id == id)
            .ok_or_else(|| anyhow!("Variant with ID '{}' not found", id))?;

        variants.variant[pos] = variant;
        Ok(())
    }

    /// Removes a variant by ID.
    ///
    /// # Errors
    /// Returns an error if no variant with the given ID exists.
    pub fn remove_variant(&mut self, id: &str) -> Result<Variant> {
        let variants = self
            .product_definitions
            .variants
            .as_mut()
            .ok_or_else(|| anyhow!("No variants defined"))?;

        let pos = variants
            .variant
            .iter()
            .position(|v| v.id == id)
            .ok_or_else(|| anyhow!("Variant with ID '{}' not found", id))?;

        Ok(variants.variant.remove(pos))
    }

    /// Gets a variant by ID.
    pub fn get_variant(&self, id: &str) -> Option<&Variant> {
        self.product_definitions
            .variants
            .as_ref()
            .and_then(|v| v.variant.iter().find(|var| var.id == id))
    }

    /// Gets a mutable reference to a variant by ID.
    pub fn get_variant_mut(&mut self, id: &str) -> Option<&mut Variant> {
        self.product_definitions
            .variants
            .as_mut()
            .and_then(|v| v.variant.iter_mut().find(|var| var.id == id))
    }

    // ==================== Photometry Operations ====================

    /// Adds a new photometry definition.
    ///
    /// # Errors
    /// Returns an error if a photometry with the same ID already exists.
    pub fn add_photometry(&mut self, photometry: Photometry) -> Result<()> {
        // Ensure photometries container exists
        if self.general_definitions.photometries.is_none() {
            self.general_definitions.photometries = Some(Photometries::default());
        }

        let photometries = self.general_definitions.photometries.as_mut().unwrap();

        // Check for duplicate ID
        if photometries
            .photometry
            .iter()
            .any(|p| p.id == photometry.id)
        {
            return Err(anyhow!(
                "Photometry with ID '{}' already exists",
                photometry.id
            ));
        }

        photometries.photometry.push(photometry);
        Ok(())
    }

    /// Updates an existing photometry definition.
    ///
    /// # Errors
    /// Returns an error if no photometry with the given ID exists.
    pub fn update_photometry(&mut self, id: &str, photometry: Photometry) -> Result<()> {
        let photometries = self
            .general_definitions
            .photometries
            .as_mut()
            .ok_or_else(|| anyhow!("No photometries defined"))?;

        let pos = photometries
            .photometry
            .iter()
            .position(|p| p.id == id)
            .ok_or_else(|| anyhow!("Photometry with ID '{}' not found", id))?;

        photometries.photometry[pos] = photometry;
        Ok(())
    }

    /// Removes a photometry definition by ID.
    ///
    /// # Errors
    /// Returns an error if no photometry with the given ID exists.
    pub fn remove_photometry(&mut self, id: &str) -> Result<Photometry> {
        let photometries = self
            .general_definitions
            .photometries
            .as_mut()
            .ok_or_else(|| anyhow!("No photometries defined"))?;

        let pos = photometries
            .photometry
            .iter()
            .position(|p| p.id == id)
            .ok_or_else(|| anyhow!("Photometry with ID '{}' not found", id))?;

        Ok(photometries.photometry.remove(pos))
    }

    /// Gets a photometry by ID.
    pub fn get_photometry(&self, id: &str) -> Option<&Photometry> {
        self.general_definitions
            .photometries
            .as_ref()
            .and_then(|p| p.photometry.iter().find(|phot| phot.id == id))
    }

    // ==================== Geometry Operations ====================

    /// Adds a new simple geometry.
    ///
    /// # Errors
    /// Returns an error if a geometry with the same ID already exists.
    pub fn add_simple_geometry(&mut self, geometry: SimpleGeometry) -> Result<()> {
        // Ensure geometries container exists
        if self.general_definitions.geometries.is_none() {
            self.general_definitions.geometries = Some(Geometries::default());
        }

        let geometries = self.general_definitions.geometries.as_mut().unwrap();

        // Check for duplicate ID across both simple and model geometries
        if geometries
            .simple_geometry
            .iter()
            .any(|g| g.id == geometry.id)
            || geometries
                .model_geometry
                .iter()
                .any(|g| g.id == geometry.id)
        {
            return Err(anyhow!("Geometry with ID '{}' already exists", geometry.id));
        }

        geometries.simple_geometry.push(geometry);
        Ok(())
    }

    /// Adds a new model geometry.
    ///
    /// # Errors
    /// Returns an error if a geometry with the same ID already exists.
    pub fn add_model_geometry(&mut self, geometry: ModelGeometry) -> Result<()> {
        // Ensure geometries container exists
        if self.general_definitions.geometries.is_none() {
            self.general_definitions.geometries = Some(Geometries::default());
        }

        let geometries = self.general_definitions.geometries.as_mut().unwrap();

        // Check for duplicate ID across both simple and model geometries
        if geometries
            .simple_geometry
            .iter()
            .any(|g| g.id == geometry.id)
            || geometries
                .model_geometry
                .iter()
                .any(|g| g.id == geometry.id)
        {
            return Err(anyhow!("Geometry with ID '{}' already exists", geometry.id));
        }

        geometries.model_geometry.push(geometry);
        Ok(())
    }

    /// Removes a simple geometry by ID.
    ///
    /// # Errors
    /// Returns an error if no simple geometry with the given ID exists.
    pub fn remove_simple_geometry(&mut self, id: &str) -> Result<SimpleGeometry> {
        let geometries = self
            .general_definitions
            .geometries
            .as_mut()
            .ok_or_else(|| anyhow!("No geometries defined"))?;

        let pos = geometries
            .simple_geometry
            .iter()
            .position(|g| g.id == id)
            .ok_or_else(|| anyhow!("Simple geometry with ID '{}' not found", id))?;

        Ok(geometries.simple_geometry.remove(pos))
    }

    /// Removes a model geometry by ID.
    ///
    /// # Errors
    /// Returns an error if no model geometry with the given ID exists.
    pub fn remove_model_geometry(&mut self, id: &str) -> Result<ModelGeometry> {
        let geometries = self
            .general_definitions
            .geometries
            .as_mut()
            .ok_or_else(|| anyhow!("No geometries defined"))?;

        let pos = geometries
            .model_geometry
            .iter()
            .position(|g| g.id == id)
            .ok_or_else(|| anyhow!("Model geometry with ID '{}' not found", id))?;

        Ok(geometries.model_geometry.remove(pos))
    }

    /// Gets a simple geometry by ID.
    pub fn get_simple_geometry(&self, id: &str) -> Option<&SimpleGeometry> {
        self.general_definitions
            .geometries
            .as_ref()
            .and_then(|g| g.simple_geometry.iter().find(|geom| geom.id == id))
    }

    /// Gets a model geometry by ID.
    pub fn get_model_geometry(&self, id: &str) -> Option<&ModelGeometry> {
        self.general_definitions
            .geometries
            .as_ref()
            .and_then(|g| g.model_geometry.iter().find(|geom| geom.id == id))
    }

    // ==================== Light Source Operations ====================

    /// Adds a new fixed light source.
    ///
    /// # Errors
    /// Returns an error if a light source with the same ID already exists.
    pub fn add_fixed_light_source(&mut self, source: FixedLightSource) -> Result<()> {
        // Ensure light sources container exists
        if self.general_definitions.light_sources.is_none() {
            self.general_definitions.light_sources = Some(LightSources::default());
        }

        let light_sources = self.general_definitions.light_sources.as_mut().unwrap();

        // Check for duplicate ID
        if light_sources
            .fixed_light_source
            .iter()
            .any(|s| s.id == source.id)
            || light_sources
                .changeable_light_source
                .iter()
                .any(|s| s.id == source.id)
        {
            return Err(anyhow!(
                "Light source with ID '{}' already exists",
                source.id
            ));
        }

        light_sources.fixed_light_source.push(source);
        Ok(())
    }

    /// Adds a new changeable light source.
    ///
    /// # Errors
    /// Returns an error if a light source with the same ID already exists.
    pub fn add_changeable_light_source(&mut self, source: ChangeableLightSource) -> Result<()> {
        // Ensure light sources container exists
        if self.general_definitions.light_sources.is_none() {
            self.general_definitions.light_sources = Some(LightSources::default());
        }

        let light_sources = self.general_definitions.light_sources.as_mut().unwrap();

        // Check for duplicate ID
        if light_sources
            .fixed_light_source
            .iter()
            .any(|s| s.id == source.id)
            || light_sources
                .changeable_light_source
                .iter()
                .any(|s| s.id == source.id)
        {
            return Err(anyhow!(
                "Light source with ID '{}' already exists",
                source.id
            ));
        }

        light_sources.changeable_light_source.push(source);
        Ok(())
    }

    /// Removes a fixed light source by ID.
    ///
    /// # Errors
    /// Returns an error if no fixed light source with the given ID exists.
    pub fn remove_fixed_light_source(&mut self, id: &str) -> Result<FixedLightSource> {
        let light_sources = self
            .general_definitions
            .light_sources
            .as_mut()
            .ok_or_else(|| anyhow!("No light sources defined"))?;

        let pos = light_sources
            .fixed_light_source
            .iter()
            .position(|s| s.id == id)
            .ok_or_else(|| anyhow!("Fixed light source with ID '{}' not found", id))?;

        Ok(light_sources.fixed_light_source.remove(pos))
    }

    /// Removes a changeable light source by ID.
    ///
    /// # Errors
    /// Returns an error if no changeable light source with the given ID exists.
    pub fn remove_changeable_light_source(&mut self, id: &str) -> Result<ChangeableLightSource> {
        let light_sources = self
            .general_definitions
            .light_sources
            .as_mut()
            .ok_or_else(|| anyhow!("No light sources defined"))?;

        let pos = light_sources
            .changeable_light_source
            .iter()
            .position(|s| s.id == id)
            .ok_or_else(|| anyhow!("Changeable light source with ID '{}' not found", id))?;

        Ok(light_sources.changeable_light_source.remove(pos))
    }

    /// Gets a fixed light source by ID.
    pub fn get_fixed_light_source(&self, id: &str) -> Option<&FixedLightSource> {
        self.general_definitions
            .light_sources
            .as_ref()
            .and_then(|ls| ls.fixed_light_source.iter().find(|s| s.id == id))
    }

    /// Gets a changeable light source by ID.
    pub fn get_changeable_light_source(&self, id: &str) -> Option<&ChangeableLightSource> {
        self.general_definitions
            .light_sources
            .as_ref()
            .and_then(|ls| ls.changeable_light_source.iter().find(|s| s.id == id))
    }

    // ==================== Emitter Operations ====================

    /// Adds a new emitter.
    ///
    /// # Errors
    /// Returns an error if an emitter with the same ID already exists.
    pub fn add_emitter(&mut self, emitter: Emitter) -> Result<()> {
        // Ensure emitters container exists
        if self.general_definitions.emitters.is_none() {
            self.general_definitions.emitters = Some(Emitters::default());
        }

        let emitters = self.general_definitions.emitters.as_mut().unwrap();

        // Check for duplicate ID
        if emitters.emitter.iter().any(|e| e.id == emitter.id) {
            return Err(anyhow!("Emitter with ID '{}' already exists", emitter.id));
        }

        emitters.emitter.push(emitter);
        Ok(())
    }

    /// Removes an emitter by ID.
    ///
    /// # Errors
    /// Returns an error if no emitter with the given ID exists.
    pub fn remove_emitter(&mut self, id: &str) -> Result<Emitter> {
        let emitters = self
            .general_definitions
            .emitters
            .as_mut()
            .ok_or_else(|| anyhow!("No emitters defined"))?;

        let pos = emitters
            .emitter
            .iter()
            .position(|e| e.id == id)
            .ok_or_else(|| anyhow!("Emitter with ID '{}' not found", id))?;

        Ok(emitters.emitter.remove(pos))
    }

    /// Gets an emitter by ID.
    pub fn get_emitter(&self, id: &str) -> Option<&Emitter> {
        self.general_definitions
            .emitters
            .as_ref()
            .and_then(|e| e.emitter.iter().find(|em| em.id == id))
    }

    /// Gets a mutable reference to an emitter by ID.
    pub fn get_emitter_mut(&mut self, id: &str) -> Option<&mut Emitter> {
        self.general_definitions
            .emitters
            .as_mut()
            .and_then(|e| e.emitter.iter_mut().find(|em| em.id == id))
    }

    // ==================== Product Metadata Operations ====================

    /// Sets the product metadata.
    pub fn set_product_metadata(&mut self, meta: ProductMetaData) {
        self.product_definitions.product_meta_data = Some(meta);
    }

    /// Gets the product metadata.
    pub fn get_product_metadata(&self) -> Option<&ProductMetaData> {
        self.product_definitions.product_meta_data.as_ref()
    }

    /// Gets a mutable reference to the product metadata.
    pub fn get_product_metadata_mut(&mut self) -> Option<&mut ProductMetaData> {
        self.product_definitions.product_meta_data.as_mut()
    }

    /// Ensures product metadata exists, creating default if needed.
    pub fn ensure_product_metadata(&mut self) -> &mut ProductMetaData {
        if self.product_definitions.product_meta_data.is_none() {
            self.product_definitions.product_meta_data = Some(ProductMetaData::default());
        }
        self.product_definitions.product_meta_data.as_mut().unwrap()
    }
}

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

    #[test]
    fn test_generate_unique_id() {
        let product = GldfProduct::default();
        let id1 = product.generate_unique_id("file");
        assert_eq!(id1, "file_1");

        let mut product2 = GldfProduct::default();
        product2
            .add_file(File {
                id: "file_1".to_string(),
                content_type: "ldc/eulumdat".to_string(),
                type_attr: "localFileName".to_string(),
                file_name: "test.ldt".to_string(),
                language: String::new(),
            })
            .unwrap();

        let id2 = product2.generate_unique_id("file");
        assert_eq!(id2, "file_2");
    }

    #[test]
    fn test_file_operations() {
        let mut product = GldfProduct::default();

        // Add file
        let file = File {
            id: "test_file".to_string(),
            content_type: "ldc/eulumdat".to_string(),
            type_attr: "localFileName".to_string(),
            file_name: "test.ldt".to_string(),
            language: String::new(),
        };
        assert!(product.add_file(file.clone()).is_ok());

        // Duplicate add should fail
        assert!(product.add_file(file.clone()).is_err());

        // Get file
        assert!(product.get_file("test_file").is_some());
        assert!(product.get_file("nonexistent").is_none());

        // Update file
        let mut updated_file = file.clone();
        updated_file.file_name = "updated.ldt".to_string();
        assert!(product.update_file("test_file", updated_file).is_ok());
        assert_eq!(
            product.get_file("test_file").unwrap().file_name,
            "updated.ldt"
        );

        // Remove file
        assert!(product.remove_file("test_file").is_ok());
        assert!(product.get_file("test_file").is_none());
    }

    #[test]
    fn test_variant_operations() {
        let mut product = GldfProduct::default();

        // Add variant
        let variant = Variant {
            id: "variant_1".to_string(),
            ..Default::default()
        };
        assert!(product.add_variant(variant.clone()).is_ok());

        // Duplicate add should fail
        assert!(product.add_variant(variant.clone()).is_err());

        // Get variant
        assert!(product.get_variant("variant_1").is_some());

        // Remove variant
        assert!(product.remove_variant("variant_1").is_ok());
        assert!(product.get_variant("variant_1").is_none());
    }

    #[test]
    fn test_light_source_operations() {
        let mut product = GldfProduct::default();

        // Add fixed light source
        let source = FixedLightSource {
            id: "source_1".to_string(),
            ..Default::default()
        };
        assert!(product.add_fixed_light_source(source.clone()).is_ok());

        // Duplicate should fail
        assert!(product.add_fixed_light_source(source).is_err());

        // Get source
        assert!(product.get_fixed_light_source("source_1").is_some());

        // Remove source
        assert!(product.remove_fixed_light_source("source_1").is_ok());
        assert!(product.get_fixed_light_source("source_1").is_none());
    }
}