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

Created UpdateItem and added necessary trait implementations #18

Merged
merged 13 commits into from
Oct 31, 2024
Merged
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pub mod get_folder;
pub mod get_item;
pub mod sync_folder_hierarchy;
pub mod sync_folder_items;
pub mod update_item;
16 changes: 14 additions & 2 deletions src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ pub enum DistinguishedPropertySet {
UnifiedMessaging,
}

/// The action an Exchange server will take upon creating a `Message` item.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The action an Exchange server will take upon creating a `Message` item.
/// The action an Exchange server will take upon creating or updating a `Message` item.

///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem#messagedisposition-attribute>
/// and <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitem#messagedisposition-attribute>
#[derive(Clone, Copy, Debug, XmlSerialize)]
#[xml_struct(text)]
pub enum MessageDisposition {
SaveOnly,
SendOnly,
SendAndSaveCopy,
}

/// The type of the value of a MAPI property.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/extendedfielduri#propertytype-attribute>
Expand Down Expand Up @@ -315,7 +327,7 @@ pub enum BaseItemId {
/// The unique identifier of an item.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemid>
#[derive(Clone, Debug, Deserialize, XmlSerialize, PartialEq)]
#[derive(Clone, Default, Debug, Deserialize, XmlSerialize, PartialEq)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: Default doesn't make sense here. See next comment.

pub struct ItemId {
#[xml_struct(attribute)]
#[serde(rename = "@Id")]
Expand Down Expand Up @@ -458,7 +470,7 @@ impl XmlSerialize for DateTime {
/// An email message.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/message-ex15websvcsotherref>
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug, Default, Deserialize, XmlSerialize)]
leftmostcat marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename_all = "PascalCase")]
pub struct Message {
/// The MIME content of the item.
Expand Down
14 changes: 2 additions & 12 deletions src/types/create_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ use xml_struct::XmlSerialize;

use crate::{
types::sealed::EnvelopeBodyContents, ArrayOfRecipients, BaseFolderId, ExtendedFieldURI, Items,
MimeContent, Operation, OperationResponse, ResponseClass, ResponseCode, MESSAGES_NS_URI,
MessageDisposition, MimeContent, Operation, OperationResponse, ResponseClass, ResponseCode,
MESSAGES_NS_URI,
};

/// The action an Exchange server will take upon creating a `Message` item.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem#messagedisposition-attribute>
#[derive(Clone, Debug, XmlSerialize)]
#[xml_struct(text)]
pub enum MessageDisposition {
SaveOnly,
SendOnly,
SendAndSaveCopy,
}

/// A request to create (and optionally send) one or more Exchange item(s).
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/createitem>
Expand Down
142 changes: 142 additions & 0 deletions src/types/update_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::types::common::{BaseItemId, Message, MessageDisposition, PathToElement};
use crate::{
types::sealed::EnvelopeBodyContents, Items, Operation, OperationResponse, ResponseClass,
ResponseCode,
};
use serde::Deserialize;
use xml_struct::XmlSerialize;

/// The method used by the Exchange server to resolve conflicts between item
/// updates.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitem#conflictresolution-attribute>
#[derive(Clone, Copy, Debug, Default, XmlSerialize)]
#[xml_struct(text)]
pub enum ConflictResolution {
NeverOverwrite,

#[default]
AutoResolve,

AlwaysOverwrite,
}

/// Represents a change to an individual item, including the item ID and updates.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: This and the other struct comments throughout the file should follow the noun phrase convention.

///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemchange>
#[derive(Clone, Debug, XmlSerialize)]
pub struct ItemChange {
pub item_id: BaseItemId, // Represents the <t:ItemId> element with Id and ChangeKey.

pub updates: Updates, // Represents the <t:Updates> element containing the changes.
tobypilling marked this conversation as resolved.
Show resolved Hide resolved
}

/// Represents a list of item changes without an explicit container tag.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemchanges>
#[derive(Clone, Debug, XmlSerialize)]
pub struct ItemChanges {
pub item_changes: Vec<ItemChange>,
}

/// Struct representing the field update operation.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: As below, "Struct representing" doesn't add much here, and the second paragraph could easily be folded into this one to give a more complete description, but as below, this needs additional revision.

///
/// This struct contains details of the field that needs to be updated.
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/setitemfield>
#[derive(Clone, Debug, XmlSerialize)]
pub struct SetItemField {
pub field_uri: PathToElement, // Reference to the field being updated.
pub message: Message, // The new value for the specified field.
}

/// Struct representing updates to be applied to an item.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: "Struct representing" is implicit in the declaration already and doesn't really add anything here.

Suggested change
/// Struct representing updates to be applied to an item.
/// A list of changes to fields, with each element representing a single change.

///
/// This struct is used to create an UpdateItem request.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: This doesn't really add anything not implied by the type hierarchy.

/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updates-item>
#[derive(Clone, Debug, XmlSerialize)]
pub struct Updates {
pub set_item_field: SetItemField,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: A couple problems here. First is that this only allows for a single field to be changed per update and it should instead be a Vec. Second is that it doesn't allow for append/delete operations. That should be done by using an enum.

}

/// Represents the UpdateItem operation for interacting with the EWS server.
#[derive(Clone, Debug, XmlSerialize)]
pub struct UpdateItem {
/// Describes how the item will be handled after it is updated.
/// The MessageDisposition attribute is required for message items, including meeting
/// messages such as meeting cancellations, meeting requests, and meeting responses.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitem#messagedisposition-attribute>
#[xml_struct(attribute)]
pub message_disposition: Option<MessageDisposition>,

/// Identifies the type of conflict resolution to try during an update.
/// The default value is AutoResolve.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitem#conflictresolution-attribute>
#[xml_struct(attribute)]
pub conflict_resolution: Option<ConflictResolution>,

/// Contains an array of ItemChange elements that identify items and
/// the updates to apply to the items.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemchanges>
pub item_changes: ItemChanges, // Represents the list of item changes to be included in the request.
}

impl UpdateItem {
/// Adds another `ItemChange` to the `UpdateItem` request.
pub fn add_item_change(&mut self, item_change: ItemChange) {
self.item_changes.item_changes.push(item_change);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: There's still not really cause to have this be a separate function. The field to be changed is public, so while this provides a slight shortcut to accessing that field, it also provides limited means of interacting with it. In particular, since we generally create these operation structs all at once, push() isn't very useful as a way of building a list of changes. Instead, we should generally be preferring using Rust iterators and collect().


impl Operation for UpdateItem {
type Response = UpdateItemResponse;
}

impl EnvelopeBodyContents for UpdateItem {
fn name() -> &'static str {
"UpdateItem"
}
}

/// A response to an [`UpdateItem`] request.
///
/// See <https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/updateitemresponse>
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct UpdateItemResponse {
pub response_messages: ResponseMessages,
}

impl OperationResponse for UpdateItemResponse {}

impl EnvelopeBodyContents for UpdateItemResponse {
fn name() -> &'static str {
"UpdateItemResponse"
}
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ResponseMessages {
pub update_item_response_message: Vec<UpdateItemResponseMessage>,
}

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct UpdateItemResponseMessage {
/// The status of the corresponding request, i.e. whether it succeeded or
/// resulted in an error.
pub response_class: ResponseClass,

pub response_code: Option<ResponseCode>,

pub message_text: Option<String>,

pub items: Items,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I have generally kept the actual definition of the operations at the beginning of the file with operation-specific types declared below. I don't have solid reasoning for this, but it's consistent and puts the named part of the file up front.

Even nitpickier but reflected in that tendency is that, in Rust, I tend to prefer to declare a type and then declare additional types on which it depends below them, e.g.:

struct Foo {
    bar: Bar,
}

struct Bar {}

Loading