@@ -20,25 +20,47 @@ use std::fmt::Debug;
2020use std:: fmt:: Formatter ;
2121
2222use log:: debug;
23+ use serde:: Deserialize ;
2324
2425use super :: backend:: OnedriveBackend ;
2526use crate :: raw:: normalize_root;
27+ use crate :: raw:: ConfigDeserializer ;
2628use crate :: raw:: HttpClient ;
2729use crate :: Scheme ;
2830use 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 ) ]
3354pub struct OnedriveBuilder {
34- access_token : Option < String > ,
35- root : Option < String > ,
55+ config : OnedriveConfig ,
3656 http_client : Option < HttpClient > ,
3757}
3858
3959impl 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 }
0 commit comments