Skip to main content

opendal_service_vercel_artifacts/
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::VercelArtifactsBuilder;
24
25/// Config for Vercel Cache support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct VercelArtifactsConfig {
30    /// The access token for Vercel.
31    pub access_token: Option<String>,
32    /// The endpoint for the Vercel artifacts API.
33    ///
34    /// Defaults to `https://api.vercel.com`.
35    pub endpoint: Option<String>,
36    /// The Vercel team ID. When set, the `teamId` query parameter
37    /// is appended to all API requests.
38    pub team_id: Option<String>,
39    /// The Vercel team slug. When set, the `slug` query parameter
40    /// is appended to all API requests.
41    pub team_slug: Option<String>,
42}
43
44impl Debug for VercelArtifactsConfig {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        f.debug_struct("VercelArtifactsConfig")
47            .finish_non_exhaustive()
48    }
49}
50
51impl opendal_core::Configurator for VercelArtifactsConfig {
52    type Builder = VercelArtifactsBuilder;
53
54    fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
55        Self::from_iter(uri.options().clone())
56    }
57
58    fn into_builder(self) -> Self::Builder {
59        VercelArtifactsBuilder { config: self }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use opendal_core::Configurator;
67    use opendal_core::OperatorUri;
68
69    #[test]
70    fn from_uri_loads_access_token() {
71        let uri = OperatorUri::new(
72            "vercel-artifacts://cache",
73            vec![("access_token".to_string(), "token123".to_string())],
74        )
75        .unwrap();
76
77        let cfg = VercelArtifactsConfig::from_uri(&uri).unwrap();
78        assert_eq!(cfg.access_token.as_deref(), Some("token123"));
79    }
80
81    #[test]
82    fn from_uri_loads_all_options() {
83        let uri = OperatorUri::new(
84            "vercel-artifacts://cache",
85            vec![
86                ("access_token".to_string(), "token123".to_string()),
87                (
88                    "endpoint".to_string(),
89                    "https://custom.api.example.com".to_string(),
90                ),
91                ("team_id".to_string(), "team_abc".to_string()),
92                ("team_slug".to_string(), "my-team".to_string()),
93            ],
94        )
95        .unwrap();
96
97        let cfg = VercelArtifactsConfig::from_uri(&uri).unwrap();
98        assert_eq!(cfg.access_token.as_deref(), Some("token123"));
99        assert_eq!(
100            cfg.endpoint.as_deref(),
101            Some("https://custom.api.example.com")
102        );
103        assert_eq!(cfg.team_id.as_deref(), Some("team_abc"));
104        assert_eq!(cfg.team_slug.as_deref(), Some("my-team"));
105    }
106
107    #[test]
108    fn defaults_are_none() {
109        let cfg = VercelArtifactsConfig::default();
110        assert!(cfg.access_token.is_none());
111        assert!(cfg.endpoint.is_none());
112        assert!(cfg.team_id.is_none());
113        assert!(cfg.team_slug.is_none());
114    }
115}