Skip to main content

beyondigital_auth_sdk/api/
runtime.rs

1//! Support types the generated models are written with (WP-21 R-21.1).
2
3use serde::{Deserialize, Deserializer, Serialize, Serializer};
4
5/// A field the contract lets a caller leave out and also lets be JSON `null`.
6///
7/// The contract keeps "absent" and "null" apart for an optional nullable field, and so does this type:
8/// `Absent` is left out of a request, `Null` is sent as `null`, and a response field is `Absent` when the key is
9/// missing. Wrap a value with `Maybe::from(value)` or `value.into()`.
10#[derive(Debug, Clone, PartialEq, Default)]
11pub enum Maybe<T> {
12    /// The key is not there.
13    #[default]
14    Absent,
15    /// The key is there and its value is `null`.
16    Null,
17    /// The key is there with a value.
18    Value(T),
19}
20
21impl<T> Maybe<T> {
22    pub fn is_absent(&self) -> bool {
23        matches!(self, Self::Absent)
24    }
25
26    pub fn is_null(&self) -> bool {
27        matches!(self, Self::Null)
28    }
29
30    /// The value, or `None` for both `Absent` and `Null`.
31    pub fn as_option(&self) -> Option<&T> {
32        match self {
33            Self::Value(value) => Some(value),
34            _ => None,
35        }
36    }
37
38    pub fn into_option(self) -> Option<T> {
39        match self {
40            Self::Value(value) => Some(value),
41            _ => None,
42        }
43    }
44}
45
46impl<T> From<T> for Maybe<T> {
47    fn from(value: T) -> Self {
48        Self::Value(value)
49    }
50}
51
52impl<T: Serialize> Serialize for Maybe<T> {
53    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
54        match self {
55            // A struct field skips `Absent` before it gets here; on its own it is the same as null.
56            Self::Absent | Self::Null => serializer.serialize_none(),
57            Self::Value(value) => value.serialize(serializer),
58        }
59    }
60}
61
62impl<'de, T: Deserialize<'de>> Deserialize<'de> for Maybe<T> {
63    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
64        // A key that is missing never reaches here: the field's `#[serde(default)]` gives `Absent`.
65        Ok(match Option::<T>::deserialize(deserializer)? {
66            Some(value) => Self::Value(value),
67            None => Self::Null,
68        })
69    }
70}
71
72/// An NDJSON export page: the raw text, each line parsed, and the cursor to pass as `after` for the next page.
73#[derive(Debug, Clone, PartialEq)]
74pub struct NdjsonPage {
75    pub text: String,
76    pub lines: Vec<serde_json::Value>,
77    pub next_cursor: Option<u64>,
78}
79
80/// A string enum from the contract. The generated enum has one variant per value the contract lists and an `Other`
81/// variant that keeps any other string, so a value the service adds later does not fail a call.
82macro_rules! string_enum {
83    ($(#[$meta:meta])* pub enum $name:ident { $($variant:ident = $value:literal,)+ }) => {
84        $(#[$meta])*
85        #[derive(Debug, Clone, PartialEq, Eq, Hash)]
86        pub enum $name {
87            $($variant,)+
88            /// A value this version of the SDK does not list.
89            Other(::std::string::String),
90        }
91
92        impl $name {
93            pub fn as_str(&self) -> &str {
94                match self {
95                    $(Self::$variant => $value,)+
96                    Self::Other(value) => value,
97                }
98            }
99        }
100
101        impl ::serde::Serialize for $name {
102            fn serialize<S: ::serde::Serializer>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error> {
103                serializer.serialize_str(self.as_str())
104            }
105        }
106
107        impl<'de> ::serde::Deserialize<'de> for $name {
108            fn deserialize<D: ::serde::Deserializer<'de>>(deserializer: D) -> ::std::result::Result<Self, D::Error> {
109                let value = <::std::string::String as ::serde::Deserialize>::deserialize(deserializer)?;
110                Ok(match value.as_str() {
111                    $($value => Self::$variant,)+
112                    _ => Self::Other(value),
113                })
114            }
115        }
116    };
117}
118pub(crate) use string_enum;