tarantool_rs/client/schema/
mod.rs

1//! Schema (spaces and indices) helper types.
2
3pub use self::{
4    index::{GenericIndex, Index, IndexMetadata, OwnedIndex},
5    space::{Space, SpaceMetadata},
6};
7
8use std::fmt;
9
10use rmpv::Value;
11use serde::{Deserialize, Serialize};
12
13mod index;
14mod space;
15
16/// First possible id of user space.
17///
18/// For details see [`SystemSpacesId`].
19pub const USER_SPACE_MIN_ID: u32 = 512;
20
21/// Id of the primary index in space.
22pub const PRIMARY_INDEX_ID: u32 = 0;
23
24// TODO: docs on variants
25/// Ids of system spaces and views.
26///
27/// According to Tarantool [sources](https://github.com/tarantool/tarantool/blob/00a9e59927399c91158aa2bf9698c4bfa6e11322/src/box/schema_def.h#L66)
28/// this values are fixed and all have an id in reserved range `[256, 511]`.
29// NOTE: if this values are changed for any reason, replace them with dynamic discovery of spaces and views.
30#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
31#[repr(u32)]
32pub enum SystemSpacesId {
33    VSpace = 281,
34    VIndex = 289,
35}
36
37/// Key of space or index.
38#[derive(Clone, Debug, PartialEq, Eq)]
39pub enum SchemaEntityKey {
40    /// Schema entity symbolic name.
41    Name(String),
42    /// Internal id of entity.
43    Id(u32),
44}
45
46impl fmt::Display for SchemaEntityKey {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            SchemaEntityKey::Name(x) => write!(f, "name '{x}'"),
50            SchemaEntityKey::Id(x) => write!(f, "id '{x}'"),
51        }
52    }
53}
54
55impl From<&str> for SchemaEntityKey {
56    fn from(value: &str) -> Self {
57        Self::Name(value.to_owned())
58    }
59}
60
61impl From<u32> for SchemaEntityKey {
62    fn from(value: u32) -> Self {
63        Self::Id(value)
64    }
65}
66
67impl From<SchemaEntityKey> for Value {
68    fn from(val: SchemaEntityKey) -> Self {
69        match val {
70            SchemaEntityKey::Name(x) => x.into(),
71            SchemaEntityKey::Id(x) => x.into(),
72        }
73    }
74}
75
76impl SchemaEntityKey {
77    pub(crate) fn space_index_id(&self) -> u32 {
78        match self {
79            SchemaEntityKey::Name(_) => 2,
80            SchemaEntityKey::Id(_) => 0,
81        }
82    }
83
84    pub(crate) fn into_value(self) -> Value {
85        self.into()
86    }
87}