Skip to main content

opendal_service_yandex_disk/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::YandexDiskBuilder;
24
25/// Config for YandexDisk services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct YandexDiskConfig {
30    /// root of this backend.
31    ///
32    /// All operations will happen under this root.
33    pub root: Option<String>,
34    /// yandex disk oauth access_token.
35    pub access_token: String,
36}
37
38impl Debug for YandexDiskConfig {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.debug_struct("YandexDiskConfig")
41            .field("root", &self.root)
42            .finish_non_exhaustive()
43    }
44}
45
46impl opendal_core::Configurator for YandexDiskConfig {
47    type Builder = YandexDiskBuilder;
48
49    fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
50        let mut map = uri.options().clone();
51
52        if let Some(root) = uri.root()
53            && !root.is_empty()
54        {
55            map.insert("root".to_string(), root.to_string());
56        }
57
58        Self::from_iter(map)
59    }
60
61    fn into_builder(self) -> Self::Builder {
62        YandexDiskBuilder { config: self }
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use opendal_core::Configurator;
70    use opendal_core::OperatorUri;
71
72    #[test]
73    fn from_uri_sets_root_and_preserves_token() {
74        let uri = OperatorUri::new(
75            "yandex-disk://disk/root/path",
76            vec![("access_token".to_string(), "secret".to_string())],
77        )
78        .unwrap();
79
80        let cfg = YandexDiskConfig::from_uri(&uri).unwrap();
81        assert_eq!(cfg.root.as_deref(), Some("root/path"));
82        assert_eq!(cfg.access_token, "secret".to_string());
83    }
84}