use std::mem;
use std::i32;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
const COLLECTION_TYPE_DOCUMENTS: i32 = 2;
const COLLECTION_TYPE_EDGES: i32 = 3;
const COLLECTION_STATUS_NEW_BORN: i32 = 1;
const COLLECTION_STATUS_UNLOADED: i32 = 2;
const COLLECTION_STATUS_LOADED: i32 = 3;
const COLLECTION_STATUS_BEING_UNLOADED: i32 = 4;
const COLLECTION_STATUS_DELETED: i32 = 5;
const COLLECTION_STATUS_BEING_LOADED: i32 = 6;
const COLLECTION_STATUS_CORRUPTED: i32 = i32::MAX;
const KEY_GENERATOR_TYPE_TRADITIONAL: &str = "traditional";
const KEY_GENERATOR_TYPE_AUTO_INCREMENT: &str = "autoincrement";
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Collection {
id: String,
name: String,
#[serde(rename = "type")]
kind: CollectionType,
status: CollectionStatus,
is_system: bool,
}
impl Collection {
pub fn id(&self) -> &str {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> CollectionType {
self.kind
}
pub fn status(&self) -> CollectionStatus {
self.status
}
pub fn is_system(&self) -> bool {
self.is_system
}
pub fn unwrap_id(self) -> String {
self.id
}
pub fn unwrap_name(self) -> String {
self.name
}
}
impl From<CollectionRevision> for Collection {
fn from(properties: CollectionRevision) -> Self {
Collection {
id: properties.id,
name: properties.name,
kind: properties.kind,
status: properties.status,
is_system: properties.is_system,
}
}
}
impl From<BasicCollectionProperties> for Collection {
fn from(basic_properties: BasicCollectionProperties) -> Self {
Collection {
id: basic_properties.id,
name: basic_properties.name,
kind: basic_properties.kind,
status: basic_properties.status,
is_system: basic_properties.is_system,
}
}
}
impl From<CollectionProperties> for Collection {
fn from(properties: CollectionProperties) -> Self {
Collection {
id: properties.id,
name: properties.name,
kind: properties.kind,
status: properties.status,
is_system: properties.is_system,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NewCollection {
name: String,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
kind: Option<CollectionType>,
#[serde(skip_serializing_if = "Option::is_none")]
is_system: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
key_options: Option<NewKeyOptions>,
#[serde(skip_serializing_if = "Option::is_none")]
wait_for_sync: Option<bool>,
#[cfg(feature = "cluster")]
#[serde(skip_serializing_if = "Option::is_none")]
number_of_shards: Option<u16>,
#[cfg(feature = "cluster")]
#[serde(skip_serializing_if = "Option::is_none")]
shard_keys: Option<String>,
#[cfg(feature = "cluster")]
#[serde(skip_serializing_if = "Option::is_none")]
replication_factor: Option<u16>,
#[cfg(feature = "mmfiles")]
#[serde(skip_serializing_if = "Option::is_none")]
is_volatile: Option<bool>,
#[cfg(feature = "mmfiles")]
#[serde(skip_serializing_if = "Option::is_none")]
do_compact: Option<bool>,
#[cfg(feature = "mmfiles")]
#[serde(skip_serializing_if = "Option::is_none")]
index_buckets: Option<u16>,
#[cfg(feature = "mmfiles")]
#[serde(skip_serializing_if = "Option::is_none")]
journal_size: Option<u64>,
}
impl NewCollection {
fn new<N, K, S>(name: N, kind: K, is_system: S) -> Self
where N: Into<String>, K: Into<Option<CollectionType>>, S: Into<Option<bool>>
{
NewCollection {
name: name.into(),
kind: kind.into(),
is_system: is_system.into(),
key_options: None,
wait_for_sync: None,
#[cfg(feature = "cluster")]
number_of_shards: None,
#[cfg(feature = "cluster")]
shard_keys: None,
#[cfg(feature = "cluster")]
replication_factor: None,
#[cfg(feature = "mmfiles")]
is_volatile: None,
#[cfg(feature = "mmfiles")]
do_compact: None,
#[cfg(feature = "mmfiles")]
index_buckets: None,
#[cfg(feature = "mmfiles")]
journal_size: None,
}
}
pub fn with_name<N>(name: N) -> Self
where N: Into<String>
{
NewCollection::new(name.into(), None, None)
}
pub fn documents_with_name<N>(name: N) -> Self
where N: Into<String>
{
NewCollection::new(name.into(), Some(CollectionType::Documents), None)
}
pub fn edges_with_name<N>(name: N) -> Self
where N: Into<String>
{
NewCollection::new(name.into(), Some(CollectionType::Edges), None)
}
pub fn system_documents_with_name<N>(name: N) -> Self
where N: Into<String>
{
NewCollection::new(name.into(), Some(CollectionType::Documents), Some(true))
}
pub fn system_edges_with_name<N>(name: N) -> Self
where N: Into<String>
{
NewCollection::new(name.into(), Some(CollectionType::Edges), Some(true))
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> Option<CollectionType> {
self.kind
}
pub fn is_system(&self) -> Option<bool> {
self.is_system
}
pub fn key_options_mut<K>(&mut self) -> &mut NewKeyOptions {
self.key_options.get_or_insert_with(NewKeyOptions::new)
}
pub fn remove_key_options(&mut self) -> Option<NewKeyOptions> {
mem::replace(&mut self.key_options, None)
}
pub fn key_options(&self) -> Option<&NewKeyOptions> {
self.key_options.as_ref()
}
pub fn set_wait_for_sync<W>(&mut self, wait_for_sync: W)
where W: Into<Option<bool>>
{
self.wait_for_sync = wait_for_sync.into();
}
pub fn is_wait_for_sync(&self) -> Option<bool> {
self.wait_for_sync
}
#[cfg(feature = "cluster")]
pub fn set_number_of_shards<S>(&mut self, number_of_shards: S)
where S: Into<Option<u16>>
{
self.number_of_shards = number_of_shards.into();
}
#[cfg(feature = "cluster")]
pub fn number_of_shards(&self) -> Option<u16> {
self.number_of_shards
}
#[cfg(feature = "cluster")]
pub fn set_shard_keys<K>(&mut self, shard_keys: K)
where K: Into<Option<String>>
{
self.shard_keys = shard_keys.into();
}
#[cfg(feature = "cluster")]
pub fn shard_keys(&self) -> Option<&String> {
self.shard_keys.as_ref()
}
#[cfg(feature = "cluster")]
pub fn set_replication_factor<R>(&mut self, replication_factor: R)
where R: Into<Option<u16>>
{
self.replication_factor = replication_factor.into();
}
#[cfg(feature = "cluster")]
pub fn replication_factor(&self) -> Option<u16> {
self.replication_factor
}
#[cfg(feature = "mmfiles")]
pub fn set_volatile<V>(&mut self, volatile: V)
where V: Into<Option<bool>>
{
self.is_volatile = volatile.into();
}
#[cfg(feature = "mmfiles")]
pub fn is_volatile(&self) -> Option<bool> {
self.is_volatile
}
#[cfg(feature = "mmfiles")]
pub fn set_do_compact<C>(&mut self, do_compact: C)
where C: Into<Option<bool>>
{
self.do_compact = do_compact.into();
}
#[cfg(feature = "mmfiles")]
pub fn is_do_compact(&self) -> Option<bool> {
self.do_compact
}
#[cfg(feature = "mmfiles")]
pub fn set_index_buckets<B>(&mut self, index_buckets: B)
where B: Into<Option<u16>>
{
self.index_buckets = index_buckets.into();
}
#[cfg(feature = "mmfiles")]
pub fn index_buckets(&self) -> Option<u16> {
self.index_buckets
}
#[cfg(feature = "mmfiles")]
pub fn set_journal_size<J>(&mut self, journal_size: J)
where J: Into<Option<u64>>
{
self.journal_size = journal_size.into();
}
#[cfg(feature = "mmfiles")]
pub fn journal_size(&self) -> Option<u64> {
self.journal_size
}
}
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct NewKeyOptions {
#[serde(skip_serializing_if = "Option::is_none")]
allow_user_keys: Option<bool>,
#[serde(rename = "type")]
kind: KeyGeneratorType,
#[serde(skip_serializing_if = "Option::is_none")]
increment: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
offset: Option<u64>,
}
impl NewKeyOptions {
fn new() -> Self {
NewKeyOptions::default()
}
pub fn set_allow_user_keys<U>(&mut self, allow_user_keys: U)
where U: Into<Option<bool>>
{
self.allow_user_keys = allow_user_keys.into();
}
pub fn is_allow_user_keys(&self) -> Option<bool> {
self.allow_user_keys
}
pub fn set_kind(&mut self, kind: KeyGeneratorType)
{
self.kind = kind;
}
pub fn kind(&self) -> &KeyGeneratorType {
&self.kind
}
pub fn set_increment<I>(&mut self, increment: I)
where I: Into<Option<u64>>
{
self.increment = increment.into();
}
pub fn increment(&self) -> Option<u64> {
self.increment
}
pub fn set_offset<O>(&mut self, offset: O)
where O: Into<Option<u64>>
{
self.offset = offset.into();
}
pub fn offset(&self) -> Option<u64> {
self.offset
}
}
impl Default for NewKeyOptions {
fn default() -> Self {
NewKeyOptions {
allow_user_keys: None,
kind: KeyGeneratorType::Traditional,
increment: None,
offset: None,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CollectionRevision {
id: String,
name: String,
#[serde(rename = "type")]
kind: CollectionType,
status: CollectionStatus,
is_system: bool,
revision: String,
}
impl CollectionRevision {
pub fn id(&self) -> &str {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> CollectionType {
self.kind
}
pub fn status(&self) -> CollectionStatus {
self.status
}
pub fn is_system(&self) -> bool {
self.is_system
}
pub fn revision(&self) -> &str {
&self.revision
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BasicCollectionProperties {
id: String,
name: String,
#[serde(rename = "type")]
kind: CollectionType,
status: CollectionStatus,
is_system: bool,
wait_for_sync: bool,
#[cfg(feature = "mmfiles")]
is_volatile: bool,
}
impl BasicCollectionProperties {
pub fn id(&self) -> &str {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> CollectionType {
self.kind
}
pub fn status(&self) -> CollectionStatus {
self.status
}
pub fn is_system(&self) -> bool {
self.is_system
}
pub fn is_wait_for_sync(&self) -> bool {
self.wait_for_sync
}
#[cfg(feature = "mmfiles")]
pub fn is_volatile(&self) -> bool {
self.is_volatile
}
}
impl From<CollectionProperties> for BasicCollectionProperties {
fn from(properties: CollectionProperties) -> Self {
BasicCollectionProperties {
id: properties.id,
name: properties.name,
kind: properties.kind,
status: properties.status,
is_system: properties.is_system,
wait_for_sync: properties.wait_for_sync,
#[cfg(feature = "mmfiles")]
is_volatile: properties.is_volatile
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CollectionProperties {
id: String,
name: String,
#[serde(rename = "type")]
kind: CollectionType,
status: CollectionStatus,
is_system: bool,
key_options: KeyOptions,
wait_for_sync: bool,
#[cfg(feature = "cluster")]
number_of_shards: u16,
#[cfg(feature = "cluster")]
shard_keys: String,
#[cfg(feature = "cluster")]
replication_factor: u64,
#[cfg(feature = "mmfiles")]
is_volatile: bool,
#[cfg(feature = "mmfiles")]
do_compact: bool,
#[cfg(feature = "mmfiles")]
index_buckets: u16,
#[cfg(feature = "mmfiles")]
journal_size: u64,
}
impl CollectionProperties {
pub fn id(&self) -> &str {
&self.id
}
pub fn name(&self) -> &str {
&self.name
}
pub fn kind(&self) -> CollectionType {
self.kind
}
pub fn status(&self) -> CollectionStatus {
self.status
}
pub fn is_system(&self) -> bool {
self.is_system
}
pub fn key_options(&self) -> &KeyOptions {
&self.key_options
}
pub fn is_wait_for_sync(&self) -> bool {
self.wait_for_sync
}
#[cfg(feature = "cluster")]
pub fn number_of_shards(&self) -> u16 {
self.number_of_shards
}
#[cfg(feature = "cluster")]
pub fn shard_keys(&self) -> &str {
&self.shard_keys
}
#[cfg(feature = "cluster")]
pub fn replication_factor(&self) -> u64 {
self.replication_factor
}
#[cfg(feature = "mmfiles")]
pub fn is_volatile(&self) -> bool {
self.is_volatile
}
#[cfg(feature = "mmfiles")]
pub fn is_do_compact(&self) -> bool {
self.do_compact
}
#[cfg(feature = "mmfiles")]
pub fn index_buckets(&self) -> u16 {
self.index_buckets
}
#[cfg(feature = "mmfiles")]
pub fn journal_size(&self) -> u64 {
self.journal_size
}
}
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CollectionPropertiesUpdate {
wait_for_sync: Option<bool>,
#[cfg(feature = "mmfiles")]
journal_size: Option<u64>,
}
#[cfg_attr(feature = "cargo-clippy", allow(new_without_default_derive))]
impl CollectionPropertiesUpdate {
pub fn new() -> Self {
CollectionPropertiesUpdate {
wait_for_sync: None,
#[cfg(feature = "mmfiles")]
journal_size: None,
}
}
pub fn set_wait_for_sync<W>(&mut self, wait_for_sync: W)
where W: Into<Option<bool>>
{
self.wait_for_sync = wait_for_sync.into();
}
pub fn is_wait_for_sync(&self) -> Option<bool> {
self.wait_for_sync
}
#[cfg(feature = "mmfiles")]
pub fn set_journal_size<J>(&mut self, journal_size: J)
where J: Into<Option<u64>>
{
self.journal_size = journal_size.into();
}
#[cfg(feature = "mmfiles")]
pub fn journal_size(&self) -> Option<u64> {
self.journal_size
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct RenameTo {
name: String,
}
impl RenameTo {
pub fn new<N>(new_name: N) -> Self
where N: Into<String>
{
RenameTo {
name: new_name.into(),
}
}
pub fn name(&self) -> &str {
&self.name
}
}
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct KeyOptions {
allow_user_keys: bool,
#[serde(rename = "type")]
kind: KeyGeneratorType,
last_value: u64,
}
impl KeyOptions {
pub fn is_allow_user_keys(&self) -> bool {
self.allow_user_keys
}
pub fn kind(&self) -> KeyGeneratorType {
self.kind
}
pub fn last_value(&self) -> u64 {
self.last_value
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CollectionType {
Documents,
Edges,
}
impl Serialize for CollectionType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
use self::CollectionType::*;
let type_id = match *self {
Documents => COLLECTION_TYPE_DOCUMENTS,
Edges => COLLECTION_TYPE_EDGES,
};
serializer.serialize_i32(type_id)
}
}
impl<'de> Deserialize<'de> for CollectionType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
use serde::de::Error;
use self::CollectionType::*;
let value = i32::deserialize(deserializer)?;
match value {
COLLECTION_TYPE_DOCUMENTS => Ok(Documents),
COLLECTION_TYPE_EDGES => Ok(Edges),
_ => Err(D::Error::custom(format!("Unknown collection type: {:?}", value))),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CollectionStatus {
NewBorn,
Unloaded,
Loaded,
BeingUnloaded,
Deleted,
BeingLoaded,
Corrupted,
}
impl Serialize for CollectionStatus {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
use self::CollectionStatus::*;
let status_id = match *self {
NewBorn => COLLECTION_STATUS_NEW_BORN,
Unloaded => COLLECTION_STATUS_UNLOADED,
Loaded => COLLECTION_STATUS_LOADED,
BeingUnloaded => COLLECTION_STATUS_BEING_UNLOADED,
Deleted => COLLECTION_STATUS_DELETED,
BeingLoaded => COLLECTION_STATUS_BEING_LOADED,
Corrupted => COLLECTION_STATUS_CORRUPTED,
};
serializer.serialize_i32(status_id)
}
}
impl<'de> Deserialize<'de> for CollectionStatus {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
use self::CollectionStatus::*;
let value = i32::deserialize(deserializer)?;
match value {
COLLECTION_STATUS_NEW_BORN => Ok(NewBorn),
COLLECTION_STATUS_UNLOADED => Ok(Unloaded),
COLLECTION_STATUS_LOADED => Ok(Loaded),
COLLECTION_STATUS_BEING_UNLOADED => Ok(BeingUnloaded),
COLLECTION_STATUS_DELETED => Ok(Deleted),
COLLECTION_STATUS_BEING_LOADED => Ok(BeingLoaded),
_ => Ok(Corrupted),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum KeyGeneratorType {
Traditional,
AutoIncrement,
}
impl Serialize for KeyGeneratorType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
use self::KeyGeneratorType::*;
let type_str = match *self {
Traditional => KEY_GENERATOR_TYPE_TRADITIONAL,
AutoIncrement => KEY_GENERATOR_TYPE_AUTO_INCREMENT,
};
serializer.serialize_str(type_str)
}
}
impl<'de> Deserialize<'de> for KeyGeneratorType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
use serde::de::Error;
use self::KeyGeneratorType::*;
let value = String::deserialize(deserializer)?;
match &value[..] {
KEY_GENERATOR_TYPE_TRADITIONAL => Ok(Traditional),
KEY_GENERATOR_TYPE_AUTO_INCREMENT => Ok(AutoIncrement),
_ => Err(D::Error::custom(format!("Unknown KeyGeneratorType: {:?}", value))),
}
}
}