Skip to main content

chipa_webhooks/
hints.rs

1use serde_json::{Map, Value};
2
3pub struct WithHints {
4    pub(crate) map: Map<String, Value>,
5}
6
7impl WithHints {
8    pub fn new() -> Self {
9        Self { map: Map::new() }
10    }
11
12    pub fn d_color(mut self, color: u32) -> Self {
13        self.map
14            .insert("__d_color".to_owned(), Value::Number(color.into()));
15        self
16    }
17
18    pub fn d_title(mut self, title: impl Into<String>) -> Self {
19        self.map
20            .insert("__d_title".to_owned(), Value::String(title.into()));
21        self
22    }
23
24    pub fn tg_silent(mut self) -> Self {
25        self.map.insert("__tg_silent".to_owned(), Value::Bool(true));
26        self
27    }
28
29    pub fn tg_disable_preview(mut self) -> Self {
30        self.map
31            .insert("__tg_disable_preview".to_owned(), Value::Bool(true));
32        self
33    }
34}
35
36impl Default for WithHints {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42/// Extracts and removes all `__`-prefixed hint keys from a Value::Object.
43/// Returns the hints as a separate map. Non-object values return empty hints.
44pub fn extract_hints(value: &mut Value) -> Map<String, Value> {
45    let Some(map) = value.as_object_mut() else {
46        return Map::new();
47    };
48
49    let hint_keys: Vec<String> = map
50        .keys()
51        .filter(|k| k.starts_with("__"))
52        .cloned()
53        .collect();
54
55    let mut hints = Map::new();
56    for key in hint_keys {
57        if let Some(v) = map.remove(&key) {
58            hints.insert(key, v);
59        }
60    }
61
62    hints
63}