Skip to main content

kmp_plugin_api/domain/
interpreted_value.rs

1use serde::{Deserialize, Serialize};
2
3use super::calendar_date::CalendarDate;
4use super::currency_code::CurrencyCode;
5use super::math_expression_notation::MathExpressionNotation;
6use super::source_code_segment_kind::SourceCodeSegmentKind;
7
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[serde(tag = "kind", rename_all = "snake_case")]
10pub enum InterpretedValue {
11    Money {
12        currency: CurrencyCode,
13        amount_minor: i64,
14        amount: f64,
15    },
16    Date {
17        date: CalendarDate,
18    },
19    Number {
20        value: f64,
21        #[serde(skip_serializing_if = "Option::is_none")]
22        unit: Option<String>,
23    },
24    MathExpression {
25        notation: MathExpressionNotation,
26        expression: String,
27    },
28    SourceCode {
29        #[serde(skip_serializing_if = "Option::is_none")]
30        language: Option<String>,
31        segment_kind: SourceCodeSegmentKind,
32        text: String,
33    },
34    Url {
35        url: String,
36    },
37}
38
39impl InterpretedValue {
40    pub fn number(value: f64, unit: Option<String>) -> Self {
41        Self::Number { value, unit }
42    }
43
44    pub fn math_expression(
45        notation: MathExpressionNotation,
46        expression: impl Into<String>,
47    ) -> Self {
48        Self::MathExpression {
49            notation,
50            expression: expression.into(),
51        }
52    }
53
54    pub fn source_code(
55        language: Option<String>,
56        segment_kind: SourceCodeSegmentKind,
57        text: impl Into<String>,
58    ) -> Self {
59        Self::SourceCode {
60            language,
61            segment_kind,
62            text: text.into(),
63        }
64    }
65
66    pub fn url(url: impl Into<String>) -> Self {
67        Self::Url { url: url.into() }
68    }
69}