drive_v3/objects/
channel.rs

1use std::fmt;
2use serde::{Serialize, Deserialize};
3
4/// A notification channel used to watch for resource changes.
5///
6/// # Warning:
7///
8/// The field `type` is renamed to `channel_type` as the word type is a reserved keyword in Rust.
9#[derive(Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct Channel {
12    /// A Boolean value to indicate whether payload is wanted.
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub payload: Option<bool>,
15
16    /// A UUID or similar unique string that identifies this channel.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub id: Option<String>,
19
20    /// An opaque ID that identifies the resource being watched on this channel.
21    ///
22    /// Stable across different API versions.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub resource_id: Option<String>,
25
26    /// A version-specific identifier for the watched resource.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub resource_uri: Option<String>,
29
30    /// An arbitrary string delivered to the target address with each
31    /// notification delivered over this channel.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub token: Option<String>,
34
35    /// Date and time of notification channel expiration, expressed as a Unix
36    /// timestamp, in milliseconds.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub expiration: Option<String>,
39
40    /// The type of delivery mechanism used for this channel.
41    ///
42    /// Valid values are `web_hook` or `webhook`.
43    #[serde(skip_serializing_if = "Option::is_none")]
44    #[serde(rename = "type")]
45    pub channel_type: Option<String>,
46
47    /// The address where notifications are delivered for this channel.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub address: Option<String>,
50
51    /// Additional parameters controlling delivery channel behavior.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub params: Option<serde_json::Map<String, serde_json::Value>>,
54
55    /// Identifies this as a notification channel, which is `api#channel`.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub kind: Option<String>,
58}
59
60impl fmt::Debug for Channel {
61    fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result {
62        f.debug_struct("Channel")
63            .field("payload", &self.payload)
64            .field("id", &self.id)
65            .field("resource_id", &self.resource_id)
66            .field("resource_uri", &self.resource_uri)
67            .field("token", &format_args!("[hidden for security]"))
68            .field("expiration", &self.expiration)
69            .field("channel_type", &self.channel_type)
70            .field("address", &self.address)
71            .field("params", &self.params)
72            .field("kind", &self.kind)
73            .finish()
74    }
75}
76
77
78impl fmt::Display for Channel {
79    fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result {
80        let json = serde_json::to_string_pretty(&self)
81            .unwrap_or( format!("unable to parse JSON, this is the debug view:\n{:#?}", self) );
82
83        write!(f, "{}", json)
84    }
85}
86
87impl Default for Channel {
88    fn default() -> Self {
89        Self {
90            payload: Default::default(),
91            id: Default::default(),
92            resource_id: Default::default(),
93            resource_uri: Default::default(),
94            token: Default::default(),
95            expiration: Default::default(),
96            channel_type: Some( "web_hook".to_string() ),
97            address: Default::default(),
98            params: Default::default(),
99            kind: Default::default(),
100        }
101    }
102}
103
104#[doc(hidden)]
105impl From<&Self> for Channel {
106    fn from( reference: &Self ) -> Self {
107        reference.clone()
108    }
109}
110
111impl Channel {
112    /// Creates a new, empty instance of this struct.
113    pub fn new() -> Self {
114        Self { ..Default::default() }
115    }
116
117    /// Creates a new instance of this struct with the given address.
118    pub fn from<T, U> ( channel_id: T, address: U ) -> Self
119        where
120            T: AsRef<str>,
121            U: AsRef<str>,
122    {
123        Self {
124            id: Some( channel_id.as_ref().to_string() ),
125            address: Some( address.as_ref().to_string() ),
126            ..Default::default()
127        }
128    }
129}