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