reql/cmd/
update.rs

1use super::args::Args;
2use crate::cmd::{self, Durability, ReturnChanges};
3use crate::{Command, Func};
4use ql2::term::TermType;
5use reql_macros::CommandOptions;
6use serde::Serialize;
7
8// TODO finish this struct
9#[derive(Debug, Clone, Copy, CommandOptions, Serialize, Default, PartialEq, PartialOrd)]
10#[non_exhaustive]
11pub struct Options {
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub durability: Option<Durability>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub return_changes: Option<ReturnChanges>,
16}
17
18pub trait Arg {
19    fn arg(self) -> cmd::Arg<Options>;
20}
21
22impl Arg for cmd::Arg<Options> {
23    fn arg(self) -> cmd::Arg<Options> {
24        self
25    }
26}
27
28impl Arg for Command {
29    fn arg(self) -> cmd::Arg<Options> {
30        Command::new(TermType::Update).with_arg(self).into_arg()
31    }
32}
33
34impl<T> Arg for T
35where
36    T: Serialize,
37{
38    fn arg(self) -> cmd::Arg<Options> {
39        Command::from_json(self).arg()
40    }
41}
42
43impl Arg for Args<(Command, Options)> {
44    fn arg(self) -> cmd::Arg<Options> {
45        let Args((arg, opts)) = self;
46        arg.arg().with_opts(opts)
47    }
48}
49
50impl<T> Arg for Args<(T, Options)>
51where
52    T: Serialize,
53{
54    fn arg(self) -> cmd::Arg<Options> {
55        let Args((arg, opts)) = self;
56        let arg = Command::from_json(arg);
57        arg.arg().with_opts(opts)
58    }
59}
60
61impl Arg for Func {
62    fn arg(self) -> cmd::Arg<Options> {
63        let Func(func) = self;
64        func.arg()
65    }
66}
67
68impl Arg for Args<(Func, Options)> {
69    fn arg(self) -> cmd::Arg<Options> {
70        let Args((Func(func), opts)) = self;
71        func.arg().with_opts(opts)
72    }
73}