ph-curves 0.2.0

no-std, no-alloc curve lookup tables and tickless scheduling for embedded Rust
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
//! Rust source-code generation from built curve data.

// Host-only: module-local std link (crate root stays `#![no_std]`).
extern crate std;

use std::prelude::v1::*;
use std::{format, vec};

use std::collections::BTreeMap;

use super::curve::{CurveData, CurveDef, DefinitionsFile};
use super::transfer::{TransferData, TransferDef};

/// Render all curves and transfers as a complete Rust source string.
pub fn generate(
    curves_file: &DefinitionsFile,
    value_type: &str,
    lut_size: usize,
) -> Result<String, String> {
    let mut curves: Vec<(&String, &CurveDef)> = curves_file.curves.iter().collect();
    curves.sort_by_key(|(name, _)| *name);
    let mut transfers: Vec<(&String, &TransferDef)> = curves_file.transfers.iter().collect();
    transfers.sort_by_key(|(name, _)| *name);

    let const_names = emitted_const_names(&curves, &transfers)?;

    let mut out = String::new();
    out.push_str("// Auto-generated by ph-curves-gen. Do not edit.\n\n");
    if !curves_file.curves.is_empty() {
        out.push_str("use ph_curves::{CurveLut, MonotonicCurveLut};\n\n");
    }
    if !curves_file.transfers.is_empty() {
        out.push_str(
            "use ph_curves::{BoundaryBehavior, MonotonicDirection, \
             PiecewiseLinearTransfer, TransferMetadata};\n\n",
        );
    }

    if !curves_file.curves.is_empty() {
        // Type aliases for convenience.
        out.push_str(&format!(
            "type Lut = CurveLut<{value_type}, {value_type}, {lut_size}>;\n",
        ));
        out.push_str(&format!(
            "type MonoLut = MonotonicCurveLut<{value_type}, {value_type}, {lut_size}>;\n\n",
        ));
    }

    for (name, def) in &curves {
        let data = super::curve::build(name, def, lut_size)?;
        let const_name = &const_names[*name];
        emit_curve(&mut out, name, const_name, def, &data, value_type, lut_size);
    }

    for (name, def) in &transfers {
        let data = super::transfer::build(name, def)?;
        let const_name = &const_names[*name];
        emit_transfer(&mut out, name, const_name, def, &data);
    }

    Ok(out)
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn emit_curve(
    out: &mut String,
    name: &str,
    const_name: &str,
    def: &CurveDef,
    data: &CurveData,
    value_type: &str,
    lut_size: usize,
) {
    // Forward LUT.
    out.push_str(&format!(
        "static {const_name}_FWD: [{vt}; {lut_size}] = {arr};\n",
        vt = value_type,
        arr = format_array(&data.fwd),
    ));

    // Inverse LUT (monotonic only).
    if let Some(inv) = &data.inv {
        out.push_str(&format!(
            "static {const_name}_INV: [{vt}; {lut_size}] = {arr};\n",
            vt = value_type,
            arr = format_array(inv),
        ));
    }

    // Public constant. Debug-format the TOML name so newlines cannot break
    // out of the generated `///` comment.
    if def.monotonic {
        out.push_str(&format!(
            "/// {name:?} \u{2014} monotonic curve.\n\
             pub const {const_name}: MonoLut = \
             MonoLut::new(&{const_name}_FWD, &{const_name}_INV);\n\n",
        ));
    } else {
        out.push_str(&format!(
            "/// {name:?} \u{2014} curve (non-monotonic).\n\
             pub const {const_name}: Lut = \
             Lut::new(&{const_name}_FWD, None);\n\n",
        ));
    }
}

fn emit_transfer(
    out: &mut String,
    name: &str,
    const_name: &str,
    def: &TransferDef,
    data: &TransferData,
) {
    let knot_count = data.inputs.len();
    let direction = match data.direction {
        crate::MonotonicDirection::Increasing => "MonotonicDirection::Increasing",
        crate::MonotonicDirection::Decreasing => "MonotonicDirection::Decreasing",
    };
    let domain_min = data.inputs[0];
    let domain_max = *data.inputs.last().unwrap();

    out.push_str(&format!(
        "#[rustfmt::skip]\n\
         static {const_name}_INPUTS: [u16; {knot_count}] = {inputs};\n",
        inputs = format_u16_array(&data.inputs),
    ));
    out.push_str(&format!(
        "#[rustfmt::skip]\n\
         static {const_name}_OUTPUTS: [i32; {knot_count}] = {outputs};\n",
        outputs = format_i32_array(&data.outputs),
    ));
    // Debug-format TOML-derived text so newlines or comment terminators in
    // names, provenance, or units cannot break out of `///` doc comments.
    out.push_str(&format!(
        "/// {name:?} sparse physical transfer function.\n\
         ///\n\
         /// Source: {provenance:?}.\n\
         /// Domain: {domain_min}..={domain_max} {input_unit:?}; output: {output_unit:?} x {output_scale}.\n\
         /// Knots: {knot_count} ({payload} bytes array payload).\n\
         /// Exhaustive numerical error: requested <= {requested}, achieved {achieved:.6} output quanta\n\
         /// (conservative metadata bound {achieved_bound}) at input {worst}.\n\
         /// This is table/quantization error against the configured ideal source, not total sensor accuracy.\n\
         #[rustfmt::skip]\n\
         pub const {const_name}: PiecewiseLinearTransfer<{knot_count}> =\n\
             PiecewiseLinearTransfer::new(&{const_name}_INPUTS, &{const_name}_OUTPUTS, {direction})\n\
                 .with_boundaries({below}, {above});\n",
        provenance = data.provenance,
        input_unit = def.input_unit,
        output_unit = def.output_unit,
        output_scale = def.output_scale,
        payload = knot_count * 6,
        requested = def.max_interpolation_error,
        achieved = data.achieved_max_error_exact,
        achieved_bound = data.achieved_max_error,
        worst = data.worst_case_input,
        below = def.below.rust_name(),
        above = def.above.rust_name(),
    ));
    let (range_min, range_max) = {
        let first = data.outputs[0];
        let last = *data.outputs.last().unwrap();
        if first <= last {
            (first, last)
        } else {
            (last, first)
        }
    };
    let flat_segment_count = data
        .outputs
        .windows(2)
        .filter(|pair| pair[0] == pair[1])
        .count();
    let strictly_monotonic = flat_segment_count == 0;
    let achieved_max_inverse_code_error =
        super::transfer::measure_inverse_code_error(&data.inputs, &data.outputs, data.direction);

    out.push_str(&format!(
        "/// Metadata for [`{const_name}`].\n\
         pub const {const_name}_METADATA: TransferMetadata = TransferMetadata {{\n\
         \x20   input_unit: {input_unit:?},\n\
         \x20   output_unit: {output_unit:?},\n\
         \x20   output_scale: {output_scale},\n\
         \x20   domain_min: {domain_min},\n\
         \x20   domain_max: {domain_max},\n\
         \x20   range_min: {range_min},\n\
         \x20   range_max: {range_max},\n\
         \x20   direction: {direction},\n\
         \x20   knot_count: {knot_count},\n\
         \x20   strictly_monotonic: {strictly_monotonic},\n\
         \x20   flat_segment_count: {flat_segment_count},\n\
         \x20   requested_max_error: {requested},\n\
         \x20   achieved_max_error: {achieved},\n\
         \x20   worst_case_input: {worst},\n\
         \x20   achieved_max_inverse_code_error: {inverse_error},\n\
         }};\n\n",
        input_unit = def.input_unit,
        output_unit = def.output_unit,
        output_scale = def.output_scale,
        requested = def.max_interpolation_error,
        achieved = data.achieved_max_error,
        worst = data.worst_case_input,
        inverse_error = achieved_max_inverse_code_error,
    ));
}

fn emitted_const_names(
    curves: &[(&String, &CurveDef)],
    transfers: &[(&String, &TransferDef)],
) -> Result<BTreeMap<String, String>, String> {
    let mut names = BTreeMap::new();
    let mut base_sources = BTreeMap::new();

    for name in curves
        .iter()
        .map(|(name, _)| *name)
        .chain(transfers.iter().map(|(name, _)| *name))
    {
        let const_name = to_const_name(name)?;
        if let Some(previous) = base_sources.insert(const_name.clone(), name.as_str()) {
            return Err(format!(
                "names `{previous}` and `{name}` both normalize to Rust identifier \
                 `{const_name}`; rename one entry"
            ));
        }
        names.insert(name.clone(), const_name);
    }

    let mut emitted_sources = BTreeMap::new();
    for (name, def) in curves {
        let const_name = &names[*name];
        let mut identifiers = vec![const_name.clone(), format!("{const_name}_FWD")];
        if def.monotonic {
            identifiers.push(format!("{const_name}_INV"));
        }
        reserve_identifiers(name, identifiers, &mut emitted_sources)?;
    }
    for (name, _) in transfers {
        let const_name = &names[*name];
        reserve_identifiers(
            name,
            [
                const_name.clone(),
                format!("{const_name}_INPUTS"),
                format!("{const_name}_OUTPUTS"),
                format!("{const_name}_METADATA"),
            ],
            &mut emitted_sources,
        )?;
    }

    Ok(names)
}

fn reserve_identifiers<'a>(
    name: &'a str,
    identifiers: impl IntoIterator<Item = String>,
    emitted_sources: &mut BTreeMap<String, &'a str>,
) -> Result<(), String> {
    for identifier in identifiers {
        if let Some(previous) = emitted_sources.insert(identifier.clone(), name) {
            return Err(format!(
                "names `{previous}` and `{name}` generate duplicate Rust identifier \
                 `{identifier}`; rename one entry"
            ));
        }
    }
    Ok(())
}

fn to_const_name(name: &str) -> Result<String, String> {
    let mut out = String::new();
    let mut prev_underscore = false;
    let mut has_alphanumeric = false;
    for ch in name.chars() {
        let next = if ch.is_ascii_alphanumeric() {
            has_alphanumeric = true;
            ch.to_ascii_uppercase()
        } else {
            '_'
        };
        if next == '_' {
            if !prev_underscore {
                out.push('_');
                prev_underscore = true;
            }
        } else {
            out.push(next);
            prev_underscore = false;
        }
    }
    if !has_alphanumeric {
        return Err(format!(
            "name `{name}` cannot be normalized to a Rust identifier; \
             include at least one ASCII letter or digit"
        ));
    }
    if out.as_bytes()[0].is_ascii_digit() {
        out.insert_str(0, "CURVE_");
    }
    Ok(out)
}

fn format_array(values: &[u32]) -> String {
    let mut out = String::from("[");
    for (idx, value) in values.iter().enumerate() {
        if idx > 0 {
            out.push_str(", ");
        }
        out.push_str(&value.to_string());
    }
    out.push(']');
    out
}

fn format_u16_array(values: &[u16]) -> String {
    format_values(values)
}

fn format_i32_array(values: &[i32]) -> String {
    format_values(values)
}

fn format_values<T: core::fmt::Display>(values: &[T]) -> String {
    let mut out = String::from("[");
    for (idx, value) in values.iter().enumerate() {
        if idx > 0 {
            out.push_str(", ");
        }
        out.push_str(&value.to_string());
    }
    out.push(']');
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::r#gen::transfer::{BoundaryDef, PhysicalPoint, TransferDef};
    use std::collections::BTreeMap;

    // ── to_const_name ─────────────────────────────────────────────

    #[test]
    fn const_name_simple() {
        assert_eq!(to_const_name("linear").unwrap(), "LINEAR");
    }

    #[test]
    fn const_name_with_underscores() {
        assert_eq!(to_const_name("ease_in_quad").unwrap(), "EASE_IN_QUAD");
    }

    #[test]
    fn const_name_collapses_multiple_non_alnum() {
        assert_eq!(to_const_name("a--b").unwrap(), "A_B");
    }

    #[test]
    fn const_name_rejects_empty() {
        let error = to_const_name("").unwrap_err();
        assert!(error.contains("cannot be normalized"));
        assert!(error.contains("ASCII letter or digit"));
    }

    #[test]
    fn const_name_rejects_special_chars_only() {
        let error = to_const_name("---").unwrap_err();
        assert!(error.contains("cannot be normalized"));
        assert!(error.contains("Rust identifier"));
    }

    #[test]
    fn const_name_prefixes_leading_digit() {
        assert_eq!(to_const_name("2bit").unwrap(), "CURVE_2BIT");
        assert_eq!(to_const_name("123 curve").unwrap(), "CURVE_123_CURVE");
    }

    // ── format_array ──────────────────────────────────────────────

    #[test]
    fn format_array_small() {
        assert_eq!(format_array(&[0, 1, 2, 3]), "[0, 1, 2, 3]");
    }

    #[test]
    fn format_array_single() {
        assert_eq!(format_array(&[42]), "[42]");
    }

    #[test]
    fn format_array_empty() {
        assert_eq!(format_array(&[]), "[]");
    }

    // ── generate (integration) ────────────────────────────────────

    #[test]
    fn generate_monotonic_curve() {
        let mut curves = BTreeMap::new();
        curves.insert(
            "linear".to_string(),
            CurveDef {
                builtin: Some("linear".into()),
                formula: None,
                points: None,
                monotonic: true,
            },
        );
        let cf = DefinitionsFile {
            curves,
            transfers: BTreeMap::new(),
        };
        let out = generate(&cf, "u8", 256).unwrap();
        assert!(out.contains("AUTO_LINEAR_FWD") || out.contains("LINEAR_FWD"));
        assert!(out.contains("LINEAR_INV"));
        assert!(out.contains("MonoLut"));
        assert!(out.contains(r#"/// "linear" — monotonic curve."#));
    }

    #[test]
    fn generate_non_monotonic_curve() {
        let mut curves = BTreeMap::new();
        curves.insert(
            "wave".to_string(),
            CurveDef {
                builtin: None,
                formula: None,
                points: Some(vec![[0, 0], [64, 200], [192, 50], [255, 255]]),
                monotonic: false,
            },
        );
        let cf = DefinitionsFile {
            curves,
            transfers: BTreeMap::new(),
        };
        let out = generate(&cf, "u8", 256).unwrap();
        assert!(out.contains("WAVE_FWD"));
        assert!(!out.contains("WAVE_INV"));
        assert!(out.contains("Lut::new"));
    }

    #[test]
    fn generate_u16_lut() {
        let mut curves = BTreeMap::new();
        curves.insert(
            "lin".to_string(),
            CurveDef {
                builtin: Some("linear".into()),
                formula: None,
                points: None,
                monotonic: true,
            },
        );
        let cf = DefinitionsFile {
            curves,
            transfers: BTreeMap::new(),
        };
        let out = generate(&cf, "u16", 10).unwrap();
        assert!(out.contains("u16"));
        assert!(out.contains("10"));
    }

    #[test]
    fn generate_rejects_normalization_collisions() {
        let mut curves = BTreeMap::new();
        for name in ["ease-in", "ease_in"] {
            curves.insert(
                name.to_string(),
                CurveDef {
                    builtin: Some("linear".into()),
                    formula: None,
                    points: None,
                    monotonic: true,
                },
            );
        }
        let error = generate(
            &DefinitionsFile {
                curves,
                transfers: BTreeMap::new(),
            },
            "u8",
            256,
        )
        .unwrap_err();
        assert!(error.contains("both normalize"));
        assert!(error.contains("`EASE_IN`"));
    }

    #[test]
    fn generate_rejects_curve_companion_collisions() {
        let mut curves = BTreeMap::new();
        for name in ["linear", "linear_fwd"] {
            curves.insert(
                name.to_string(),
                CurveDef {
                    builtin: Some("linear".into()),
                    formula: None,
                    points: None,
                    monotonic: true,
                },
            );
        }
        let error = generate(
            &DefinitionsFile {
                curves,
                transfers: BTreeMap::new(),
            },
            "u8",
            256,
        )
        .unwrap_err();
        assert!(error.contains("duplicate Rust identifier"));
        assert!(error.contains("`LINEAR_FWD`"));
    }

    #[test]
    fn generate_escapes_curve_names_in_doc_comments() {
        let mut curves = BTreeMap::new();
        curves.insert(
            "line\nbreak".to_string(),
            CurveDef {
                builtin: Some("linear".into()),
                formula: None,
                points: None,
                monotonic: true,
            },
        );
        let out = generate(
            &DefinitionsFile {
                curves,
                transfers: BTreeMap::new(),
            },
            "u8",
            256,
        )
        .unwrap();
        assert!(out.contains(r#"/// "line\nbreak" — monotonic curve."#));
        assert!(!out.contains("/// \"line\nbreak"));
    }

    #[test]
    fn generate_rejects_curve_transfer_name_collisions() {
        let mut curves = BTreeMap::new();
        curves.insert(
            "sensor".to_string(),
            CurveDef {
                builtin: Some("linear".into()),
                formula: None,
                points: None,
                monotonic: true,
            },
        );
        let mut transfers = BTreeMap::new();
        transfers.insert(
            "sensor".to_string(),
            TransferDef {
                input_unit: "adc_code".into(),
                output_unit: "volt".into(),
                output_scale: 1000,
                max_interpolation_error: 1,
                max_knots: 256,
                below: BoundaryDef::Error,
                above: BoundaryDef::Error,
                points: Some(vec![
                    PhysicalPoint {
                        input: 0,
                        output: 0.0,
                    },
                    PhysicalPoint {
                        input: 10,
                        output: 1.0,
                    },
                ]),
                formula: None,
                model: None,
                domain: None,
                output_range: None,
            },
        );
        let error = generate(&DefinitionsFile { curves, transfers }, "u8", 256).unwrap_err();
        assert!(error.contains("both normalize"));
        assert!(error.contains("`SENSOR`"));
    }

    #[test]
    fn generate_rejects_transfer_companion_collisions() {
        let mut transfers = BTreeMap::new();
        for name in ["ntc", "ntc_inputs"] {
            transfers.insert(
                name.to_string(),
                TransferDef {
                    input_unit: "adc_code".into(),
                    output_unit: "volt".into(),
                    output_scale: 1000,
                    max_interpolation_error: 1,
                    max_knots: 256,
                    below: BoundaryDef::Error,
                    above: BoundaryDef::Error,
                    points: Some(vec![
                        PhysicalPoint {
                            input: 0,
                            output: 0.0,
                        },
                        PhysicalPoint {
                            input: 10,
                            output: 1.0,
                        },
                    ]),
                    formula: None,
                    model: None,
                    domain: None,
                    output_range: None,
                },
            );
        }
        let error = generate(
            &DefinitionsFile {
                curves: BTreeMap::new(),
                transfers,
            },
            "u8",
            256,
        )
        .unwrap_err();
        assert!(error.contains("duplicate Rust identifier"));
        assert!(error.contains("`NTC_INPUTS`"));
    }

    #[test]
    fn generate_escapes_transfer_fields_in_doc_comments() {
        let mut transfers = BTreeMap::new();
        transfers.insert(
            "line\nbreak".to_string(),
            TransferDef {
                input_unit: "adc\ncode".into(),
                output_unit: "volt\nunit".into(),
                output_scale: 1000,
                max_interpolation_error: 1,
                max_knots: 256,
                below: BoundaryDef::Error,
                above: BoundaryDef::Error,
                formula: Some("x\n* 0.5".into()),
                points: None,
                model: None,
                domain: Some([0, 10]),
                output_range: None,
            },
        );
        let out = generate(
            &DefinitionsFile {
                curves: BTreeMap::new(),
                transfers,
            },
            "u8",
            256,
        )
        .unwrap();
        assert!(out.contains(r#"/// "line\nbreak" sparse physical transfer function."#));
        assert!(out.contains(r#"Source: "formula y = x\n* 0.5"."#));
        assert!(out.contains(r#""adc\ncode""#));
        assert!(out.contains(r#""volt\nunit""#));
        assert!(!out.contains("/// \"line\nbreak"));
    }

    #[test]
    fn reference_ntc_matches_golden_output() {
        let definition: DefinitionsFile =
            toml::from_str(include_str!("../../assets/transfers.toml")).unwrap();
        let output = generate(&definition, "u8", 256).unwrap();
        let expected = include_str!("../../tests/fixtures/ntc_generated.rs").replace("\r\n", "\n");
        assert_eq!(output.trim_end(), expected.trim_end());
        assert!(!output.contains("f32"));
        assert!(!output.contains("f64"));
    }

    #[test]
    fn custom_transfer_examples_generate_integer_firmware_code() {
        let definition: DefinitionsFile =
            toml::from_str(include_str!("../../assets/custom-transfers.toml")).unwrap();
        let output = generate(&definition, "u8", 256).unwrap();
        assert!(output.contains("pub const PRESSURE_100KPA"));
        assert!(output.contains("pub const TANK_LEVEL"));
        assert!(output.contains("PiecewiseLinearTransfer"));
        assert!(!output.contains("f32"));
        assert!(!output.contains("f64"));
    }
}