streamduck_core/core/
button.rs

1use std::collections::HashMap;
2use std::ops::Deref;
3use serde::{Serialize, Deserialize};
4use serde::de::DeserializeOwned;
5use serde_json::{Error, Value};
6use crate::core::UniqueButton;
7
8/// Button definition, it's simply a hashmap, but is used to represent all the components of the button
9#[derive(Serialize, Deserialize, Debug, Clone, Default)]
10pub struct Button(pub HashMap<String, Value>);
11
12impl Button {
13    /// Creates a new empty button
14    pub fn new() -> Button {
15        Button::default()
16    }
17
18    /// Inserts new component into the button
19    pub fn insert_component<T: Component + Serialize>(&mut self, component: T) -> Result<Option<Value>, ParseError> {
20        Ok(self.0.insert(T::NAME.to_string(), serialize_component(component)?))
21    }
22
23    /// Removes component of provided type from the button
24    pub fn remove_component<T: Component>(&mut self) -> Option<Value> {
25        self.0.remove(T::NAME)
26    }
27
28    /// Returns list of components in the button
29    pub fn component_names(&self) -> Vec<String> {
30        self.0.keys().map(|x| x.clone()).collect()
31    }
32
33    /// Checks if button contains specified component name
34    pub fn contains(&self, name: &str) -> bool {
35        self.0.contains_key(name)
36    }
37}
38
39/// Component trait, simply provides name for component generic methods
40pub trait Component {
41    /// Name of the component
42    const NAME: &'static str;
43}
44
45/// Attempts to retrieve a component from a button
46pub fn parse_button_to_component<T: Component + DeserializeOwned>(button: &Button) -> Result<T, ParseError> {
47    if let Some(component_value) = button.0.get(T::NAME) {
48        Ok(serde_json::from_value(component_value.clone())?)
49    } else {
50        Err(ParseError::Missing)
51    }
52}
53
54/// Attempts to retrieve a component from reference counted button
55pub async fn parse_unique_button_to_component<T: Component + DeserializeOwned>(button: &UniqueButton) -> Result<T, ParseError> {
56    parse_button_to_component(button.read().await.deref())
57}
58
59/// Serializes component into JSON
60pub fn serialize_component<T: Component + Serialize>(component: T) -> Result<Value, ParseError> {
61    Ok(serde_json::to_value(component)?)
62}
63
64/// Parse error used for functions in this module
65#[derive(Debug)]
66pub enum ParseError {
67    /// Component is missing
68    Missing,
69    /// Component failed to parse
70    JSONError(serde_json::Error)
71}
72
73impl From<serde_json::Error> for ParseError {
74    fn from(err: Error) -> Self {
75        ParseError::JSONError(err)
76    }
77}