Skip to content

Commit 2d494d5

Browse files
committed
Keep both
1 parent a569f7d commit 2d494d5

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

crates/catalog/loader/src/lib.rs

+28-38
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,47 @@
1+
use std::future::Future;
12
use std::sync::Arc;
23

34
use iceberg::{Catalog, CatalogBuilder, Error, ErrorKind, Result};
45
use iceberg_catalog_rest::RestCatalogBuilder;
56

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>>>>;
814
}
915

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);
1719
}
18-
}
1920

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);
2523
}
2624

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);
3127
}
3228

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);
3931
}
4032

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>) })
4736
}
37+
}
4838

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+
)),
5646
}
5747
}

crates/catalog/rest/src/catalog.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -245,22 +245,22 @@ impl Default for RestCatalogBuilder {
245245
impl CatalogBuilder for RestCatalogBuilder {
246246
type C = RestCatalog;
247247

248-
fn name(mut self, name: impl Into<String>) -> Self {
248+
fn name(&mut self, name: impl Into<String>) -> &mut Self {
249249
self.0.name = Some(name.into());
250250
self
251251
}
252252

253-
fn uri(mut self, uri: impl Into<String>) -> Self {
253+
fn uri(&mut self, uri: impl Into<String>) -> &mut Self {
254254
self.0.uri = uri.into();
255255
self
256256
}
257257

258-
fn warehouse(mut self, warehouse: impl Into<String>) -> Self {
258+
fn warehouse(&mut self, warehouse: impl Into<String>) -> &mut Self {
259259
self.0.warehouse = Some(warehouse.into());
260260
self
261261
}
262262

263-
fn with_prop(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
263+
fn with_prop(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
264264
self.0.props.insert(key.into(), value.into());
265265
self
266266
}
@@ -288,7 +288,7 @@ impl CatalogBuilder for RestCatalogBuilder {
288288

289289
impl RestCatalogBuilder {
290290
/// Configures the catalog with a custom HTTP client.
291-
pub fn with_client(mut self, client: Client) -> Self {
291+
pub fn with_client(&mut self, client: Client) -> &mut Self {
292292
self.0.client = Some(client);
293293
self
294294
}
@@ -2331,13 +2331,13 @@ mod tests {
23312331

23322332
#[tokio::test]
23332333
async fn test_create_rest_catalog() {
2334-
let catalog = RestCatalogBuilder::default()
2334+
let mut builder = RestCatalogBuilder::default();
2335+
builder
23352336
.name("test")
23362337
.uri("http://localhost:8080")
23372338
.with_client(Client::new())
2338-
.with_prop("a", "b")
2339-
.build()
2340-
.await;
2339+
.with_prop("a", "b");
2340+
let catalog = builder.build().await;
23412341

23422342
assert!(catalog.is_ok());
23432343
}

crates/iceberg/src/catalog/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ pub trait Catalog: Debug + Sync + Send {
101101
pub trait CatalogBuilder: Default + Debug + Send + Sync {
102102
/// The catalog type that this builder creates.
103103
type C: Catalog;
104-
/// Configure name of the catalog.
105-
fn name(self, name: impl Into<String>) -> Self;
104+
/// Configure the name of the catalog.
105+
fn name(&mut self, name: impl Into<String>) -> &mut Self;
106106
/// Configure uri of the catalog.
107-
fn uri(self, uri: impl Into<String>) -> Self;
108-
/// Configure warehouse location of the catalog.
109-
fn warehouse(self, warehouse: impl Into<String>) -> Self;
107+
fn uri(&mut self, uri: impl Into<String>) -> &mut Self;
108+
/// Configure the warehouse location of the catalog.
109+
fn warehouse(&mut self, warehouse: impl Into<String>) -> &mut Self;
110110
/// Configure properties of the catalog.
111-
fn with_prop(self, key: impl Into<String>, value: impl Into<String>) -> Self;
111+
fn with_prop(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self;
112112
/// Create the catalog
113113
fn build(self) -> impl Future<Output = Result<Self::C>>;
114114
}

0 commit comments

Comments
 (0)