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
use super::{document, find};
use document::DocumentId;
use find::SortSpec;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Index fields abstraction
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub struct IndexFields {
    pub fields: Vec<SortSpec>,
}

impl IndexFields {
    #[must_use]
    pub fn new(fields: Vec<SortSpec>) -> IndexFields {
        IndexFields { fields }
    }
}

/// Index abstraction
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub struct Index {
    pub ddoc: Option<DocumentId>,
    pub name: String,
    #[serde(rename = "type")]
    pub index_type: Option<IndexType>,
    pub def: IndexFields,
}

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub enum IndexType {
    #[serde(rename = "json")]
    Json,
    #[serde(rename = "text")]
    Text,
    #[serde(rename = "special")]
    Special, // reserved for primary index
}

impl fmt::Display for IndexType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IndexType::Json => write!(f, "json"),
            IndexType::Text => write!(f, "text"),
            IndexType::Special => write!(f, "special"),
        }
    }
}

/// Database index list abstraction
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct DatabaseIndexList {
    pub total_rows: u32,
    pub indexes: Vec<Index>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DeleteIndexResponse {
    pub ok: bool,
}