|
| 1 | +use std::future::Future; |
1 | 2 | use std::sync::Arc;
|
2 | 3 |
|
3 | 4 | use iceberg::{Catalog, CatalogBuilder, Error, ErrorKind, Result};
|
4 | 5 | use iceberg_catalog_rest::RestCatalogBuilder;
|
5 | 6 |
|
6 |
| -pub enum CatalogBuilderDef { |
7 |
| - Rest(RestCatalogBuilder), |
| 7 | +pub trait BoxedCatalogBuilder { |
| 8 | + fn name(&mut self, name: String); |
| 9 | + fn uri(&mut self, uri: String); |
| 10 | + fn warehouse(&mut self, warehouse: String); |
| 11 | + fn with_prop(&mut self, key: String, value: String); |
| 12 | + |
| 13 | + fn build(self: Box<Self>) -> Box<dyn Future<Output = Result<Arc<dyn Catalog>>>>; |
8 | 14 | }
|
9 | 15 |
|
10 |
| -pub fn load(r#type: &str) -> Result<CatalogBuilderDef> { |
11 |
| - match r#type { |
12 |
| - "rest" => Ok(CatalogBuilderDef::Rest(RestCatalogBuilder::default())), |
13 |
| - _ => Err(Error::new( |
14 |
| - ErrorKind::FeatureUnsupported, |
15 |
| - format!("Unsupported catalog type: {}", r#type), |
16 |
| - )), |
| 16 | +impl<T: CatalogBuilder + 'static> BoxedCatalogBuilder for T { |
| 17 | + fn name(&mut self, name: String) { |
| 18 | + self.name(name); |
17 | 19 | }
|
18 |
| -} |
19 | 20 |
|
20 |
| -impl CatalogBuilderDef { |
21 |
| - pub fn name(self, name: impl Into<String>) -> Self { |
22 |
| - match self { |
23 |
| - CatalogBuilderDef::Rest(builder) => CatalogBuilderDef::Rest(builder.name(name)), |
24 |
| - } |
| 21 | + fn uri(&mut self, uri: String) { |
| 22 | + self.uri(uri); |
25 | 23 | }
|
26 | 24 |
|
27 |
| - pub fn uri(self, uri: impl Into<String>) -> Self { |
28 |
| - match self { |
29 |
| - CatalogBuilderDef::Rest(builder) => CatalogBuilderDef::Rest(builder.uri(uri)), |
30 |
| - } |
| 25 | + fn warehouse(&mut self, warehouse: String) { |
| 26 | + self.warehouse(warehouse); |
31 | 27 | }
|
32 | 28 |
|
33 |
| - pub fn warehouse(self, warehouse: impl Into<String>) -> Self { |
34 |
| - match self { |
35 |
| - CatalogBuilderDef::Rest(builder) => { |
36 |
| - CatalogBuilderDef::Rest(builder.warehouse(warehouse)) |
37 |
| - } |
38 |
| - } |
| 29 | + fn with_prop(&mut self, key: String, value: String) { |
| 30 | + self.with_prop(key, value); |
39 | 31 | }
|
40 | 32 |
|
41 |
| - pub fn with_prop(self, key: impl Into<String>, value: impl Into<String>) -> Self { |
42 |
| - match self { |
43 |
| - CatalogBuilderDef::Rest(builder) => { |
44 |
| - CatalogBuilderDef::Rest(builder.with_prop(key, value)) |
45 |
| - } |
46 |
| - } |
| 33 | + fn build(self: Box<Self>) -> Box<dyn Future<Output = Result<Arc<dyn Catalog>>>> { |
| 34 | + let builder = *self; |
| 35 | + Box::new(async move { Ok(Arc::new(builder.build().await.unwrap()) as Arc<dyn Catalog>) }) |
47 | 36 | }
|
| 37 | +} |
48 | 38 |
|
49 |
| - pub async fn build(self) -> Result<Arc<dyn Catalog>> { |
50 |
| - match self { |
51 |
| - CatalogBuilderDef::Rest(builder) => builder |
52 |
| - .build() |
53 |
| - .await |
54 |
| - .map(|c| Arc::new(c) as Arc<dyn Catalog>), |
55 |
| - } |
| 39 | +pub fn load(r#type: &str) -> Result<Box<dyn BoxedCatalogBuilder>> { |
| 40 | + match r#type { |
| 41 | + "rest" => Ok(Box::new(RestCatalogBuilder::default()) as Box<dyn BoxedCatalogBuilder>), |
| 42 | + _ => Err(Error::new( |
| 43 | + ErrorKind::FeatureUnsupported, |
| 44 | + format!("Unsupported catalog type: {}", r#type), |
| 45 | + )), |
56 | 46 | }
|
57 | 47 | }
|
0 commit comments