hydrus_api/wrapper/builders/
notes_builder.rs

1use crate::api_core::common::FileIdentifier;
2use crate::error::Result;
3use crate::Client;
4use std::collections::HashMap;
5
6/// Builder to create a request for adding notes to a given file
7pub struct AddNotesBuilder {
8    client: Client,
9    file: FileIdentifier,
10    notes: HashMap<String, String>,
11}
12
13impl AddNotesBuilder {
14    /// Creates a new notes builder for the given file id
15    pub fn new(client: Client, file: FileIdentifier) -> Self {
16        Self {
17            client,
18            file,
19            notes: HashMap::new(),
20        }
21    }
22
23    /// Adds a single note
24    pub fn add_note<S1: ToString, S2: ToString>(mut self, name: S1, note: S2) -> Self {
25        self.notes.insert(name.to_string(), note.to_string());
26
27        self
28    }
29
30    /// Adds multiple notes to the builder
31    pub fn add_notes<I: IntoIterator<Item = (S1, S2)>, S1: ToString, S2: ToString>(
32        mut self,
33        notes: I,
34    ) -> Self {
35        let notes_iter = notes
36            .into_iter()
37            .map(|(k, v): (S1, S2)| (k.to_string(), v.to_string()));
38        self.notes.extend(notes_iter);
39
40        self
41    }
42
43    /// Adds all notes mentioned in the builder to the given file
44    pub async fn run(self) -> Result<()> {
45        self.client.set_notes(self.file, self.notes).await
46    }
47}