1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Basic types for describing shared objects

use std::fmt::Display;

use serde::{Deserialize, Serialize};

/// The type of a share as defined in the Delta Sharing protocol.
///
/// A share is a logical grouping to share with recipients. A share can be
/// shared with one or multiple recipients. A recipient can access all
/// resources in a share. A share may contain multiple schemas.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd)]
pub struct Share {
    name: String,
    id: Option<String>,
}

impl Share {
    /// Retrieve the name from `self`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::ShareBuilder;
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// assert_eq!(share.name(), "my-share");
    /// ```
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }

    /// Retrieve the id from `self`.
    ///
    /// # Example
    ///  
    /// ```rust
    /// use delta_sharing_server::protocol::securable::ShareBuilder;
    ///
    /// let share = ShareBuilder::new("my-share").id("my-share-id").build();
    /// assert_eq!(share.id(), Some("my-share-id"));
    /// ```
    pub fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }
}

/// Builder for [`Share`].
pub struct ShareBuilder {
    name: String,
    id: Option<String>,
}

impl ShareBuilder {
    /// Create a new `ShareBuilder`.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            id: None,
        }
    }

    /// Set the id of the share.
    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Set the id of the share.
    pub fn set_id<S: Into<String>>(mut self, id: Option<S>) -> Self {
        self.id = id.map(Into::into);
        self
    }

    /// Build the [`Share`].
    pub fn build(self) -> Share {
        Share {
            name: self.name,
            id: self.id,
        }
    }
}

impl Display for Share {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}

/// The type of a schema as defined in the Delta Sharing protocol.
///
/// A schema is a logical grouping of tables. A schema may contain multiple
/// tables. A schema is defined within the context of a [`Share`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Hash)]
pub struct Schema {
    share: Share,
    name: String,
    id: Option<String>,
}

impl Schema {
    /// Returns the name of the share associated with `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// assert_eq!(schema.share_name(), "my-share");
    /// ```
    pub fn share_name(&self) -> &str {
        self.share.name()
    }

    /// Returns the id of the share associated with `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").id("my-share-id").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// assert_eq!(schema.share_id(), Some("my-share-id"));
    /// ```
    pub fn share_id(&self) -> Option<&str> {
        self.share.id()
    }

    /// Returns the name of `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// assert_eq!(schema.name(), "my-schema");
    /// ```
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }

    /// Returns the id of `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").id("my-schema-id").build();
    /// assert_eq!(schema.id(), Some("my-schema-id"));
    /// ```
    pub fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }
}

/// Builder for [`Schema`].
pub struct SchemaBuilder {
    share: Share,
    name: String,
    id: Option<String>,
}

impl SchemaBuilder {
    /// Create a new `SchemaBuilder`.
    pub fn new(share: Share, schema_name: impl Into<String>) -> Self {
        Self {
            share,
            name: schema_name.into(),
            id: None,
        }
    }

    /// Set the id of the schema.
    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Set the id of the schema.
    pub fn set_id<S: Into<String>>(mut self, id: Option<S>) -> Self {
        self.id = id.map(Into::into);
        self
    }

    /// Build the [`Schema`].
    pub fn build(self) -> Schema {
        Schema {
            share: self.share,
            name: self.name,
            id: self.id,
        }
    }
}

impl Display for Schema {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}", self.share_name(), self.name())
    }
}

/// The type of a table as defined in the Delta Sharing protocol.
///
/// A table is a Delta Lake table or a view on top of a Delta Lake table. A
/// table is defined within the context of a [`Schema`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Hash)]
pub struct Table {
    schema: Schema,
    name: String,
    id: Option<String>,
    storage_path: String,
    format: String,
}

impl Table {
    /// Returns the name of the share associated with `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").build();
    /// assert_eq!(table.share_name(), "my-share");
    /// ```
    pub fn share_name(&self) -> &str {
        self.schema.share_name()
    }

    /// Returns the id of the share associated with `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").id("my-share-id").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").build();
    /// assert_eq!(table.share_id(), Some("my-share-id"));
    /// ```
    pub fn share_id(&self) -> Option<&str> {
        self.schema.share_id()
    }

    /// Returns the name of the schema associated with `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").build();
    /// assert_eq!(table.schema_name(), "my-schema");
    /// ```
    pub fn schema_name(&self) -> &str {
        self.schema.name()
    }

    /// Returns the id of the schema associated with `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").id("my-schema-id").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").build();
    /// assert_eq!(table.schema_id(), Some("my-schema-id"));
    /// ```
    pub fn schema_id(&self) -> Option<&str> {
        self.schema.id()
    }

    /// Returns the name of `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").build();
    /// assert_eq!(table.name(), "my-table");
    /// ```
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }

    /// Returns the id of `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").id("my-table-id").build();
    /// assert_eq!(table.id(), Some("my-table-id"));
    /// ```
    pub fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }

    /// Returns the storage path of `self`
    ///
    /// # Example
    ///
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").build();
    /// assert_eq!(table.storage_path(), "my-storage-path");
    /// ```
    pub fn storage_path(&self) -> &str {
        self.storage_path.as_ref()
    }

    /// Returns the format of `self`
    ///
    /// # Example
    ///    
    /// ```rust
    /// use delta_sharing_server::protocol::securable::{ShareBuilder, SchemaBuilder, TableBuilder};
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").format("PARQUET").build();
    /// assert_eq!(table.format(), "PARQUET");
    ///
    /// let share = ShareBuilder::new("my-share").build();
    /// let schema = SchemaBuilder::new(share, "my-schema").build();
    /// let table = TableBuilder::new(schema, "my-table", "my-storage-path").build();
    /// assert_eq!(table.format(), "DELTA");
    /// ```
    pub fn format(&self) -> &str {
        self.format.as_ref()
    }
}

/// Builder for `Table`
pub struct TableBuilder {
    schema: Schema,
    name: String,
    id: Option<String>,
    storage_path: String,
    format: Option<String>,
}

impl TableBuilder {
    /// Creates a new `TableBuilder` with the given `schema`, `table_name` and `storage_path`
    pub fn new(
        schema: Schema,
        table_name: impl Into<String>,
        storage_path: impl Into<String>,
    ) -> Self {
        Self {
            schema,
            name: table_name.into(),
            id: None,
            storage_path: storage_path.into(),
            format: None,
        }
    }

    /// Sets the id of the table
    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Sets the id of the table
    pub fn set_id<S: Into<String>>(mut self, id: Option<S>) -> Self {
        self.id = id.map(Into::into);
        self
    }

    /// Sets the format of the table
    pub fn format<S: Into<String>>(mut self, format: S) -> Self {
        self.format = Some(format.into());
        self
    }

    /// Sets the format of the table
    pub fn set_format<S: Into<String>>(mut self, format: Option<S>) -> Self {
        self.format = format.map(Into::into);
        self
    }

    /// Builds a `Table` from the current `TableBuilder`
    pub fn build(self) -> Table {
        Table {
            schema: self.schema,
            name: self.name,
            id: self.id,
            storage_path: self.storage_path,
            format: self.format.unwrap_or_else(|| "DELTA".to_string()),
        }
    }
}

impl Display for Table {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}.{}.{}",
            self.share_name(),
            self.schema_name(),
            self.name()
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn display_share() {
        let share = ShareBuilder::new("share").id("share_id").build();
        assert_eq!(format!("{}", share), "share");
    }

    #[test]
    fn display_schema() {
        let share = ShareBuilder::new("share").build();
        let schema = SchemaBuilder::new(share, "schema").build();
        assert_eq!(format!("{}", schema), "share.schema");
    }

    #[test]
    fn display_table() {
        let share = ShareBuilder::new("share").build();
        let schema = SchemaBuilder::new(share, "schema").build();
        let table = TableBuilder::new(schema, "table", "storage_path").build();
        assert_eq!(format!("{}", table), "share.schema.table");
    }
}