opendal_core/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_core::services::Ipmfs;
56/// use opendal_core::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
75impl Debug for IpmfsBuilder {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 f.debug_struct("IpmfsBuilder")
78 .field("config", &self.config)
79 .finish_non_exhaustive()
80 }
81}
82
83impl IpmfsBuilder {
84 /// Set root for ipfs.
85 pub fn root(mut self, root: &str) -> Self {
86 self.config.root = if root.is_empty() {
87 None
88 } else {
89 Some(root.to_string())
90 };
91
92 self
93 }
94
95 /// Set endpoint for ipfs.
96 ///
97 /// Default: http://localhost:5001
98 pub fn endpoint(mut self, endpoint: &str) -> Self {
99 self.config.endpoint = if endpoint.is_empty() {
100 None
101 } else {
102 Some(endpoint.to_string())
103 };
104 self
105 }
106}
107
108impl Builder for IpmfsBuilder {
109 type Config = IpmfsConfig;
110
111 fn build(self) -> Result<impl Access> {
112 let root = normalize_root(&self.config.root.unwrap_or_default());
113 debug!("backend use root {root}");
114
115 let endpoint = self
116 .config
117 .endpoint
118 .clone()
119 .unwrap_or_else(|| "http://localhost:5001".to_string());
120
121 let info = AccessorInfo::default();
122 info.set_scheme(IPMFS_SCHEME)
123 .set_root(&root)
124 .set_native_capability(Capability {
125 stat: true,
126
127 read: true,
128
129 write: true,
130 delete: true,
131
132 list: true,
133
134 shared: true,
135
136 ..Default::default()
137 });
138
139 let accessor_info = Arc::new(info);
140 let core = Arc::new(IpmfsCore {
141 info: accessor_info,
142 root: root.to_string(),
143 endpoint: endpoint.to_string(),
144 });
145
146 Ok(IpmfsBackend { core })
147 }
148}