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
//! API specific documentation.

mod account;
mod action;
mod certificate;
mod custom_image;
mod domain;
mod domain_record;
mod droplet;
mod droplet_action;
mod floating_ip;
mod floating_ip_action;
mod image;
mod image_action;
mod load_balancer;
mod region;
mod size;
mod snapshot;
mod ssh_key;
mod tag;
mod volume;
mod volume_action;

use serde::de::DeserializeOwned;
use url::Url;
use url_serde;

pub use self::account::Account;
pub use self::action::Action;
pub use self::certificate::Certificate;
pub use self::custom_image::CustomImage;
pub use self::domain::Domain;
pub use self::domain_record::DomainRecord;
pub use self::droplet::{droplet_fields, Droplet};
pub use self::floating_ip::FloatingIp;
pub use self::image::Image;
pub use self::load_balancer::{load_balancer_fields, LoadBalancer};
pub use self::region::Region;
pub use self::size::Size;
pub use self::snapshot::Snapshot;
pub use self::ssh_key::SshKey;
pub use self::tag::Tag;
pub use self::volume::Volume;

// Defined in https://developers.digitalocean.com/documentation/v2/#links
pub const MAX_PER_PAGE: usize = 200;

#[derive(Deserialize, Serialize, Debug, Clone)]
struct ApiLinks {
    pages: Option<ApiPages>,
}

impl ApiLinks {
    fn next(&self) -> Option<Url> {
        match self.pages {
            Some(ref pages) => match pages.next {
                Some(ref v) => Some(v.clone()),
                None => None,
            },
            None => None,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone)]
struct ApiPages {
    #[serde(with = "url_serde", default)]
    prev: Option<Url>,
    #[serde(with = "url_serde", default)]
    first: Option<Url>,
    #[serde(with = "url_serde", default)]
    next: Option<Url>,
    #[serde(with = "url_serde", default)]
    last: Option<Url>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
struct ApiMeta {
    total: usize,
}

pub trait HasPagination {
    fn next_page(&self) -> Option<Url>;
}

pub trait HasValue {
    type Value: DeserializeOwned;
    fn value(self) -> Self::Value;
}

impl HasValue for () {
    type Value = ();
    fn value(self) -> Self::Value {
        ()
    }
}

pub trait HasResponse: DeserializeOwned + Clone {
    type Response: DeserializeOwned + Clone + HasValue<Value = Self>;
}

impl HasResponse for () {
    type Response = ();
}