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