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
// Copyright (c) 2021 ruarango developers
//
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! [`Input`](crate::coll::input)/[`Output`](crate::coll::output) for [`Collection`](crate::Collection) operations

use serde::{
    de::{self, Deserialize as Deser, Deserializer, Visitor},
    ser::{Serialize as Ser, Serializer},
};
use std::fmt;

pub mod input;
pub mod output;

/// The collection kind
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CollectionKind {
    /// A document collection
    Document,
    /// An edges collection
    Edges,
}

impl Ser for CollectionKind {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            CollectionKind::Document => serializer.serialize_u64(2),
            CollectionKind::Edges => serializer.serialize_u64(3),
        }
    }
}

impl<'de> Deser<'de> for CollectionKind {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_u64(CollectionKindVisitor)
    }
}

struct CollectionKindVisitor;

impl Visitor<'_> for CollectionKindVisitor {
    type Value = CollectionKind;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("u64")
    }

    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        match value {
            2 => Ok(CollectionKind::Document),
            3 => Ok(CollectionKind::Edges),
            _ => Err(E::custom("Invalid collection kind")),
        }
    }
}

/// The collection status
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Status {
    /// unknown - may be corrupted
    Unknown,
    /// unloaded
    Unloaded,
    /// loaded
    Loaded,
    /// unloading
    Unloading,
    /// deleted
    Deleted,
    /// loading
    Loading,
}

impl Ser for Status {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Status::Unknown => serializer.serialize_u64(0),
            Status::Unloaded => serializer.serialize_u64(2),
            Status::Loaded => serializer.serialize_u64(3),
            Status::Unloading => serializer.serialize_u64(4),
            Status::Deleted => serializer.serialize_u64(5),
            Status::Loading => serializer.serialize_u64(6),
        }
    }
}

impl<'de> Deser<'de> for Status {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_u64(StatusVisitor)
    }
}

struct StatusVisitor;

impl Visitor<'_> for StatusVisitor {
    type Value = Status;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("u64")
    }

    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        match value {
            2 => Ok(Status::Unloaded),
            3 => Ok(Status::Loaded),
            4 => Ok(Status::Unloading),
            5 => Ok(Status::Deleted),
            6 => Ok(Status::Loading),
            _ => Ok(Status::Unknown),
        }
    }
}