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
use super::edges::EdgeKey;
use super::types::Type;
use chrono::DateTime;
use chrono::offset::Utc;
use uuid::Uuid;

/// Specifies what kind of items should be piped from one type of query to
/// another.
///
/// Edge and vertex queries can build off of one another via pipes - e.g. you
/// can get the outbound edges of a set of vertices by piping from a vertex
/// query to an edge query. `EdgeDirection`s are used to specify which
/// end of things you want to pipe - either the outbound items or the inbound
/// items.
#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Hash, Copy)]
pub enum EdgeDirection {
    #[serde(rename = "outbound")] Outbound,
    #[serde(rename = "inbound")] Inbound,
}

/// A query for vertices.
///
/// This is used by transactions to get, set and delete vertices and vertex
/// metadata.
#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Hash)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum VertexQuery {
    All {
        start_id: Option<Uuid>,
        limit: u32,
    },
    Vertices {
        ids: Vec<Uuid>,
    },
    Pipe {
        edge_query: Box<EdgeQuery>,
        converter: EdgeDirection,
        limit: u32,
    },
}

impl VertexQuery {
    pub fn outbound_edges(
        self,
        t: Option<Type>,
        high: Option<DateTime<Utc>>,
        low: Option<DateTime<Utc>>,
        limit: u32,
    ) -> EdgeQuery {
        EdgeQuery::Pipe {
            vertex_query: Box::new(self),
            converter: EdgeDirection::Outbound,
            type_filter: t,
            high_filter: high,
            low_filter: low,
            limit: limit,
        }
    }

    pub fn inbound_edges(
        self,
        t: Option<Type>,
        high: Option<DateTime<Utc>>,
        low: Option<DateTime<Utc>>,
        limit: u32,
    ) -> EdgeQuery {
        EdgeQuery::Pipe {
            vertex_query: Box::new(self),
            converter: EdgeDirection::Inbound,
            type_filter: t,
            high_filter: high,
            low_filter: low,
            limit: limit,
        }
    }
}

/// A query for edges.
///
/// This is used by transactions to get, set and delete edges and edge
/// metadata.
#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Hash)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum EdgeQuery {
    Edges {
        keys: Vec<EdgeKey>,
    },
    Pipe {
        vertex_query: Box<VertexQuery>,
        converter: EdgeDirection,
        type_filter: Option<Type>,
        high_filter: Option<DateTime<Utc>>,
        low_filter: Option<DateTime<Utc>>,
        limit: u32,
    },
}

impl EdgeQuery {
    pub fn outbound_vertices(self, limit: u32) -> VertexQuery {
        VertexQuery::Pipe {
            edge_query: Box::new(self),
            converter: EdgeDirection::Outbound,
            limit: limit,
        }
    }

    pub fn inbound_vertices(self, limit: u32) -> VertexQuery {
        VertexQuery::Pipe {
            edge_query: Box::new(self),
            converter: EdgeDirection::Inbound,
            limit: limit,
        }
    }
}