opendal/services/ipmfs/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::sync::Arc;
19
20use log::debug;
21
22use super::backend::IpmfsBackend;
23use super::core::IpmfsCore;
24use crate::raw::*;
25use crate::services::IpmfsConfig;
26use crate::*;
27
28impl Configurator for IpmfsConfig {
29 type Builder = IpmfsBuilder;
30
31 #[allow(deprecated)]
32 fn into_builder(self) -> Self::Builder {
33 IpmfsBuilder {
34 config: self,
35 http_client: None,
36 }
37 }
38}
39
40/// IPFS file system support based on [IPFS MFS](https://docs.ipfs.tech/concepts/file-systems/) API.
41///
42/// # Capabilities
43///
44/// This service can be used to:
45///
46/// - [x] read
47/// - [x] write
48/// - [x] list
49/// - [ ] presign
50/// - [ ] blocking
51///
52/// # Configuration
53///
54/// - `root`: Set the work directory for backend
55/// - `endpoint`: Customizable endpoint setting
56///
57/// You can refer to [`IpmfsBuilder`]'s docs for more information
58///
59/// # Example
60///
61/// ## Via Builder
62///
63/// ```no_run
64/// use anyhow::Result;
65/// use opendal::services::Ipmfs;
66/// use opendal::Operator;
67///
68/// #[tokio::main]
69/// async fn main() -> Result<()> {
70/// // create backend builder
71/// let mut builder = Ipmfs::default()
72/// // set the storage bucket for OpenDAL
73/// .endpoint("http://127.0.0.1:5001");
74///
75/// let op: Operator = Operator::new(builder)?.finish();
76///
77/// Ok(())
78/// }
79/// ```
80#[derive(Default, Debug)]
81pub struct IpmfsBuilder {
82 config: IpmfsConfig,
83
84 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
85 http_client: Option<HttpClient>,
86}
87
88impl IpmfsBuilder {
89 /// Set root for ipfs.
90 pub fn root(mut self, root: &str) -> Self {
91 self.config.root = if root.is_empty() {
92 None
93 } else {
94 Some(root.to_string())
95 };
96
97 self
98 }
99
100 /// Set endpoint for ipfs.
101 ///
102 /// Default: http://localhost:5001
103 pub fn endpoint(mut self, endpoint: &str) -> Self {
104 self.config.endpoint = if endpoint.is_empty() {
105 None
106 } else {
107 Some(endpoint.to_string())
108 };
109 self
110 }
111
112 /// Specify the http client that used by this service.
113 ///
114 /// # Notes
115 ///
116 /// This API is part of OpenDAL's Raw API. `HttpClient` could be changed
117 /// during minor updates.
118 #[deprecated(since = "0.53.0", note = "Use `Operator::update_http_client` instead")]
119 #[allow(deprecated)]
120 pub fn http_client(mut self, client: HttpClient) -> Self {
121 self.http_client = Some(client);
122 self
123 }
124}
125
126impl Builder for IpmfsBuilder {
127 const SCHEME: Scheme = Scheme::Ipmfs;
128 type Config = IpmfsConfig;
129
130 fn build(self) -> Result<impl Access> {
131 let root = normalize_root(&self.config.root.unwrap_or_default());
132 debug!("backend use root {}", root);
133
134 let endpoint = self
135 .config
136 .endpoint
137 .clone()
138 .unwrap_or_else(|| "http://localhost:5001".to_string());
139
140 let info = AccessorInfo::default();
141 info.set_scheme(Scheme::Ipmfs)
142 .set_root(&root)
143 .set_native_capability(Capability {
144 stat: true,
145 stat_has_content_length: true,
146
147 read: true,
148
149 write: true,
150 delete: true,
151
152 list: true,
153 list_has_content_length: true,
154
155 shared: true,
156
157 ..Default::default()
158 });
159
160 let accessor_info = Arc::new(info);
161 let core = Arc::new(IpmfsCore {
162 info: accessor_info,
163 root: root.to_string(),
164 endpoint: endpoint.to_string(),
165 });
166
167 Ok(IpmfsBackend { core })
168 }
169}