summaryrefslogtreecommitdiff
path: root/rola-cli/src/bucket_mgr/bind/chains.rs
blob: b8037e2041385990bc424661ab2ac412c014f272 (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::{
    Next,
    bucket_mgr::bind::{
        EntryBucketBind, ErrorBucketBindIndexNotFound, ErrorBucketBindIndexNotProvided,
        ResultAllBucketBind, ResultBucketOperated,
        ResultEnumBucketOperation::{self, Set},
        ResultOneBucketBind, StateBucketBindListAll, StateBucketBindListSpecified,
        StateBucketBindRemove, StateBucketBindSet,
    },
    error::require_overwrite::ErrorRequireOverwrite,
    error::space::ErrorSpace,
    res::{bucket::ResBucketWithoutProtocol, overwrite::ResOverwrite},
};
use mingling::{
    Groupped, LazyRes,
    macros::{chain, route},
};
use rorolala::bucket::{self};
use shared_functions::info;

// Chains

#[chain]
pub fn handle_bucket_bind(args: EntryBucketBind) -> Next {
    match args.index {
        Some(op_idx) => {
            if let Some(url) = args.set {
                // bind idx --set url
                return StateBucketBindSet::new((op_idx, url)).to_chain();
            } else if args.remove {
                // bind idx --remove
                return StateBucketBindRemove::new(op_idx).to_chain();
            } else {
                // bind idx
                return StateBucketBindListSpecified::new(op_idx).to_chain();
            }
        }
        None => {
            if !args.remove && args.set.is_none() {
                // bind
                return StateBucketBindListAll::new(()).to_chain();
            } else {
                // index not provided
                // bind --set url
                // or
                // bind --remove
                return ErrorBucketBindIndexNotProvided::default().to_render();
            }
        }
    }
}

#[chain]
pub fn handle_state_bucket_bind_set(
    set: StateBucketBindSet,
    bucket: &mut LazyRes<ResBucketWithoutProtocol>,
    overwrite: &ResOverwrite,
) -> Next {
    let (index, url) = set.inner;
    let bucket_space_ref = &bucket.get_ref().space;

    let exist = route!(bucket::bind::check_bucket_bind_exists(
        bucket_space_ref,
        index
    ));

    // When the bind already exists but overwrite is not specified
    // Dispatch to ErrorRequireOverwrite
    if exist && !overwrite.overwrite {
        return ErrorRequireOverwrite::default().to_render();
    }

    if let Err(space_error) =
        bucket::bind::write_bucket_bind(bucket_space_ref, index, url.clone().as_str())
    {
        ErrorSpace::from(space_error).to_chain();
    }

    ResultBucketOperated {
        operation: Set,
        index,
        url: Some(url),
    }
    .to_render()
}

#[chain]
pub fn handle_state_bucket_bind_remove(
    remove: StateBucketBindRemove,
    bucket: &mut LazyRes<ResBucketWithoutProtocol>,
) -> Next {
    let index = remove.inner;
    let bucket_space_ref = &bucket.get_ref().space;

    route!(bucket::bind::remove_bucket_bind(bucket_space_ref, index));

    ResultBucketOperated {
        operation: ResultEnumBucketOperation::Remove,
        index,
        url: None,
    }
    .to_render()
}

#[chain]
pub fn handle_state_bucket_bind_list_specified(
    index: StateBucketBindListSpecified,
    bucket: &mut LazyRes<ResBucketWithoutProtocol>,
) -> Next {
    let index = index.inner;
    let bucket_space_ref = &bucket.get_ref().space;
    let bind_info = match route!(bucket::bind::read_bucket_bind(bucket_space_ref, index)) {
        None => return ErrorBucketBindIndexNotFound::new(index).to_render(),
        Some(r) => r,
    };

    ResultOneBucketBind {
        index: bind_info.get_index(),
        url: bind_info.get_url().to_string(),
    }
    .to_render()
}

#[chain]
pub fn handle_state_bucket_bind_list_all(
    _p: StateBucketBindListAll,
    bucket: &mut LazyRes<ResBucketWithoutProtocol>,
) -> Next {
    let bucket_space_ref = &bucket.get_ref().space;
    info!("Reading all bucket binds from space");
    let bind_list = route!(bucket::bind::read_bucket_binds(bucket_space_ref));
    info!("Read {} bucket binds", bind_list.len());
    ResultAllBucketBind { bind_list }.to_render()
}