1
use std::collections::HashMap;
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
use crate::key::KeyDescription;
6
use crate::schema::view::ViewUpdatePolicy;
7
use crate::schema::{CollectionName, SchemaName, Schematic, ViewName};
8

            
9
/// A summary of a [`Schema`](crate::schema::Schema)/[`Schematic`].
10
///
11
/// This type is a serializable summary of a [`Schematic`] and is the result of
12
/// [`StorageConnection::list_available_schemas`](crate::connection::StorageConnection::list_available_schemas)/[`AsyncStorageConnection::list_available_schemas`](crate::connection::AsyncStorageConnection::list_available_schemas).
13
/// It can be used to query information stored in BonsaiDb without needing
14
/// access to the Rust types.
15
765
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
16
pub struct SchemaSummary {
17
    /// The name of the [`Schema`](crate::schema::Schema) this summary is of.
18
    pub name: SchemaName,
19
    collections: HashMap<CollectionName, CollectionSummary>,
20
}
21

            
22
impl SchemaSummary {
23
    /// Returns the summary of named collection, if the schema contains it.
24
    #[must_use]
25
320
    pub fn collection(&self, name: &CollectionName) -> Option<&CollectionSummary> {
26
320
        self.collections.get(name)
27
320
    }
28

            
29
    /// Returns an iterator over all collections contained in this schema.
30
320
    pub fn collections(&self) -> impl Iterator<Item = &CollectionSummary> {
31
320
        self.collections.values()
32
320
    }
33
}
34

            
35
impl<'a> From<&'a Schematic> for SchemaSummary {
36
800
    fn from(schematic: &'a Schematic) -> Self {
37
800
        let mut summary = Self {
38
800
            name: schematic.name.clone(),
39
800
            collections: HashMap::new(),
40
800
        };
41

            
42
2880
        for collection_name in schematic.collections() {
43
2880
            let collection = summary
44
2880
                .collections
45
2880
                .entry(collection_name.clone())
46
2880
                .or_insert_with(|| CollectionSummary {
47
2880
                    name: collection_name.clone(),
48
2880
                    primary_key: schematic
49
2880
                        .collection_primary_key_description(collection_name)
50
2880
                        .expect("invalid schematic")
51
2880
                        .clone(),
52
2880
                    views: HashMap::new(),
53
2880
                });
54
4960
            for view in schematic.views_in_collection(collection_name) {
55
4960
                let name = view.view_name();
56
4960
                collection.views.insert(
57
4960
                    name.clone(),
58
4960
                    ViewSummary {
59
4960
                        name,
60
4960
                        key: view.key_description(),
61
4960
                        policy: view.update_policy(),
62
4960
                        version: view.version(),
63
4960
                    },
64
4960
                );
65
4960
            }
66
        }
67

            
68
800
        summary
69
800
    }
70
}
71

            
72
/// A summary of a [`Collection`](crate::schema::Collection).
73
3570
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
74
pub struct CollectionSummary {
75
    /// The name of the [`Collection`](crate::schema::Collection) this is a summary of.
76
    pub name: CollectionName,
77
    /// The description of [`Collection::PrimaryKey`](crate::schema::Collection::PrimaryKey).
78
    pub primary_key: KeyDescription,
79
    views: HashMap<ViewName, ViewSummary>,
80
}
81

            
82
impl CollectionSummary {
83
    /// Returns the summary of the named view, if it is contained in this collection.
84
    #[must_use]
85
320
    pub fn view(&self, name: &ViewName) -> Option<&ViewSummary> {
86
320
        self.views.get(name)
87
320
    }
88

            
89
    /// Returns an iterator over all summaries of views in this collection.
90
320
    pub fn views(&self) -> impl Iterator<Item = &ViewSummary> {
91
320
        self.views.values()
92
320
    }
93
}
94

            
95
/// A summary of a [`ViewSchema`](crate::schema::ViewSchema).
96
7803
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)]
97
pub struct ViewSummary {
98
    /// The name of the [`ViewSchema`](crate::schema::ViewSchema) this is a
99
    /// summary of.
100
    pub name: ViewName,
101
    /// The description of [`View::Key`](crate::schema::View::Key).
102
    pub key: KeyDescription,
103
    /// The result of
104
    /// [`ViewSchema::update_policy()`](crate::schema::ViewSchema::update_policy)
105
    /// for this view.
106
    pub policy: ViewUpdatePolicy,
107
    /// The result of
108
    /// [`ViewSchema::version()`](crate::schema::ViewSchema::version) for this
109
    /// view.
110
    pub version: u64,
111
}