Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use flattened structs for deserializing common values #8

Open
leftmostcat opened this issue May 7, 2024 · 0 comments
Open

Use flattened structs for deserializing common values #8

leftmostcat opened this issue May 7, 2024 · 0 comments

Comments

@leftmostcat
Copy link
Collaborator

Instead of repeating properties which form a common base, we should use structs to encapsulate these and serde(flatten) to flatten them into the containing structure.

An initial attempt at this (definitions below) with the following error:

Deserialize(Custom("invalid type: map, expected a string"))`

/// An attachment to an Exchange item.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/attachments-ex15websvcsotherref>
#[derive(Debug, Deserialize)]
pub enum Attachment {
    /// An attachment containing an Exchange item.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemattachment>
    #[serde(rename_all = "PascalCase")]
    ItemAttachment {
        #[serde(flatten)]
        info: AttachmentCommon,
        // XXX: With this field in place, parsing will fail if there is no
        // `AttachmentItem` in the response.
        // See https://github.com/tafia/quick-xml/issues/683
        // /// The attached item.
        // #[serde(rename = "$value")]
        // content: Option<AttachmentItem>,
    },

    /// An attachment containing a file.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/fileattachment>
    #[serde(rename_all = "PascalCase")]
    FileAttachment {
        #[serde(flatten)]
        info: AttachmentCommon,

        /// Whether the attachment represents a contact photo.
        ///
        /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/iscontactphoto>
        is_contact_photo: Option<bool>,

        /// The base64-encoded content of the attachment.
        ///
        /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/content>
        content: Option<String>,
    },
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct AttachmentCommon {
    /// An identifier for the attachment.
    pub attachment_id: AttachmentId,

    /// The name of the attachment.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/name-attachmenttype>
    pub name: String,

    /// The MIME type of the attachment's content.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/contenttype>
    pub content_type: String,

    /// An arbitrary identifier for the attachment.
    ///
    /// This field is not set by Exchange and is intended for use by
    /// external applications.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/contentid>
    pub content_id: Option<String>,

    /// A URI representing the location of the attachment's content.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/contentlocation>
    pub content_location: Option<String>,

    /// The size of the attachment's content in bytes.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/size>
    pub size: Option<usize>,

    /// The most recent modification time for the attachment.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/lastmodifiedtime>
    pub last_modified_time: Option<DateTime>,

    /// Whether the attachment appears inline in the item body.
    ///
    /// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/isinline>
    pub is_inline: Option<bool>,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant