use serde::{Deserialize, Serialize};
pub type Url = String;
pub type JsonValue = Box<serde_json::Value>;
pub type TimestampString = String;
macro_rules! define_string_wrapper {
($($(#[$meta:meta])* $vis:vis $name:ident;)*) => { $(
$(#[$meta])*
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
$vis struct $name(pub String);
impl $name {
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
)* };
}
define_string_wrapper! {
pub DriveId;
pub ItemId;
pub Tag;
}
#[doc(hidden)]
#[allow(clippy::module_name_repetitions)] pub trait ResourceField: Copy {
fn __raw_name(self) -> &'static str;
}
macro_rules! define_resource_object {
($(
$(#[$meta:meta])*
$vis:vis struct $struct_name:ident #$field_enum_name:ident {
$(
$(#[$field_meta:meta])*
pub $field_name:ident
$(@$field_rename:literal)?
: Option<$field_ty:ty>,
)*
}
)*) => {
$(
$(#[$meta])*
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
$vis struct $struct_name {
$(
#[allow(missing_docs)]
#[serde(skip_serializing_if="Option::is_none")]
$(#[$field_meta])*
$(#[serde(rename = $field_rename)])?
pub $field_name: Option<$field_ty>,
)*
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, strum::VariantNames)]
#[strum(serialize_all = "camelCase")]
#[non_exhaustive]
#[allow(missing_docs, non_camel_case_types)]
$vis enum $field_enum_name {
$(
$(#[strum(serialize = $field_rename)])?
$field_name,
)*
}
impl $field_enum_name {
#[must_use]
pub fn raw_name(self) -> &'static str {
<Self as strum::VariantNames>::VARIANTS[self as usize]
}
}
impl ResourceField for $field_enum_name {
#[inline]
fn __raw_name(self) -> &'static str {
self.raw_name()
}
}
)*
};
}
define_resource_object! {
pub struct Drive #DriveField {
pub id: Option<DriveId>,
pub created_by: Option<JsonValue>,
pub created_date_time: Option<TimestampString>,
pub description: Option<String>,
pub drive_type: Option<JsonValue>,
pub items: Option<Vec<DriveItem>>,
pub last_modified_by: Option<JsonValue>,
pub last_modified_date_time: Option<TimestampString>,
pub name: Option<String>,
pub owner: Option<JsonValue>,
pub quota: Option<JsonValue>,
pub root: Option<DriveItem>,
pub sharepoint_ids: Option<JsonValue>,
pub special: Option<Vec<DriveItem>>,
pub system: Option<JsonValue>,
pub web_url: Option<Url>,
}
pub struct DriveItem #DriveItemField {
pub audio: Option<JsonValue>,
pub bundle: Option<JsonValue>,
pub content: Option<JsonValue>,
pub c_tag: Option<Tag>,
pub deleted: Option<JsonValue>,
pub description: Option<String>,
pub file: Option<JsonValue>,
pub file_system_info: Option<JsonValue>,
pub folder: Option<JsonValue>,
pub image: Option<JsonValue>,
pub location: Option<JsonValue>,
pub package: Option<JsonValue>,
pub photo: Option<JsonValue>,
pub publication: Option<JsonValue>,
pub remote_item: Option<JsonValue>,
pub root: Option<JsonValue>,
pub search_result: Option<JsonValue>,
pub shared: Option<JsonValue>,
pub sharepoint_ids: Option<JsonValue>,
pub size: Option<i64>,
pub special_folder: Option<JsonValue>,
pub video: Option<JsonValue>,
pub web_dav_url: Option<Url>,
pub children: Option<Vec<DriveItem>>,
pub created_by_user: Option<JsonValue>,
pub last_modified_by_user: Option<JsonValue>,
pub permissions: Option<JsonValue>,
pub thumbnails: Option<JsonValue>,
pub versions: Option<JsonValue>,
pub id: Option<ItemId>,
pub created_by: Option<JsonValue>,
pub created_date_time: Option<TimestampString>,
pub e_tag: Option<Tag>,
pub last_modified_by: Option<JsonValue>,
pub last_modified_date_time: Option<TimestampString>,
pub name: Option<String>,
pub parent_reference: Option<JsonValue>,
pub web_url: Option<Url>,
pub download_url @"@microsoft.graph.downloadUrl": Option<Url>,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[non_exhaustive]
pub struct ErrorResponse {
pub code: String,
pub message: String,
#[serde(rename = "innererror")]
pub inner_error: Option<serde_json::Map<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[allow(missing_docs)]
#[non_exhaustive]
pub struct OAuth2ErrorResponse {
pub error: String,
pub error_description: String,
pub error_codes: Option<Vec<u32>>,
pub timestamp: Option<String>,
pub trace_id: Option<String>,
pub correlation_id: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_raw_name() {
assert_eq!(DriveField::id.raw_name(), "id");
assert_eq!(DriveField::drive_type.raw_name(), "driveType");
assert_eq!(DriveField::owner.raw_name(), "owner");
assert_eq!(DriveField::web_url.raw_name(), "webUrl");
assert_eq!(DriveItemField::id.raw_name(), "id");
assert_eq!(
DriveItemField::file_system_info.raw_name(),
"fileSystemInfo"
);
assert_eq!(DriveItemField::size.raw_name(), "size");
assert_eq!(DriveItemField::web_dav_url.raw_name(), "webDavUrl");
assert_eq!(DriveItemField::web_url.raw_name(), "webUrl");
assert_eq!(
DriveItemField::download_url.raw_name(),
"@microsoft.graph.downloadUrl",
);
}
}