1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! # onedrive-api
//!
//! `onedrive-api` crate provides middle-level HTTP APIs [`OneDrive`][one_drive] to the
//! [OneDrive][ms_onedrive] API through [Microsoft Graph][ms_graph], and also [`Auth`][auth]
//! with utilities for OAuth2.
//!
//! ## Example
//! ```
//! use onedrive_api::{OneDrive, FileName, DriveLocation, ItemLocation};
//! use reqwest::Client;
//!
//! # async fn run() -> onedrive_api::Result<()> {
//! let client = Client::new();
//! let drive = OneDrive::new(
//!     "<...TOKEN...>".to_owned(), // Login token to Microsoft Graph.
//!     DriveLocation::me(),
//! );
//!
//! let folder_item = drive
//!     .create_folder(
//!         ItemLocation::root(),
//!         FileName::new("test_folder").unwrap(),
//!     )
//!     .await?;
//!
//! drive
//!     .upload_small(
//!         folder_item.id.as_ref().unwrap(),
//!         &b"Hello, world"[..],
//!     )
//!     .await?;
//!
//! # Ok(())
//! # }
//! ```
//!
//! # Features
//! - `beta`
//!
//!   Most of Microsoft APIs used in this crate are stable.
//!   But there are also some beta APIs, which are subject to change and
//!   is not suggested to be used in production application.
//!   Microsoft references of beta APIs usually contain a `(beta)` suffix in title.
//!
//!   To avoid breakage, we put beta APIs and related resources under feature gate `beta`.
//!   They may change to follow Microsoft API references **ANYTIME**,
//!   and do **NOT** follow the semantic version of this crate.
//!
//!   Be carefully using it and **do NOT use it in production**.
//!
//! [ms_onedrive]: https://products.office.com/en-us/onedrive/online-cloud-storage
//! [ms_graph]: https://docs.microsoft.com/graph/overview
//! [one_drive]: ./struct.OneDrive.html
//! [auth]: ./struct.Auth.html
//! [api]: ./trait.Api.html
//! [api_execute]: ./trait.Api.html#tymethod.execute
//! [client]: ./trait.Client.html
// #![deny(warnings)]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
use serde::{de, Serialize};

mod auth;
mod error;
mod onedrive;
pub mod option;
pub mod resource;
mod util;

pub use self::{
    auth::{Auth, Permission, TokenResponse},
    error::{Error, Result},
    onedrive::{
        CopyProgressMonitor, ListChildrenFetcher, OneDrive, TrackChangeFetcher, UploadSession,
    },
    resource::{DriveId, ItemId, Tag},
    util::{DriveLocation, FileName, ItemLocation},
};

#[cfg(feature = "beta")]
pub use self::onedrive::{CopyProgress, CopyStatus};

/// The conflict resolution behavior for actions that create a new item.
///
/// # See also
/// [Microsoft Docs](https://docs.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#instance-attributes)
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ConflictBehavior {
    /// Make the request fail. Usually cause HTTP 409 CONFLICT.
    Fail,
    /// **DANGER**: Replace the existing item.
    Replace,
    /// Rename the newly created item to another name.
    ///
    /// The new name is not specified and usually can be retrived from the response.
    Rename,
}

/// A half-open byte range `start..end` or `start..`.
#[derive(Debug, PartialEq, Eq)]
pub struct ExpectRange {
    /// The lower bound of the range (inclusive).
    pub start: u64,
    /// The optional upper bound of the range (exclusive).
    pub end: Option<u64>,
}

impl<'de> de::Deserialize<'de> for ExpectRange {
    fn deserialize<D: de::Deserializer<'de>>(
        deserializer: D,
    ) -> std::result::Result<Self, D::Error> {
        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = ExpectRange;

            fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "Expect Range")
            }

            fn visit_str<E: de::Error>(self, v: &str) -> std::result::Result<Self::Value, E> {
                let parse = || -> Option<ExpectRange> {
                    let mut it = v.split('-');
                    let start = it.next()?.parse().ok()?;
                    let end = match it.next()? {
                        "" => None,
                        s => {
                            let end = s.parse::<u64>().ok()?.checked_add(1)?; // Exclusive.
                            if end <= start {
                                return None;
                            }
                            Some(end)
                        }
                    };
                    if it.next().is_some() {
                        return None;
                    }

                    Some(ExpectRange { start, end })
                };
                match parse() {
                    Some(v) => Ok(v),
                    None => Err(E::invalid_value(
                        de::Unexpected::Str(v),
                        &"`{lower}-` or `{lower}-{upper}`",
                    )),
                }
            }
        }

        deserializer.deserialize_str(Visitor)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_range_parsing() {
        let max = format!("0-{}", u64::max_value() - 1);
        let overflow = format!("0-{}", u64::max_value());
        let cases = [
            (
                "42-196",
                Some(ExpectRange {
                    start: 42,
                    end: Some(197),
                }),
            ), // [left, right)
            (
                "418-",
                Some(ExpectRange {
                    start: 418,
                    end: None,
                }),
            ),
            ("", None),
            ("42-4", None),
            ("-9", None),
            ("-", None),
            ("1-2-3", None),
            ("0--2", None),
            ("-1-2", None),
            (
                &max,
                Some(ExpectRange {
                    start: 0,
                    end: Some(u64::max_value()),
                }),
            ),
            (&overflow, None),
        ];

        for &(s, ref expect) in &cases {
            let ret = serde_json::from_str(&serde_json::to_string(s).unwrap());
            assert_eq!(
                ret.as_ref().ok(),
                expect.as_ref(),
                "Failed: Got {:?} on {:?}",
                ret,
                s,
            );
        }
    }
}