Skip to content

Commit 6a65168

Browse files
committed
refactor(services/onedrive): Add OnedriveConfig to implement ConfigDeserializer
1 parent 65973eb commit 6a65168

3 files changed

Lines changed: 42 additions & 13 deletions

File tree

core/src/services/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ mod webhdfs;
216216
mod onedrive;
217217
#[cfg(feature = "services-onedrive")]
218218
pub use onedrive::Onedrive;
219+
#[cfg(feature = "services-onedrive")]
220+
pub use onedrive::OnedriveConfig;
219221

220222
#[cfg(feature = "services-gdrive")]
221223
mod gdrive;

core/src/services/onedrive/builder.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,47 @@ use std::fmt::Debug;
2020
use std::fmt::Formatter;
2121

2222
use log::debug;
23+
use serde::Deserialize;
2324

2425
use super::backend::OnedriveBackend;
2526
use crate::raw::normalize_root;
27+
use crate::raw::ConfigDeserializer;
2628
use crate::raw::HttpClient;
2729
use crate::Scheme;
2830
use crate::*;
2931

32+
/// Config for [OneDrive](https://onedrive.com) backend support.
33+
#[derive(Default, Deserialize)]
34+
#[serde(default)]
35+
#[non_exhaustive]
36+
pub struct OnedriveConfig {
37+
/// bearer access token for OneDrive
38+
pub access_token: Option<String>,
39+
/// root path of OneDrive folder.
40+
pub root: Option<String>,
41+
}
42+
43+
impl Debug for OnedriveConfig {
44+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45+
f.debug_struct("OnedriveConfig")
46+
.field("root", &self.root)
47+
.finish_non_exhaustive()
48+
}
49+
}
50+
3051
/// [OneDrive](https://onedrive.com) backend support.
3152
#[doc = include_str!("docs.md")]
3253
#[derive(Default)]
3354
pub struct OnedriveBuilder {
34-
access_token: Option<String>,
35-
root: Option<String>,
55+
config: OnedriveConfig,
3656
http_client: Option<HttpClient>,
3757
}
3858

3959
impl Debug for OnedriveBuilder {
4060
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41-
f.debug_struct("Backend").field("root", &self.root).finish()
61+
f.debug_struct("Backend")
62+
.field("root", &self.config.root)
63+
.finish()
4264
}
4365
}
4466

@@ -47,13 +69,13 @@ impl OnedriveBuilder {
4769
///
4870
/// default: no access token, which leads to failure
4971
pub fn access_token(&mut self, access_token: &str) -> &mut Self {
50-
self.access_token = Some(access_token.to_string());
72+
self.config.access_token = Some(access_token.to_string());
5173
self
5274
}
5375

5476
/// Set root path of OneDrive folder.
5577
pub fn root(&mut self, root: &str) -> &mut Self {
56-
self.root = Some(root.to_string());
78+
self.config.root = Some(root.to_string());
5779
self
5880
}
5981

@@ -75,16 +97,19 @@ impl Builder for OnedriveBuilder {
7597
type Accessor = OnedriveBackend;
7698

7799
fn from_map(map: HashMap<String, String>) -> Self {
78-
let mut builder = Self::default();
79-
80-
map.get("root").map(|v| builder.root(v));
81-
map.get("access_token").map(|v| builder.access_token(v));
82-
83-
builder
100+
// Deserialize the configuration from the HashMap.
101+
let config = OnedriveConfig::deserialize(ConfigDeserializer::new(map))
102+
.expect("config deserialize must succeed");
103+
104+
// Create an OnedriveBuilder instance with the deserialized config.
105+
OnedriveBuilder {
106+
config,
107+
http_client: None,
108+
}
84109
}
85110

86111
fn build(&mut self) -> Result<Self::Accessor> {
87-
let root = normalize_root(&self.root.take().unwrap_or_default());
112+
let root = normalize_root(&self.config.root.take().unwrap_or_default());
88113
debug!("backend use root {}", root);
89114

90115
let client = if let Some(client) = self.http_client.take() {
@@ -96,7 +121,7 @@ impl Builder for OnedriveBuilder {
96121
})?
97122
};
98123

99-
match self.access_token.clone() {
124+
match self.config.access_token.clone() {
100125
Some(access_token) => Ok(OnedriveBackend::new(root, access_token, client)),
101126
None => Err(Error::new(ErrorKind::ConfigInvalid, "access_token not set")),
102127
}

core/src/services/onedrive/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ mod error;
2121
mod graph_model;
2222

2323
pub use builder::OnedriveBuilder as Onedrive;
24+
pub use builder::OnedriveConfig;
25+
2426
mod lister;
2527
mod writer;

0 commit comments

Comments
 (0)