coda_api/ext/
rich_rows.rs

1use crate::types::{CurrencyAmount, ImageStatus, LinkedDataType, RowType, ScalarValue, TableReference, ValueFormat};
2use crate::{ValueFormatProvider, opt_f64_from_string_or_f64};
3use chrono::{DateTime, Utc};
4use derive_more::Display;
5use serde::{Deserialize, Serialize};
6use std::collections::HashMap;
7
8#[derive(Display, Deserialize, Serialize, Clone, Debug)]
9#[serde(deny_unknown_fields)]
10#[display("{name}")]
11pub struct RichRow {
12    #[serde(rename = "browserLink")]
13    pub browser_link: String,
14    #[serde(rename = "createdAt")]
15    pub created_at: DateTime<Utc>,
16    pub href: String,
17    pub id: String,
18    pub index: i64,
19    pub name: String,
20    #[serde(default, skip_serializing_if = "Option::is_none")]
21    pub parent: Option<TableReference>,
22    #[serde(rename = "type")]
23    pub type_: RowType,
24    #[serde(rename = "updatedAt")]
25    pub updated_at: DateTime<Utc>,
26    pub values: HashMap<String, RichValue>,
27}
28
29impl ValueFormatProvider for RichRow {
30    fn value_format() -> ValueFormat {
31        ValueFormat::Rich
32    }
33}
34
35#[derive(Deserialize, Serialize, Clone, Debug)]
36#[serde(untagged)]
37pub enum RichValue {
38    Single(RichSingleValue),
39    Collection(Vec<RichValueEntry>),
40}
41
42#[derive(Deserialize, Serialize, Clone, Debug)]
43#[serde(untagged)]
44pub enum RichValueEntry {
45    Single(RichSingleValue),
46    Many(Vec<RichSingleValue>),
47}
48
49#[derive(Deserialize, Serialize, Clone, Debug)]
50#[serde(untagged)]
51pub enum RichSingleValue {
52    Scalar(ScalarValue),
53    Currency(RichCurrencyValue),
54    Image(RichImageValue),
55    Person(RichPersonValue),
56    Url(RichUrlValue),
57    Row(RichRowReference),
58}
59
60#[derive(Deserialize, Serialize, Clone, Debug)]
61#[serde(deny_unknown_fields)]
62pub struct RichCurrencyValue {
63    #[serde(rename = "@context")]
64    pub context: String,
65    #[serde(rename = "@type")]
66    pub type_: LinkedDataType,
67    #[serde(rename = "additionalType", default, skip_serializing_if = "Option::is_none")]
68    pub additional_type: Option<String>,
69    pub currency: String,
70    pub amount: CurrencyAmount,
71}
72
73#[derive(Deserialize, Serialize, Clone, Debug)]
74#[serde(deny_unknown_fields)]
75pub struct RichImageValue {
76    #[serde(rename = "@context")]
77    pub context: String,
78    #[serde(rename = "@type")]
79    pub type_: LinkedDataType,
80    #[serde(rename = "additionalType", default, skip_serializing_if = "Option::is_none")]
81    pub additional_type: Option<String>,
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub name: Option<String>,
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub url: Option<String>,
86    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "opt_f64_from_string_or_f64")]
87    pub width: Option<f64>,
88    #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "opt_f64_from_string_or_f64")]
89    pub height: Option<f64>,
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    pub status: Option<ImageStatus>,
92}
93
94#[derive(Deserialize, Serialize, Clone, Debug)]
95#[serde(deny_unknown_fields)]
96pub struct RichPersonValue {
97    #[serde(rename = "@context")]
98    pub context: String,
99    #[serde(rename = "@type")]
100    pub type_: LinkedDataType,
101    #[serde(rename = "additionalType", default, skip_serializing_if = "Option::is_none")]
102    pub additional_type: Option<String>,
103    pub name: String,
104    #[serde(default, skip_serializing_if = "Option::is_none")]
105    pub email: Option<String>,
106}
107
108#[derive(Deserialize, Serialize, Clone, Debug)]
109#[serde(deny_unknown_fields)]
110pub struct RichUrlValue {
111    #[serde(rename = "@context")]
112    pub context: String,
113    #[serde(rename = "@type")]
114    pub type_: LinkedDataType,
115    #[serde(rename = "additionalType", default, skip_serializing_if = "Option::is_none")]
116    pub additional_type: Option<String>,
117    #[serde(default, skip_serializing_if = "Option::is_none")]
118    pub name: Option<String>,
119    pub url: String,
120}
121
122#[derive(Deserialize, Serialize, Clone, Debug)]
123#[serde(deny_unknown_fields)]
124pub struct RichRowReference {
125    #[serde(rename = "@context")]
126    pub context: String,
127    #[serde(rename = "@type")]
128    pub type_: LinkedDataType,
129    #[serde(rename = "additionalType")]
130    pub additional_type: String,
131    pub name: String,
132    pub url: String,
133    #[serde(rename = "tableId")]
134    pub table_id: String,
135    #[serde(rename = "rowId")]
136    pub row_id: String,
137    #[serde(rename = "tableUrl")]
138    pub table_url: String,
139}