opendal/types/builder.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::de::DeserializeOwned;
21use serde::Serialize;
22
23use crate::raw::*;
24use crate::*;
25
26/// Builder is used to set up underlying services.
27///
28/// This trait allows the developer to define a builder struct that can:
29///
30/// - build a service via builder style API.
31/// - configure in-memory options like `http_client` or `customized_credential_load`.
32///
33/// Usually, users don't need to use or import this trait directly, they can use `Operator` API instead.
34///
35/// For example:
36///
37/// ```
38/// # use anyhow::Result;
39/// use opendal::services::Fs;
40/// use opendal::Operator;
41/// async fn test() -> Result<()> {
42/// // Create fs backend builder.
43/// let mut builder = Fs::default().root("/tmp");
44///
45/// // Build an `Operator` to start operating the storage.
46/// let op: Operator = Operator::new(builder)?.finish();
47///
48/// Ok(())
49/// }
50/// ```
51pub trait Builder: Default + 'static {
52 /// Associated configuration for this builder.
53 type Config: Configurator;
54
55 /// Consume the accessor builder to build a service.
56 fn build(self) -> Result<impl Access>;
57}
58
59/// Dummy implementation of builder
60impl Builder for () {
61 type Config = ();
62
63 fn build(self) -> Result<impl Access> {
64 Ok(())
65 }
66}
67
68/// Configurator is used to configure the underlying service.
69///
70/// This trait allows the developer to define a configuration struct that can:
71///
72/// - deserialize from an iterator like hashmap or vector.
73/// - convert into a service builder and finally build the underlying services.
74///
75/// Usually, users don't need to use or import this trait directly, they can use `Operator` API instead.
76///
77/// For example:
78///
79/// ```
80/// # use anyhow::Result;
81/// use std::collections::HashMap;
82///
83/// use opendal::services::MemoryConfig;
84/// use opendal::Operator;
85/// async fn test() -> Result<()> {
86/// let mut cfg = MemoryConfig::default();
87/// cfg.root = Some("/".to_string());
88///
89/// // Build an `Operator` to start operating the storage.
90/// let op: Operator = Operator::from_config(cfg)?.finish();
91///
92/// Ok(())
93/// }
94/// ```
95///
96/// Some service builder might contain in memory options like `http_client` . Users can call
97/// `into_builder` to convert the configuration into a builder instead.
98///
99/// ```
100/// # use anyhow::Result;
101/// use std::collections::HashMap;
102///
103/// use opendal::raw::HttpClient;
104/// use opendal::services::S3Config;
105/// use opendal::Configurator;
106/// use opendal::Operator;
107///
108/// async fn test() -> Result<()> {
109/// let mut cfg = S3Config::default();
110/// cfg.root = Some("/".to_string());
111/// cfg.bucket = "test".to_string();
112///
113/// let builder = cfg.into_builder();
114/// let builder = builder.http_client(HttpClient::new()?);
115///
116/// // Build an `Operator` to start operating the storage.
117/// let op: Operator = Operator::new(builder)?.finish();
118///
119/// Ok(())
120/// }
121/// ```
122pub trait Configurator: Serialize + DeserializeOwned + Debug + 'static {
123 /// Associated builder for this configuration.
124 type Builder: Builder;
125
126 /// Deserialize from an iterator.
127 ///
128 /// This API is provided by opendal, developer should not implement it.
129 fn from_iter(iter: impl IntoIterator<Item = (String, String)>) -> Result<Self> {
130 let cfg = ConfigDeserializer::new(iter.into_iter().collect());
131
132 Self::deserialize(cfg).map_err(|err| {
133 Error::new(ErrorKind::ConfigInvalid, "failed to deserialize config").set_source(err)
134 })
135 }
136
137 /// Convert this configuration into a service builder.
138 fn into_builder(self) -> Self::Builder;
139}
140
141impl Configurator for () {
142 type Builder = ();
143
144 fn into_builder(self) -> Self::Builder {}
145}