bicmath-core 0.1.0

Core numeric contract, values, schemas, limits, and function contracts for BicMath.
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
//! Module and function contracts, invocation arguments, and outcomes.

use std::collections::BTreeMap;
use std::sync::Arc;

use num_bigint::BigInt;
use num_traits::ToPrimitive;
use serde::{Deserialize, Serialize};

use crate::context::{Conversion, ExecContext};
use crate::envelope::Exactness;
use crate::error::{EngineError, ErrorCode};
use crate::number::{Decimal, Number, NumericMode};
use crate::schema::ValueSchema;
use crate::value::Value;

/// A statically registered module.
#[derive(Clone)]
pub struct Module {
    pub descriptor: ModuleDescriptor,
    pub functions: Vec<Arc<dyn Function>>,
}

impl Module {
    pub fn new(descriptor: ModuleDescriptor, functions: Vec<Arc<dyn Function>>) -> Module {
        Module {
            descriptor,
            functions,
        }
    }
}

/// Metadata describing a module.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ModuleDescriptor {
    pub id: String,
    pub title: String,
    pub version: String,
    pub description: String,
    pub capabilities: Vec<String>,
    pub dependencies: Vec<String>,
    pub supported_modes: Vec<NumericMode>,
    pub license: String,
    pub source: String,
}

impl ModuleDescriptor {
    pub fn new(
        id: impl Into<String>,
        title: impl Into<String>,
        version: impl Into<String>,
        description: impl Into<String>,
    ) -> ModuleDescriptor {
        ModuleDescriptor {
            id: id.into(),
            title: title.into(),
            version: version.into(),
            description: description.into(),
            capabilities: Vec::new(),
            dependencies: Vec::new(),
            supported_modes: vec![NumericMode::Auto],
            license: "MIT".to_string(),
            source: String::new(),
        }
    }

    pub fn with_capabilities(
        mut self,
        capabilities: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.capabilities = capabilities.into_iter().map(Into::into).collect();
        self
    }

    pub fn with_dependencies(
        mut self,
        dependencies: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.dependencies = dependencies.into_iter().map(Into::into).collect();
        self
    }

    pub fn with_modes(mut self, modes: impl IntoIterator<Item = NumericMode>) -> Self {
        self.supported_modes = modes.into_iter().collect();
        self
    }

    pub fn with_source(mut self, source: impl Into<String>) -> Self {
        self.source = source.into();
        self
    }
}

/// Whether a function has side effects.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Purity {
    Pure,
}

/// Determinism class of a function.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Determinism {
    Deterministic,
    SeededRandom,
}

/// Rough cost class, used for documentation and batch budgeting.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CostClass {
    Constant,
    Linear,
    Quadratic,
    Cubic,
    Iterative,
}

/// Deprecation metadata.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Deprecation {
    pub since: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub replacement: Option<String>,
}

/// A function parameter.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ParamDescriptor {
    pub name: String,
    pub description: String,
    pub schema: ValueSchema,
    pub required: bool,
    /// Whether a string may be used as a numeric shorthand for this parameter.
    pub numeric_shorthand: bool,
    /// Whether the parameter may be supplied positionally in expressions.
    pub positional: bool,
}

impl ParamDescriptor {
    pub fn required(
        name: impl Into<String>,
        description: impl Into<String>,
        schema: ValueSchema,
    ) -> ParamDescriptor {
        let numeric = schema.is_numeric();
        ParamDescriptor {
            name: name.into(),
            description: description.into(),
            schema,
            required: true,
            numeric_shorthand: numeric,
            positional: true,
        }
    }

    pub fn optional(
        name: impl Into<String>,
        description: impl Into<String>,
        schema: ValueSchema,
    ) -> ParamDescriptor {
        let numeric = schema.is_numeric();
        ParamDescriptor {
            name: name.into(),
            description: description.into(),
            schema,
            required: false,
            numeric_shorthand: numeric,
            positional: true,
        }
    }

    pub fn with_shorthand(mut self, allowed: bool) -> Self {
        self.numeric_shorthand = allowed;
        self
    }

    pub fn with_positional(mut self, allowed: bool) -> Self {
        self.positional = allowed;
        self
    }
}

/// Expected outcome of a documented example.
///
/// Serialized explicitly because an internally tagged enum cannot represent
/// newtype variants whose payload is not a map (booleans, arrays, strings).
#[derive(Clone, Debug, PartialEq)]
pub enum ExampleExpectation {
    Value(Value),
    Error(ErrorCode),
    Contains(String),
}

impl Serialize for ExampleExpectation {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;
        match self {
            ExampleExpectation::Value(value) => {
                let mut map = serializer.serialize_map(Some(2))?;
                map.serialize_entry("type", "value")?;
                map.serialize_entry("value", value)?;
                map.end()
            }
            ExampleExpectation::Error(code) => {
                let mut map = serializer.serialize_map(Some(2))?;
                map.serialize_entry("type", "error")?;
                map.serialize_entry("code", code)?;
                map.end()
            }
            ExampleExpectation::Contains(text) => {
                let mut map = serializer.serialize_map(Some(2))?;
                map.serialize_entry("type", "contains")?;
                map.serialize_entry("text", text)?;
                map.end()
            }
        }
    }
}

impl<'de> Deserialize<'de> for ExampleExpectation {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        use serde::de::Error as DeError;
        let raw = serde_json::Value::deserialize(deserializer)?;
        let kind = raw
            .get("type")
            .and_then(|value| value.as_str())
            .ok_or_else(|| D::Error::custom("example expectation is missing \"type\""))?;
        match kind {
            "value" => {
                let value = raw
                    .get("value")
                    .ok_or_else(|| D::Error::custom("value expectation is missing \"value\""))?;
                Ok(ExampleExpectation::Value(
                    serde_json::from_value(value.clone()).map_err(D::Error::custom)?,
                ))
            }
            "error" => {
                let code = raw
                    .get("code")
                    .ok_or_else(|| D::Error::custom("error expectation is missing \"code\""))?;
                Ok(ExampleExpectation::Error(
                    serde_json::from_value(code.clone()).map_err(D::Error::custom)?,
                ))
            }
            "contains" => {
                let text = raw
                    .get("text")
                    .and_then(|value| value.as_str())
                    .ok_or_else(|| D::Error::custom("contains expectation is missing \"text\""))?;
                Ok(ExampleExpectation::Contains(text.to_string()))
            }
            other => Err(D::Error::custom(format!(
                "unknown example expectation type {other:?}"
            ))),
        }
    }
}

/// A documented, executable example. CI runs every example and checks the
/// expectation so documentation cannot drift from behaviour.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Example {
    pub title: String,
    pub arguments: BTreeMap<String, Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expected: Option<ExampleExpectation>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub note: Option<String>,
}

impl Example {
    pub fn new(title: impl Into<String>, arguments: BTreeMap<String, Value>) -> Example {
        Example {
            title: title.into(),
            arguments,
            expected: None,
            note: None,
        }
    }

    pub fn with_value(mut self, value: Value) -> Example {
        self.expected = Some(ExampleExpectation::Value(value));
        self
    }

    pub fn with_error(mut self, code: ErrorCode) -> Example {
        self.expected = Some(ExampleExpectation::Error(code));
        self
    }

    pub fn with_contains(mut self, text: impl Into<String>) -> Example {
        self.expected = Some(ExampleExpectation::Contains(text.into()));
        self
    }
}

/// The authoritative function declaration.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FunctionDescriptor {
    pub id: String,
    pub module: String,
    pub version: String,
    pub title: String,
    pub summary: String,
    pub description: String,
    pub parameters: Vec<ParamDescriptor>,
    pub output: ValueSchema,
    pub output_description: String,
    pub modes: Vec<NumericMode>,
    pub purity: Purity,
    pub determinism: Determinism,
    pub cost: CostClass,
    pub units_rule: String,
    pub method_ref: String,
    pub examples: Vec<Example>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<Deprecation>,
    pub tags: Vec<String>,
}

impl FunctionDescriptor {
    pub fn new(
        id: impl Into<String>,
        module: impl Into<String>,
        version: impl Into<String>,
        title: impl Into<String>,
        summary: impl Into<String>,
    ) -> FunctionDescriptor {
        FunctionDescriptor {
            id: id.into(),
            module: module.into(),
            version: version.into(),
            title: title.into(),
            summary: summary.into(),
            description: String::new(),
            parameters: Vec::new(),
            output: ValueSchema::Any,
            output_description: String::new(),
            modes: vec![NumericMode::Auto],
            purity: Purity::Pure,
            determinism: Determinism::Deterministic,
            cost: CostClass::Constant,
            units_rule: String::new(),
            method_ref: String::new(),
            examples: Vec::new(),
            deprecated: None,
            tags: Vec::new(),
        }
    }

    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    pub fn with_parameters(mut self, parameters: Vec<ParamDescriptor>) -> Self {
        self.parameters = parameters;
        self
    }

    pub fn with_output(mut self, output: ValueSchema, description: impl Into<String>) -> Self {
        self.output = output;
        self.output_description = description.into();
        self
    }

    pub fn with_modes(mut self, modes: impl IntoIterator<Item = NumericMode>) -> Self {
        self.modes = modes.into_iter().collect();
        self
    }

    pub fn with_cost(mut self, cost: CostClass) -> Self {
        self.cost = cost;
        self
    }

    pub fn with_units_rule(mut self, rule: impl Into<String>) -> Self {
        self.units_rule = rule.into();
        self
    }

    pub fn with_method_ref(mut self, method_ref: impl Into<String>) -> Self {
        self.method_ref = method_ref.into();
        self
    }

    pub fn with_examples(mut self, examples: Vec<Example>) -> Self {
        self.examples = examples;
        self
    }

    pub fn with_tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.tags = tags.into_iter().map(Into::into).collect();
        self
    }

    pub fn with_deprecation(mut self, deprecation: Deprecation) -> Self {
        self.deprecated = Some(deprecation);
        self
    }

    pub fn parameter(&self, name: &str) -> Option<&ParamDescriptor> {
        self.parameters.iter().find(|p| p.name == name)
    }
}

/// A reference to a function and its version.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionRef {
    pub id: String,
    pub version: String,
}

/// Warning attached to a result.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Warning {
    pub code: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Value>,
}

impl Warning {
    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Warning {
        Warning {
            code: code.into(),
            message: message.into(),
            details: None,
        }
    }

    pub fn with_details(mut self, details: Value) -> Warning {
        self.details = Some(details);
        self
    }
}

/// Where an assumption came from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AssumptionSource {
    /// Supplied by the caller and not verified by the engine.
    UserSupplied,
    /// Checked by the engine for the supplied data.
    Checked,
    /// Required by the method but not verifiable from the supplied inputs.
    Unverified,
}

/// An explicit method assumption.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Assumption {
    pub id: String,
    pub statement: String,
    pub source: AssumptionSource,
}

impl Assumption {
    pub fn user_supplied(id: impl Into<String>, statement: impl Into<String>) -> Assumption {
        Assumption {
            id: id.into(),
            statement: statement.into(),
            source: AssumptionSource::UserSupplied,
        }
    }

    pub fn checked(id: impl Into<String>, statement: impl Into<String>) -> Assumption {
        Assumption {
            id: id.into(),
            statement: statement.into(),
            source: AssumptionSource::Checked,
        }
    }

    pub fn unverified(id: impl Into<String>, statement: impl Into<String>) -> Assumption {
        Assumption {
            id: id.into(),
            statement: statement.into(),
            source: AssumptionSource::Unverified,
        }
    }
}

/// A validated numerical error estimate, only present when the method can
/// actually provide one.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ErrorEstimate {
    pub kind: String,
    pub bound: Value,
    pub method: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes: Option<String>,
}

impl ErrorEstimate {
    pub fn new(kind: impl Into<String>, bound: Value, method: impl Into<String>) -> ErrorEstimate {
        ErrorEstimate {
            kind: kind.into(),
            bound,
            method: method.into(),
            notes: None,
        }
    }

    pub fn with_notes(mut self, notes: impl Into<String>) -> ErrorEstimate {
        self.notes = Some(notes.into());
        self
    }
}

/// One bounded trace step.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TraceStep {
    pub op: String,
    pub detail: BTreeMap<String, Value>,
}

/// A bounded execution trace of computational steps. This is not a narrative
/// explanation and contains no hidden reasoning.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Trace {
    pub steps: Vec<TraceStep>,
}

impl Trace {
    pub fn new() -> Trace {
        Trace { steps: Vec::new() }
    }

    pub fn push(&mut self, step: TraceStep) {
        self.steps.push(step);
    }
}

/// A successful invocation outcome.
#[derive(Clone, Debug)]
pub struct Outcome {
    pub value: Value,
    pub exactness: Exactness,
    pub warnings: Vec<Warning>,
    pub assumptions: Vec<Assumption>,
    pub error_estimate: Option<ErrorEstimate>,
    pub trace: Option<Trace>,
    pub conversions: Vec<Conversion>,
}

impl Outcome {
    pub fn new(value: Value, exactness: Exactness) -> Outcome {
        Outcome {
            value,
            exactness,
            warnings: Vec::new(),
            assumptions: Vec::new(),
            error_estimate: None,
            trace: None,
            conversions: Vec::new(),
        }
    }

    pub fn exact(value: Value) -> Outcome {
        Outcome::new(value, Exactness::Exact)
    }

    pub fn rounded(value: Value) -> Outcome {
        Outcome::new(value, Exactness::Rounded)
    }

    pub fn approximate(value: Value) -> Outcome {
        Outcome::new(value, Exactness::Approximate)
    }

    pub fn with_warning(mut self, warning: Warning) -> Outcome {
        self.warnings.push(warning);
        self
    }

    pub fn with_assumption(mut self, assumption: Assumption) -> Outcome {
        self.assumptions.push(assumption);
        self
    }

    pub fn with_error_estimate(mut self, estimate: ErrorEstimate) -> Outcome {
        self.error_estimate = Some(estimate);
        self
    }

    pub fn with_trace(mut self, trace: Trace) -> Outcome {
        self.trace = Some(trace);
        self
    }

    pub fn with_conversion(mut self, conversion: Conversion) -> Outcome {
        self.conversions.push(conversion);
        self
    }
}

/// Validated arguments for a function invocation.
#[derive(Clone, Debug, Default)]
pub struct Args {
    values: BTreeMap<String, Value>,
}

impl Args {
    pub fn new(values: BTreeMap<String, Value>) -> Args {
        Args { values }
    }

    pub fn get(&self, name: &str) -> Option<&Value> {
        self.values.get(name)
    }

    pub fn contains(&self, name: &str) -> bool {
        self.values.contains_key(name)
    }

    pub fn iter(&self) -> impl Iterator<Item = (&String, &Value)> {
        self.values.iter()
    }

    pub fn into_map(self) -> BTreeMap<String, Value> {
        self.values
    }

    fn expected(name: &str, kind: &str) -> EngineError {
        EngineError::malformed(format!(
            "missing or invalid argument {name:?}: expected {kind}"
        ))
        .with_path(name.to_string())
    }

    pub fn require(&self, name: &str) -> Result<&Value, EngineError> {
        self.values
            .get(name)
            .ok_or_else(|| Self::expected(name, "a value"))
    }

    pub fn number(&self, name: &str) -> Result<&Number, EngineError> {
        self.require(name)?
            .as_number()
            .map_err(|e| e.with_path(name.to_string()))
    }

    pub fn optional_number(&self, name: &str) -> Result<Option<&Number>, EngineError> {
        match self.values.get(name) {
            None | Some(Value::Null) => Ok(None),
            Some(value) => Ok(Some(
                value
                    .as_number()
                    .map_err(|e| e.with_path(name.to_string()))?,
            )),
        }
    }

    pub fn text(&self, name: &str) -> Result<&str, EngineError> {
        self.require(name)?
            .as_text()
            .map_err(|e| e.with_path(name.to_string()))
    }

    pub fn optional_text(&self, name: &str) -> Result<Option<&str>, EngineError> {
        match self.values.get(name) {
            None | Some(Value::Null) => Ok(None),
            Some(value) => Ok(Some(
                value.as_text().map_err(|e| e.with_path(name.to_string()))?,
            )),
        }
    }

    pub fn bool(&self, name: &str) -> Result<bool, EngineError> {
        self.require(name)?
            .as_bool()
            .map_err(|e| e.with_path(name.to_string()))
    }

    pub fn optional_bool(&self, name: &str) -> Result<Option<bool>, EngineError> {
        match self.values.get(name) {
            None | Some(Value::Null) => Ok(None),
            Some(value) => Ok(Some(
                value.as_bool().map_err(|e| e.with_path(name.to_string()))?,
            )),
        }
    }

    pub fn array(&self, name: &str) -> Result<&[Value], EngineError> {
        self.require(name)?
            .as_array()
            .map_err(|e| e.with_path(name.to_string()))
    }

    pub fn record(&self, name: &str) -> Result<&BTreeMap<String, Value>, EngineError> {
        self.require(name)?
            .as_record()
            .map_err(|e| e.with_path(name.to_string()))
    }

    pub fn money(&self, name: &str) -> Result<(&Number, &str), EngineError> {
        self.require(name)?
            .as_money()
            .map_err(|e| e.with_path(name.to_string()))
    }

    pub fn quantity(&self, name: &str) -> Result<(&Number, crate::value::Dimension), EngineError> {
        self.require(name)?
            .as_quantity()
            .map_err(|e| e.with_path(name.to_string()))
    }

    pub fn integer(&self, name: &str) -> Result<BigInt, EngineError> {
        match self.number(name)? {
            Number::Integer(value) => Ok(value.clone()),
            other => Err(Self::expected(
                name,
                &format!("an integer, found {}", other.kind_name()),
            )),
        }
    }

    pub fn optional_integer(&self, name: &str) -> Result<Option<BigInt>, EngineError> {
        match self.optional_number(name)? {
            None => Ok(None),
            Some(Number::Integer(value)) => Ok(Some(value.clone())),
            Some(other) => Err(Self::expected(
                name,
                &format!("an integer, found {}", other.kind_name()),
            )),
        }
    }

    /// Convert an integer argument to `usize`, rejecting negatives and overflow.
    pub fn usize_param(&self, name: &str) -> Result<usize, EngineError> {
        let value = self.integer(name)?;
        value
            .to_usize()
            .ok_or_else(|| Self::expected(name, "a non-negative machine-sized integer"))
    }

    pub fn u32_param(&self, name: &str) -> Result<u32, EngineError> {
        let value = self.integer(name)?;
        value
            .to_u32()
            .ok_or_else(|| Self::expected(name, "a non-negative 32-bit integer"))
    }

    pub fn u64_param(&self, name: &str) -> Result<u64, EngineError> {
        let value = self.integer(name)?;
        value
            .to_u64()
            .ok_or_else(|| Self::expected(name, "a non-negative 64-bit integer"))
    }

    /// Convert a numeric argument to f64. This is an explicit conversion and is
    /// recorded by callers when it matters; it never happens for exact results.
    pub fn f64_param(&self, name: &str) -> Result<f64, EngineError> {
        let value = self.number(name)?;
        value
            .to_f64()
            .ok_or_else(|| Self::expected(name, "a value representable as float64"))
    }

    pub fn optional_f64(&self, name: &str) -> Result<Option<f64>, EngineError> {
        match self.optional_number(name)? {
            None => Ok(None),
            Some(value) => value
                .to_f64()
                .map(Some)
                .ok_or_else(|| Self::expected(name, "a value representable as float64")),
        }
    }

    pub fn decimal(&self, name: &str) -> Result<Decimal, EngineError> {
        match self.number(name)? {
            Number::Decimal(value) => Ok(value.clone()),
            Number::Integer(value) => Ok(Decimal::from_bigint(value.clone())),
            other => Err(Self::expected(
                name,
                &format!("a decimal, found {}", other.kind_name()),
            )),
        }
    }

    pub fn optional_decimal(&self, name: &str) -> Result<Option<Decimal>, EngineError> {
        match self.optional_number(name)? {
            None => Ok(None),
            Some(number) => match number {
                Number::Decimal(value) => Ok(Some(value.clone())),
                Number::Integer(value) => Ok(Some(Decimal::from_bigint(value.clone()))),
                other => Err(Self::expected(
                    name,
                    &format!("a decimal, found {}", other.kind_name()),
                )),
            },
        }
    }
}

/// The callable interface implemented by every function in every module.
pub trait Function: Send + Sync {
    fn descriptor(&self) -> &FunctionDescriptor;
    fn invoke(&self, args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError>;
}

/// A function implemented by a plain function pointer plus its descriptor.
///
/// This is the explicit registration mechanism used by the numerical modules:
/// no macro framework, no hidden behaviour. The descriptor is the
/// authoritative schema; the engine validates arguments against it before
/// [`Function::invoke`] is called.
pub struct SimpleFunction {
    descriptor: FunctionDescriptor,
    invoke: fn(&Args, &ExecContext) -> Result<Outcome, EngineError>,
}

impl SimpleFunction {
    pub fn new(
        descriptor: FunctionDescriptor,
        invoke: fn(&Args, &ExecContext) -> Result<Outcome, EngineError>,
    ) -> SimpleFunction {
        SimpleFunction { descriptor, invoke }
    }

    pub fn arc(
        descriptor: FunctionDescriptor,
        invoke: fn(&Args, &ExecContext) -> Result<Outcome, EngineError>,
    ) -> Arc<dyn Function> {
        Arc::new(SimpleFunction::new(descriptor, invoke))
    }
}

impl Function for SimpleFunction {
    fn descriptor(&self) -> &FunctionDescriptor {
        &self.descriptor
    }

    fn invoke(&self, args: &Args, ctx: &ExecContext) -> Result<Outcome, EngineError> {
        (self.invoke)(args, ctx)
    }
}

/// Helper to require that the context mode is supported by a function.
pub fn require_mode(
    ctx: &ExecContext,
    modes: &[NumericMode],
    function_id: &str,
) -> Result<(), EngineError> {
    if modes.contains(&ctx.numeric.mode) {
        Ok(())
    } else {
        Err(EngineError::new(
            ErrorCode::UnsupportedNumericMode,
            format!(
                "function {function_id} supports modes {:?}, not {}",
                modes, ctx.numeric.mode
            ),
        ))
    }
}