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
use crate::api_core::adding_tags::{AddTagsRequestBuilder, TagAction};
use crate::api_core::common::ServiceIdentifier;
use crate::error::Result;
use crate::wrapper::tag::Tag;
use crate::Client;
use std::collections::HashMap;

pub struct TaggingBuilder {
    client: Client,
    hashes: Vec<String>,
    tag_mappings: HashMap<ServiceIdentifier, HashMap<TagAction, Vec<Tag>>>,
}

impl TaggingBuilder {
    pub(crate) fn new(client: Client) -> Self {
        Self {
            client,
            hashes: Vec::new(),
            tag_mappings: Default::default(),
        }
    }

    /// Adds a file that should get the tags defined for this request
    pub fn add_file<S: ToString>(mut self, hash: S) -> Self {
        self.hashes.push(hash.to_string());

        self
    }

    /// Adds a single tag for a given service
    pub fn add_tag(self, service: ServiceIdentifier, action: TagAction, tag: Tag) -> Self {
        self.add_tags(service, action, vec![tag])
    }

    /// Adds tags with actions for the given service
    pub fn add_tags(
        mut self,
        service: ServiceIdentifier,
        action: TagAction,
        mut tags: Vec<Tag>,
    ) -> Self {
        let service_action_mappings =
            if let Some(service_action_mappings) = self.tag_mappings.get_mut(&service) {
                service_action_mappings
            } else {
                self.tag_mappings.insert(service.clone(), HashMap::new());
                self.tag_mappings.get_mut(&service).unwrap()
            };
        if let Some(action_tag_mappings) = service_action_mappings.get_mut(&action) {
            action_tag_mappings.append(&mut tags)
        } else {
            service_action_mappings.insert(action, tags);
        }

        self
    }

    /// Executes the request
    pub async fn run(self) -> Result<()> {
        let mut request = AddTagsRequestBuilder::default().add_hashes(self.hashes);
        for (service, action_tag_mappings) in self.tag_mappings {
            for (action, tags) in action_tag_mappings {
                for tag in tags {
                    request = request.add_tag_with_action(
                        service.clone().into(),
                        tag.to_string(),
                        action.clone(),
                    );
                }
            }
        }

        self.client.add_tags(request.build()).await
    }
}