flows_connector_dsi/
notion.rs

1use std::collections::HashMap;
2
3use serde::Serialize;
4
5#[derive(Serialize, Default)]
6pub struct OutboundData {
7    #[serde(flatten)]
8    inner: HashMap<String, String>,
9}
10
11impl OutboundData {
12    /// Sets the content of specified property, 
13    /// it will create a new property if it doesn't already exists
14    pub fn property<P: Into<String>, C: Into<String>>(
15        mut self,
16        property: P,
17        content: C,
18    ) -> OutboundData {
19        self.inner.insert(property.into(), content.into());
20        self
21    }
22
23    #[deprecated(
24        since = "0.1.4",
25        note = "Please use property() instead of page(), this name can be confusing."
26    )]
27    pub fn page<P: Into<String>, C: Into<String>>(self, property: P, content: C) -> OutboundData {
28        self.property(property, content)
29    }
30
31    /// Build outbound JSON data.
32    pub fn build(self) -> Result<String, String> {
33        if self.inner.is_empty() {
34            return Err("OutboundData build failed: No properties to create a page".to_string());
35        }
36
37        serde_json::to_string(&self)
38            .map_err(|e| format!("OutboundData build failed: {}", e.to_string()))
39    }
40}
41
42/// Create a Notion database page with multiple properties.
43///
44/// eg.
45/// ```rust
46/// /*
47/// | Name   | Email              |
48/// | ------ | ------------------ |
49/// | ho-229 | ho-229@example.com |
50/// */
51/// outbound()
52///     .property("Name", "ho-229")
53///     .property("Email", "ho-229@example.com")
54///     .build()
55/// ```
56pub fn outbound() -> OutboundData {
57    OutboundData::default()
58}