Skip to main content

builder_pattern_example/
builder_pattern_example.rs

1//! Example demonstrating the builder pattern API for creating VSF files
2//!
3//! This shows the ergonomic dot notation for incrementally setting fields, which is especially useful when fields are conditional or optional.
4
5use vsf::builders::RawImageBuilder;
6use vsf::types::BitPackedTensor;
7
8fn main() {
9    // Create a simple 8x8 test image
10    let samples: Vec<u64> = (0..64).map(|i| i * 4).collect(); // 0, 4, 8, 12, ..., 252
11    let image = BitPackedTensor::pack(8, vec![8, 8], &samples);
12
13    // Create builder with image
14    let mut raw = RawImageBuilder::new(image);
15
16    // Set RAW metadata fields using dot notation
17    raw.raw.cfa_pattern = Some(vec![b'R', b'G', b'G', b'B']); // RGGB Bayer pattern
18    raw.raw.black_level = Some(64.0);
19    raw.raw.white_level = Some(255.0);
20
21    // Set camera settings using dot notation
22    raw.camera.iso_speed = Some(800.0);
23    raw.camera.shutter_time_s = Some(1.0 / 60.0); // 1/60 second
24    raw.camera.aperture_f_number = Some(2.8);
25    raw.camera.focal_length_m = Some(0.050); // 50mm
26    raw.camera.focus_distance_m = Some(3.5);
27    raw.camera.flash_fired = Some(false);
28    raw.camera.metering_mode = Some("matrix".to_string());
29
30    // Set lens info using dot notation
31    raw.lens.make = Some("Sony".to_string());
32    raw.lens.model = Some("FE 50mm F1.2 GM".to_string());
33    raw.lens.min_focal_length_m = Some(0.050); // 50mm prime
34    raw.lens.max_focal_length_m = Some(0.050);
35    raw.lens.max_aperture_f = Some(1.2); // f/1.2 maximum aperture
36
37    // Build the VSF file
38    let bytes = raw.build().expect("Failed to build VSF file");
39
40    // Write to file
41    std::fs::write("builder_example.vsf", &bytes).expect("Failed to write file");
42
43    println!("Created builder_example.vsf ({} bytes)", bytes.len());
44    println!("\nThe builder pattern allows you to:");
45    println!("  - Use intuitive dot notation: raw.camera.iso_speed = Some(800.0)");
46    println!("  - Incrementally set fields based on conditions");
47    println!("  - Skip optional fields easily (they default to None)");
48}