ph-curves 0.3.0

no-std, no-alloc Rust for embedded: curve lookup tables, ADC-to-measurement transfer functions with inverse and calibration, temporal filters, and tickless scheduling
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
//! Public code-generation entry points for `build.rs` and host tools.

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

use std::format;
use std::prelude::v1::*;

use std::fs;
use std::io;
use std::path::Path;

use super::codegen;
use super::curve::DefinitionsFile;

/// LUT index / value width used by generated curve tables.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueType {
    /// 8-bit domain (`lut_size` must be 256).
    U8,
    /// 16-bit domain (`lut_size` must be 65_536).
    U16,
}

impl ValueType {
    /// Parse a CLI / TOML-style type name (`"u8"` or `"u16"`).
    pub fn parse(name: &str) -> Result<Self, Error> {
        match name {
            "u8" => Ok(Self::U8),
            "u16" => Ok(Self::U16),
            other => Err(Error::Validation(format!(
                "unsupported value type `{other}` (expected `u8` or `u16`)"
            ))),
        }
    }

    /// Rust type name emitted in generated source.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::U8 => "u8",
            Self::U16 => "u16",
        }
    }

    /// Required full-domain LUT length for this value type.
    pub const fn required_lut_size(self) -> usize {
        match self {
            Self::U8 => 256,
            Self::U16 => 65_536,
        }
    }
}

/// Options controlling curve LUT generation.
///
/// Defaults match the CLI today: `u8` values with a 256-entry LUT.
/// Transfer-only documents should use [`Self::transfers_only`]; `value_type`
/// and `lut_size` are ignored unless the document contains `[curves]`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GenerateOptions {
    /// Rust type used for LUT index and value (`u8` or `u16`).
    /// Ignored when the document has no `[curves]`.
    pub value_type: ValueType,
    /// Number of entries in each LUT (must cover the full value-type domain).
    /// Ignored when the document has no `[curves]`.
    pub lut_size: usize,
}

impl Default for GenerateOptions {
    fn default() -> Self {
        Self {
            value_type: ValueType::U8,
            lut_size: ValueType::U8.required_lut_size(),
        }
    }
}

impl GenerateOptions {
    /// Options for a document with no `[curves]`.
    ///
    /// LUT fields keep CLI-compatible defaults but are not validated or used
    /// when the definitions contain only transfers, families, or gaps.
    pub fn transfers_only() -> Self {
        Self::default()
    }
}

/// Errors from reading TOML, validating options, or generating source.
#[derive(Debug)]
pub enum Error {
    /// Filesystem read/write failure.
    Io(io::Error),
    /// Input was not valid TOML for the definitions schema.
    Toml(toml::de::Error),
    /// Options or definition content failed validation.
    Validation(String),
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Io(error) => write!(f, "{error}"),
            Self::Toml(error) => write!(f, "invalid TOML: {error}"),
            Self::Validation(message) => write!(f, "{message}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(error) => Some(error),
            Self::Toml(error) => Some(error),
            Self::Validation(_) => None,
        }
    }
}

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

impl From<toml::de::Error> for Error {
    fn from(error: toml::de::Error) -> Self {
        Self::Toml(error)
    }
}

/// Read a TOML definitions file and return generated Rust source.
pub fn generate_from_toml(path: impl AsRef<Path>, opts: &GenerateOptions) -> Result<String, Error> {
    let toml_str = fs::read_to_string(path)?;
    generate_from_str(&toml_str, opts)
}

/// Parse an in-memory TOML string and return generated Rust source.
pub fn generate_from_str(toml: &str, opts: &GenerateOptions) -> Result<String, Error> {
    let defs = DefinitionsFile::from_toml_str(toml)?;
    generate(&defs, opts)
}

/// Generate Rust source from `input` TOML and write it to `output`.
pub fn generate_to_path(
    input: impl AsRef<Path>,
    output: impl AsRef<Path>,
    opts: &GenerateOptions,
) -> Result<(), Error> {
    let source = generate_from_toml(input, opts)?;
    fs::write(output, source)?;
    Ok(())
}

/// Lower-level entry: already-parsed definitions → Rust source.
pub fn generate(defs: &DefinitionsFile, opts: &GenerateOptions) -> Result<String, Error> {
    if !defs.curves().is_empty() {
        validate_options(opts)?;
    }
    codegen::generate(defs, opts.value_type.as_str(), opts.lut_size).map_err(Error::Validation)
}

/// Read a TOML definitions file and return source plus the host audit report.
pub fn generate_from_toml_report(
    path: impl AsRef<Path>,
    opts: &GenerateOptions,
) -> Result<super::GenerationResult, Error> {
    let toml_str = fs::read_to_string(path)?;
    generate_from_str_report(&toml_str, opts)
}

/// Parse an in-memory TOML string and return source plus the host audit report.
pub fn generate_from_str_report(
    toml: &str,
    opts: &GenerateOptions,
) -> Result<super::GenerationResult, Error> {
    let defs = DefinitionsFile::from_toml_str(toml)?;
    generate_report(&defs, opts)
}

/// Lower-level entry: already-parsed definitions → source plus the host audit report.
///
/// Family aggregate budgets fail closed here. The `String`-returning helpers
/// call this function and discard the report, so they cannot bypass a budget.
pub fn generate_report(
    defs: &DefinitionsFile,
    opts: &GenerateOptions,
) -> Result<super::GenerationResult, Error> {
    if !defs.curves().is_empty() {
        validate_options(opts)?;
    }
    codegen::generate_with_report(defs, opts.value_type.as_str(), opts.lut_size)
        .map_err(Error::Validation)
}

fn validate_options(opts: &GenerateOptions) -> Result<(), Error> {
    let required = opts.value_type.required_lut_size();
    if opts.lut_size != required {
        return Err(Error::Validation(format!(
            "--lut-size must be {required} for --value-type {} \
             (the LUT must cover the full {} domain)",
            opts.value_type.as_str(),
            opts.value_type.as_str(),
        )));
    }
    Ok(())
}

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

    #[test]
    fn lut_size_must_cover_full_u8_domain() {
        let ok = GenerateOptions {
            value_type: ValueType::U8,
            lut_size: 256,
        };
        assert!(validate_options(&ok).is_ok());

        let bad = GenerateOptions {
            value_type: ValueType::U8,
            lut_size: 255,
        };
        assert_eq!(
            validate_options(&bad).unwrap_err().to_string(),
            "--lut-size must be 256 for --value-type u8 \
             (the LUT must cover the full u8 domain)"
        );
    }

    #[test]
    fn lut_size_must_cover_full_u16_domain() {
        let ok = GenerateOptions {
            value_type: ValueType::U16,
            lut_size: 65_536,
        };
        assert!(validate_options(&ok).is_ok());

        let bad = GenerateOptions {
            value_type: ValueType::U16,
            lut_size: 256,
        };
        assert_eq!(
            validate_options(&bad).unwrap_err().to_string(),
            "--lut-size must be 65536 for --value-type u16 \
             (the LUT must cover the full u16 domain)"
        );
    }

    #[test]
    fn value_type_parse_rejects_unknown() {
        let error = ValueType::parse("u32").unwrap_err().to_string();
        assert!(error.contains("unsupported value type"));
    }

    #[test]
    fn generate_from_str_linear_curve() {
        let toml = r#"
            [curves.linear]
            builtin = "linear"
        "#;
        let out = generate_from_str(toml, &GenerateOptions::default()).unwrap();
        assert!(out.contains("LINEAR_FWD"));
        assert!(out.contains("LINEAR_INV"));
        assert!(out.contains("// Auto-generated by ph-curves-gen."));
    }

    #[test]
    fn semantic_validation_returns_error_instead_of_panicking() {
        let error = generate_from_str("[curves.bad]\n", &GenerateOptions::default()).unwrap_err();

        assert!(matches!(error, Error::Validation(_)));
        assert!(error.to_string().contains("exactly one"));
    }

    #[test]
    fn invalid_formula_returns_validation_error() {
        let error = generate_from_str(
            "[curves.bad]\nformula = \"t @ 2\"\n",
            &GenerateOptions::default(),
        )
        .unwrap_err();

        assert!(matches!(error, Error::Validation(_)));
        assert!(error.to_string().contains("unexpected character"));
    }

    #[test]
    fn misspelled_transfers_table_returns_toml_error() {
        let error =
            generate_from_str("[tranfsers.sensor]\n", &GenerateOptions::default()).unwrap_err();

        assert!(matches!(error, Error::Toml(_)));
        let message = error.to_string();
        assert!(
            message.contains("unknown field `tranfsers`"),
            "expected the misspelled table to be named, got: {message}"
        );
    }

    #[test]
    fn unknown_table_returns_toml_error_not_header_only() {
        let toml = "[invented]\nfoo = 1\n";
        let error = generate_from_str(toml, &GenerateOptions::default()).unwrap_err();

        assert!(matches!(error, Error::Toml(_)));
        let message = error.to_string();
        assert!(
            message.contains("unknown field `invented`"),
            "expected a named unknown top-level table, got: {message}"
        );
    }

    #[test]
    fn misspelled_standalone_saturation_returns_toml_error() {
        let toml = r#"
[transfers]
requires = ["observation_guard_v1"]

[transfers.sensor]
input_unit = "count"
output_unit = "unit"
output_scale = 1
max_interpolation_error = 1
saturaton = { code = 65535, behavior = "error" }
formula = "x"
domain = [1, 10]
"#;
        let error = generate_from_str(toml, &GenerateOptions::transfers_only()).unwrap_err();
        assert!(matches!(error, Error::Toml(_)));
        assert!(error.to_string().contains("unknown field `saturaton`"));
    }

    #[test]
    fn standalone_guard_without_capability_returns_toml_error() {
        let toml = r#"
[transfers.sensor]
input_unit = "count"
output_unit = "unit"
output_scale = 1
max_interpolation_error = 1
saturation = { code = 65535, behavior = "error" }
formula = "x"
domain = [1, 10]
"#;
        let error = generate_from_str(toml, &GenerateOptions::transfers_only()).unwrap_err();
        assert!(matches!(error, Error::Toml(_)));
        assert!(
            error
                .to_string()
                .contains("requires = [\"observation_guard_v1\"]")
        );
    }

    #[test]
    fn malformed_description_only_member_fails_generate() {
        let toml = r#"
[transfers]
requires = ["transfer_families_v1"]

[transfer_families.als]
provenance = { identity = "test fixture" }
input_unit = "count"
output_unit = "unit"
output_scale = 1000
max_interpolation_error = 50
formula = "x"
selector_axes = { gain = ["div4", "x1"], integration_time_ms = [100] }

[[transfer_families.als.members]]
selectors = { gain = "div4", integration_time_ms = 100 }
status = "emit"
applicability = { observation = [1, 10] }

[[transfer_families.als.members]]
selectors = { gain = "x1", integration_time_ms = 100 }
status = "unnecessary"
reason = "still validated"
applicability = { observation = [10, 1] }
"#;
        let error = generate_from_str(toml, &GenerateOptions::default()).unwrap_err();
        assert!(matches!(error, Error::Validation(_)));
        assert!(error.to_string().contains("applicability.observation"));
    }

    #[test]
    fn transfer_only_generation_skips_lut_size_validation() {
        let toml = r#"
            [transfers.linear]
            input_unit = "code"
            output_unit = "unit"
            output_scale = 1
            max_interpolation_error = 1
            points = [
              { input = 0, output = 0.0 },
              { input = 10, output = 10.0 },
            ]
        "#;
        let opts = GenerateOptions {
            value_type: ValueType::U8,
            lut_size: 1,
        };
        let out = generate_from_str(toml, &opts).unwrap();
        assert!(out.contains("PiecewiseLinearTransfer"));
        assert!(!out.contains("CurveLut"));
    }

    #[test]
    fn curves_still_require_full_lut_domain() {
        let toml = r#"
            [curves.linear]
            builtin = "linear"
        "#;
        let opts = GenerateOptions {
            value_type: ValueType::U8,
            lut_size: 1,
        };
        let error = generate_from_str(toml, &opts).unwrap_err();
        assert!(error.to_string().contains("--lut-size must be 256"));
    }
}