am_api/request/
view.rs

1//! Request builder that supports views
2
3use crate::request::context::RequestContext;
4use std::collections::{HashMap, HashSet};
5use std::fmt::Display;
6
7/// A trait for getting information about a view type
8pub trait ViewTrait: Display {
9    /// Get view object name
10    fn get_object(&self) -> &'static str;
11}
12
13/// View storage
14#[derive(Default)]
15pub struct ViewStorage {
16    storage: HashMap<&'static str, HashSet<String>>,
17}
18
19impl ViewStorage {
20    /// Add a view to this storage
21    pub fn add_view(&mut self, view: impl ViewTrait) {
22        self.storage
23            .entry(view.get_object())
24            .or_default()
25            .insert(view.to_string());
26    }
27
28    /// Build view query parameters draining this storage
29    pub fn build_query_drain(&mut self, request_context: &mut RequestContext) {
30        for (object, extend) in self.storage.drain() {
31            request_context.query.push((
32                format!("views[{}]", object),
33                extend.into_iter().collect::<Vec<_>>().join(","),
34            ));
35        }
36    }
37}