rodo_lib 0.1.2

A Library for a Todo Manager
Documentation
use std::collections::HashMap;

use uuid::Uuid;

pub enum Functions<A, E> {
    ADD((Uuid, A)),
    EDIT((Uuid, E)),
    REMOVE(Uuid),
}

/// Deciedes what action to perform based on `fnc`
///
/// * `hashmap`: the field to perform the action on
/// * `fnc`: the function to run from `Functions`
pub fn handler<T, U>(hashmap: &mut HashMap<Uuid, T>, fnc: Functions<T, U>)
where
    T: RodoStruct<U>,
{
    match fnc {
        Functions::ADD(data) => {
            // NOTE: handle duplicates?
            hashmap.insert(data.0, data.1);
        }
        Functions::EDIT(data) => {
            if let Some(v) = hashmap.get_mut(&data.0) {
                v.edit(data.1)
            }
        }
        Functions::REMOVE(id) => {
            hashmap.remove(&id);
        }
    };
}

/// Adds or Removes a Uuid from the given Vector
///
/// * `field`: the Vector to manipulate
/// * `id`: the Uuid to add or remove
pub fn edit_vecs(field: &mut Vec<Uuid>, id: Uuid) {
    if field.contains(&id) {
        field.retain(|i| *i != id);
    } else {
        field.push(id);
    }
}

pub trait RodoStruct<F> {
    /// Edits the given Field in the current Struct
    ///
    /// * `fields`: the Field to edit
    fn edit(&mut self, fields: F);
}