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
use super::Selection;
use crate::ArgumentValue;
use schema::QuerySchema;

#[derive(Debug, Clone)]
pub enum Operation {
    Read(Selection),
    Write(Selection),
}

impl Operation {
    pub(crate) fn is_find_unique(&self, schema: &QuerySchema) -> bool {
        schema
            .find_query_field(self.name())
            .map(|field| field.is_find_unique())
            .unwrap_or(false)
    }

    pub fn into_read(self) -> Option<Selection> {
        match self {
            Self::Read(sel) => Some(sel),
            _ => None,
        }
    }

    pub fn into_write(self) -> Option<Selection> {
        match self {
            Self::Write(sel) => Some(sel),
            _ => None,
        }
    }

    pub fn into_selection(self) -> Selection {
        match self {
            Operation::Read(selection) => selection,
            Operation::Write(selection) => selection,
        }
    }

    pub fn as_read(&self) -> Option<&Selection> {
        if let Self::Read(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn arguments(&self) -> &[(String, ArgumentValue)] {
        match self {
            Operation::Read(x) => x.arguments(),
            Operation::Write(x) => x.arguments(),
        }
    }
}

impl Operation {
    pub fn dedup_selections(self) -> Self {
        match self {
            Self::Read(s) => Self::Read(s.dedup()),
            Self::Write(s) => Self::Write(s.dedup()),
        }
    }

    pub fn name(&self) -> &str {
        match self {
            Self::Read(s) => s.name(),
            Self::Write(s) => s.name(),
        }
    }

    pub fn nested_selections(&self) -> &[Selection] {
        match self {
            Self::Read(s) => s.nested_selections(),
            Self::Write(s) => s.nested_selections(),
        }
    }
}