fobserver 0.2.4

A lightweight and straightforward HTTP server library written in Rust
Documentation
use std::{
    any::Any,
    collections::HashMap,
    sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

/// A struct to manage a collection of arguments, allowing for dynamic typing and thread-safe access.
#[derive(Debug)]
pub struct Args {
    args: HashMap<String, Arc<RwLock<dyn Any + Send + Sync>>>,
}

impl Args {
    /// Creates a new instance of `Args`.
    pub fn new() -> Self {
        Args {
            args: HashMap::new(),
        }
    }

    /// Adds an argument to the `Args` collection.
    ///
    /// This method allows you to insert a new argument, identified by a string `name`,
    /// along with its value wrapped in `Arc<RwLock<dyn Any + Send + Sync>>`.
    ///
    /// # Parameters
    /// - `name`: A string slice that represents the name of the argument.
    /// - `arg`: An `Arc<RwLock<dyn Any + Send + Sync>>` containing the argument value.
    ///
    /// # Returns
    /// A mutable reference to `self` to allow for method chaining.
    pub fn add_arg(&mut self, name: &str, arg: Arc<RwLock<dyn Any + Send + Sync>>) -> &mut Self {
        self.args.insert(name.to_string(), arg);

        self
    }

    /// Retrieves an argument from the `Args` collection by its name.
    ///
    /// This method returns a `Box<RwLockReadGuard<dyn Any + Send + Sync + 'static>>`.
    ///
    /// # Parameters
    /// - `name`: A string slice that represents the name of the argument to retrieve.
    ///
    /// # Returns
    /// A `Box` containing the argument.
    pub fn arg(&self, name: &str) -> Box<RwLockReadGuard<dyn Any + Send + Sync + 'static>> {
        let b = self
            .args
            .get(name)
            .expect(&format!("Unable to get argument {}", name));
        let l = b.read().unwrap();

        Box::new(l)
    }

    /// Retrieves an argument from the `Args` collection by its name.
    ///
    /// This method returns a `Box<RwLockWriteGuard<dyn Any + Send + Sync + 'static>>`.
    ///
    /// # Parameters
    /// - `name`: A string slice that represents the name of the argument to retrieve.
    ///
    /// # Returns
    /// A `Box` containing the argument.
    pub fn arg_mut(&self, name: &str) -> Box<RwLockWriteGuard<dyn Any + Send + Sync + 'static>> {
        let b = self
            .args
            .get(name)
            .expect(&format!("Unable to get argument {}", name));
        let l = b.write().unwrap();

        Box::new(l)
    }
}