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
// 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.

//! Graph Delete Vertex Collection Input Structs

use crate::{
    model::{add_qp, BuildUrl, QueryParam::DropCollection},
    Connection,
};
use anyhow::{Context, Result};
use derive_builder::Builder;
use getset::Getters;
use reqwest::Url;
use serde::{Deserialize, Serialize};

/// Graph delete vertex collection configuration
#[derive(Builder, Clone, Debug, Default, Deserialize, Getters, Serialize)]
#[getset(get = "pub(crate)")]
pub struct Config {
    /// The name of the graph to delete the vertex collection from
    #[builder(setter(into))]
    name: String,
    /// The name of the vertex collection to remove
    #[builder(setter(into))]
    collection: String,
    /// Drop the collection as well.
    /// Collection will only be dropped if it is not used in other graphs.
    #[builder(setter(strip_option), default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    drop_collection: Option<bool>,
}

impl Config {
    fn build_suffix(&self, base: &str) -> String {
        let mut url = format!("{}/{}/vertex/{}", base, self.name, self.collection);
        let mut has_qp = false;

        add_qp(
            *self.drop_collection(),
            &mut url,
            &mut has_qp,
            DropCollection,
        );

        url
    }
}

impl BuildUrl for Config {
    fn build_url(&self, base: &str, conn: &Connection) -> Result<Url> {
        let suffix = self.build_suffix(base);
        conn.db_url()
            .join(&suffix)
            .with_context(|| format!("Unable to build '{}' url", suffix))
    }
}