pub struct Account<N, K, V> { /* private fields */ }
Expand description

A HashMap wrapper for layered settings.

The Stg type is a type abstraction that can be used to to have an Account with distinct types.

An Account<N,K,S> can also hold other Accounts. This allows for complex systems where an app can have multiple layers of settings. The top most layer being the first one to be searched for a specific setting, and in the case it isn’t found the next layer will be search, this will be done until the setting is found on the last layer that would be the default layer containing all the settings.

An Account contains the following fields:

§New Account

A new Account can be created with:

  • new: Create a new Account.

  • new_unchecked: Creates a new account without verifying its validity.

  • clone: Clone an existing Account.

An AccountBuilder is planned to be created in the future.

§Name

An Account's name is used to identify an Account in multiple methods involving child Accounts .

For this reason child Accounts need to be uniquely named for the parent Account to be valid and N is required to implement the Incrementable trait automatically increments the name in case of repetition.

§Active

If a child Account is inactive it’s settings will be ignore by the parent Account.

§Settings

A HashMap holding Settings. Contains all the settings present in the child Accounts but can contain settings that aren’t in them.

  • hashmap: Returns a reference to HashMap.

  • get: Returns a reference to the value corresponding to the key

  • insert: Inserts a key-value pair into the map.

  • deep_insert: Inserts a key-value pair into the map of a child Account.

  • remove: Removes a key-value pair from the map.

  • deep_remove: Removes a key-value pair from the map of a child Account.

  • keys: An iterator visiting all keys in arbitrary order

  • contains_key: Returns true if the Account contains a value for the specified key.

  • capacity: Returns the number of elements the map can hold without reallocating.

  • update_setting: Updates a setting with the value its supposed to have.

  • update_setting: Updates a setting with the value its supposed to have and.

  • update_setting: Updates a group of settings with the value they are supposed to have.

  • update_setting: Updates all settings currently present in the Account with the value they are supposed to have.

§Accounts

A Vec of Accounts. The Account that holds the Vec is the parent Account and the Accounts that are being held are the child Accounts.

We consider the bottom layer of the Vec the Account at index 0, and the top layer the on at len()-1.

When the Vec is changed, the parent account will update its settings, such that when we use get() on the parent Account we obtain the value present in the top layer containing the setting or return None if no layer contained it.

  • accounts: Get an Account’s child Accounts

  • accounts: Return a Vec of names of the child Accounts.

  • len: Returns the number of elements in the Vec.

  • is_empty: Returns true if the Vec contains no elements.

  • push: Appends an Account to the back of the Vec.

  • deep_push: Appends an Account to the back of the Vecof a child Account.

  • pop: Removes the last element from a vector and returns it, or None if it is empty.

  • deep_pop: Removes the last element from a vector of a child Account

§Valid

A valid Account is one where it’s methods will always behave as intended.

There are certain methods that may make an Account invalid if improperly used, and that would make other methods have unindent effects.

If a method can make an Account invalid it will be mentioned.

§Validity Defined:

Account contains a valid field of type Valid that tracks if an Account is valid.

Valid contains 3 bool fields corresponding to the 3 ways an account can be invalid:

  • names: An Account is invalid if it’s children Accounts have duplicated names.

  • settings: An Account is invalid if it doesn’t contain all settings present in it’s children Accounts.

  • accounts: An Account is invalid if it’s children Accounts are themselves invalid.

If all the fields are true then the Account is valid.

An Account can be temporary made invalid for:

But this should be fixed immediately after.

-valid: Returns a reference to the Account valid field.

-update_valid: Updates valid to the values it’s supposed to have.

-change_valid: Changes the valid to a the provided Valid

-fix_valid: Makes an invalid Account valid

§Deep Functions

Deep Functions are versions of functions to interact with a child Account of the parent Account that the function is called.

They accept an extra Vec of &N that are the list of child Accounts you have to pass though to get to the child Account the function will be called. For each value in the Vec the value to its right is its parent. Meaning that the right most value is the a direct child of the Account we call the function on, and the left most is the the Account we will interact with.

Deep functions can return DeepError’s

The main function is deep to get a reference to a child Account, deep_mut exists but it can make an Account invalid so its recommend to use the deep version of methods instead

Implementations§

source§

impl<N, K, V> Account<N, K, V>

source

pub fn new_unchecked( name: N, active: bool, settings: HashMap<K, V>, accounts: Vec<Self>, valid: Valid ) -> Self

Creates a new account without verifying its validity

The is no validity check, so the account created can be an invalid account. Use new to make sure that the account created is valid.

It’s recommend that the parent Accounts are made with new but child Accounts are made with with new_unchecked.

§Example
use std::collections::HashMap;
use hashmap_settings::account::*;
let account = Account::new_unchecked(
    "New Account".to_string(),
    true,
    HashMap::from([
        ("answer".to_string(),42),
        ("zero".to_string(),0),
        ("big_number".to_string(),10000),
    ]),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), Default::default())
    ],
    Valid::new(true,true,true),
);

assert_eq!(account.name(), "New Account");
assert!(account.active());
assert!(account.hashmap() ==
    &HashMap::from([
        ("answer".to_string(),42),
        ("zero".to_string(),0),
        ("big_number".to_string(),10000)
    ])
);
assert!(account.accounts() ==
    &vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), Default::default())
    ],
);
assert!(account.valid() ==
    &Valid::new(true,true,true)
);
source

pub const fn name(&self) -> &N

Returns the name of the Account

§Examples
use hashmap_settings::account::Account;
let account = Account::<String,(),()>::new(
    "New account".to_string(),
    Default::default(),
    Default::default(),
    Default::default()
);

assert_eq!(account.name(), "New account");
source

pub const fn active(&self) -> bool

Return true if the Account is active

When not active Accounts will be treated as if they were not there when called by some of the parent’s Account methods.

When creating an Account with Default active will be true.

§Examples
use hashmap_settings::account::Account;
let mut account = Account::<String,(),()>::new(Default::default(), true, Default::default(), Default::default());

assert!(account.active());
account.change_activity(false);
assert!(!account.active());
source

pub fn change_activity(&mut self, new_active: bool) -> bool

Takes a bool and changes the value of active, returns true if changes were made.

§Examples
use hashmap_settings::account::Account;
let mut account = Account::<String,(),()>::new(Default::default(), false, Default::default(), Default::default());

assert!(!account.active());
assert_eq!(account.change_activity(true), true);
assert!(account.active());
assert_eq!(account.change_activity(true), false);
assert!(account.active());
source

pub const fn hashmap(&self) -> &HashMap<K, V>

Return a reference to the HashMap

§Examples
use hashmap_settings::account::Account;
use std::collections::HashMap;
let account = Account::<String,String,i32>::new(
    "New Account".to_string(),
    Default::default(),
    HashMap::from([
        ("answer".to_string(),42),
        ("zero".to_string(),0),
        ("big_number".to_string(),10000),
    ]),
    Default::default(),
);

assert!(account.hashmap() ==
    &HashMap::from([
        ("answer".to_string(),42),
        ("zero".to_string(),0),
        ("big_number".to_string(),10000),
    ])
);
source

pub fn keys(&self) -> Keys<'_, K, V>

An iterator visiting all keys in arbitrary order. The iterator element type is &'a K.

This method is a direct call to HashMap’s keys().

§Examples
use hashmap_settings::account::Account;
use std::collections::HashMap;
let account = Account::<String,String,i32>::new(
    Default::default(),
    Default::default(),
    HashMap::from([
        ("answer".to_string(),42),
        ("zero".to_string(),0),
        ("big_number".to_string(),10000),
    ]),
    Default::default(),
);

for key in account.keys() {
    println!("{key}");
}
§Performance

In the current implementation, iterating over keys takes O(capacity) time instead of O(len) because it internally visits empty buckets too.

source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

This number is a lower bound; the HashMap<K, V> might be able to hold more, but is guaranteed to be able to hold at least this many.

This method is a direct call to HashMap’s keys().

§Examples
use hashmap_settings::account::Account;
use std::collections::HashMap;
let account = Account::<String,(),()>::new(Default::default(), Default::default(), HashMap::with_capacity(100), Default::default());
assert!(account.capacity() >= 100);
source

pub const fn accounts(&self) -> &Vec<Self>

Return a reference to the Vec of child Accounts

§Examples
use hashmap_settings::account::Account;
let account = Account::<i32,(),()>::new(
    0,
    Default::default(),
    Default::default(),
    vec![
        Account::new(1, true, Default::default(), Default::default()),
        Account::new(2, true, Default::default(), Default::default()),
        Account::new(3, true, Default::default(), Default::default())
    ],
);

assert!(account.accounts() ==
    &vec![
        Account::new(1, true, Default::default(), Default::default()),
        Account::new(2, true, Default::default(), Default::default()),
        Account::new(3, true, Default::default(), Default::default())
    ],
);
source

pub fn accounts_names(&self) -> Vec<&N>

Return a Vec of names of the child Accounts

§Examples
use hashmap_settings::account::Account;
let account = Account::<String,(),()>::new(
    "New Account".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), Default::default())
    ],
);

assert!(account.accounts_names() == vec!["1","2","3"]);
source

pub fn len(&self) -> usize

Returns the number of elements in the Vec of child Accounts, also referred to as its ‘length’.

This method is a direct call to Vec’s len().

§Examples
use hashmap_settings::account::Account;
let account = Account::<i32,(),()>::new(
        Default::default(),
        Default::default(),
        Default::default(),
        vec![
            Account::new(1, Default::default(), Default::default(), Default::default()),
            Account::new(2, Default::default(), Default::default(), Default::default()),
            Account::new(3, Default::default(), Default::default(), Default::default())
        ],
    );
assert_eq!(account.len(), 3);
source

pub fn is_empty(&self) -> bool

Returns true if the Vec of child Accounts contains no elements.

This method is a direct call to Vec’s is_empty().

§Examples
use hashmap_settings::account::{Account,Valid};
let mut account = Account::<String,(),()>::default();
assert!(account.is_empty());

account.push(Account::<String,(),()>::default(), Valid::new_true());
assert!(!account.is_empty());
source

pub fn get_mut_account(&mut self, index: usize) -> Option<&mut Self>

Returns a mutable reference to a child Account

§Examples
 //TODO(Example)
source

pub const fn valid(&self) -> &Valid

Returns a reference to the Account’s Valid.

Check valid

§Examples
use hashmap_settings::account::{Account,Valid};
let account = Account::<(),(),()>::default();
assert_eq!(account.valid(),&Valid::new(true,true,true));
source

pub fn change_valid(&mut self, new_valid: Valid) -> bool

Change Account’s Valid to the provided value.

Returns true if new value is different than the previous one.

This method (along with update_valid) is intended to be used with methods that can make an account invalid to correctly update they values for a future use of fix_valid.

change_valid can make an account invalid if improperly used.

§Examples
use hashmap_settings::account::{Account,Valid};
let mut account = Account::<(),(),()>::default();
assert_eq!(account.valid(),&Valid::new(true,true,true));
assert_eq!(account.change_valid(Valid::new(true,true,true)),false);
assert_eq!(account.change_valid(Valid::new(false,true,true)),true);
assert_eq!(account.valid(),&Valid::new(false,true,true));
source

pub fn rename(&mut self, new_name: N) -> N

Takes a N and updates the name of the Account.

Returns the previous name that the Account had.

§Examples
use hashmap_settings::account::Account;
let mut account = Account::<String,(),()>::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    Default::default()
);
assert_eq!(account.name(), "Old Name");
assert_eq!(account.rename("New Name".to_string()), "Old Name".to_string());
assert_eq!(account.name(), "New Name");
source§

impl<N: PartialEq, K, V> Account<N, K, V>

source

pub fn deep(&self, account_names: &mut Vec<&N>) -> Result<&Self, DeepError>

Returns a reference to a child Account.

deep can be used with other methods that don’t need a &mut self (like get or len) to use those methods on child Accounts

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function.

§Errors

Deep functions can return DeepError’s

§Examples
use std::collections::HashMap;
use hashmap_settings::account::Account;
let account = Account::<String,String,i32>::new(
    "Parent Account".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new(
                "3_2".to_string(),
                true,
                HashMap::from([
                    ("answer".to_string(),42),
                    ("zero".to_string(),0),
                    ("big_number".to_string(),10000),
                ]),
                Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default()),
        ])
    ],
);

assert_eq!(account.deep(&mut vec![&"3_2".to_string(),&"3".to_string()])?.get(&"answer".to_string()), Some(&42));
source

pub fn deep_mut( &mut self, account_names: &mut Vec<&N> ) -> Result<&mut Self, DeepError>

Returns a mutable reference to a child Account.

Consider using deep with methods that don’t need a &mut self, or the respective deep_function for a specific method as deep_mut can make an account invalid

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function.

§Errors

Deep functions can return DeepError’s

§Examples
use std::collections::HashMap;
use hashmap_settings::account::Account;
let mut account = Account::<String,String,i32>::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new(
                "3_2".to_string(),
                true,
                HashMap::from([
                    ("answer".to_string(),42),
                    ("zero".to_string(),0),
                    ("big_number".to_string(),10000),
                ]),
                Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default()),
        ])
    ],
);
assert_eq!(account.deep_mut(&mut vec![&"3_2".to_string(),&"3".to_string()])?.insert("answer".to_string(), 777), Some(42));
assert_eq!(account.deep(&mut vec![&"3_2".to_string(),&"3".to_string()])?.get(&"answer".to_string()), Some(&777));
source§

impl<N, K: Eq + Hash, V> Account<N, K, V>

source

pub fn get(&self, setting_name: &K) -> Option<&V>

Returns the value corresponding to the key.

This method is a direct call to HashMap’s get().

§Examples
use hashmap_settings::account::Account;
let mut account: Account<(),&str,i32> = Default::default();
account.insert("a small number", 42);
assert_eq!(account.get(&"a small number"), Some(&42));
assert_eq!(account.get(&"a big number"), None);
source

pub fn insert(&mut self, setting_name: K, setting_value: V) -> Option<V>

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.

This method is a direct call to HashMap’s insert().

§Examples
use hashmap_settings::account::Account;
let mut account: Account<(),&str,i32> = Default::default();
assert_eq!(account.insert("a small number", 1), None);
assert_eq!(account.hashmap().is_empty(), false);

account.insert("a small number", 2);
assert_eq!(account.insert("a small number", 3), Some(2));
assert!(account.hashmap()[&"a small number"] == 3);
source

pub fn remove(&mut self, setting_to_remove: &K) -> Option<V>

Removes a setting from the map, returning the value at the key if the key was previously in the map.

This method is a direct call to HashMap’s remove().

§Examples
use hashmap_settings::account::Account;
let mut account: Account<(),&str,i32> = Default::default();
assert_eq!(account.insert("a small number", 1), None);
assert_eq!(account.remove(&"a small number"), Some(1));
assert_eq!(account.remove(&"a small number"), None);
source

pub fn contains_key(&self, setting_name: &K) -> bool

Returns true if the Account contains a value for the specified key.

The key may be any borrowed form of the map’s key type, but Hash and PartialEq on the borrowed form must match those for the key type.

This method is a direct call to HashMap’s contains_key() .

§Examples
use hashmap_settings::account::Account;
let mut account: Account<(),&str,i32> = Default::default();
account.insert("a small number", 42);
assert_eq!(account.contains_key(&"a small number"), true);
assert_eq!(account.contains_key(&"a big number"), false);
source§

impl<N, K: Clone + Eq + Hash, V: Clone> Account<N, K, V>

source

pub fn update_setting(&mut self, setting: &K)

Updates a setting with the value its supposed to have.

This function doesn’t return anything, consider using update_setting_returns if a return value is needed.

Use update_vec if you want to update multiple settings.

Use update_all_settings if you want to update all settings.

If an Account is valid this wont do anything.

§Examples
 //TODO(Example)
source

pub fn update_vec(&mut self, settings: &Vec<&K>)

Updates a group of settings with the value they are supposed to have.

If an Account is valid this wont do anything.

Use update_setting if you want to update a single setting.

Use update_all_settings if you want to update all settings.

§Examples
 //TODO(Example)
source

pub fn update_all_settings(&mut self)

Updates all settings in the Account with the value they are supposed to have.

If an Account is valid this wont do anything.

Use update_setting if you want to update a single setting.

Use update_vec if you want to update multiple but not all settings.

§Examples
 //TODO(Example)
source§

impl<N: Eq + Hash, K: Eq + Hash, V: PartialEq> Account<N, K, V>

source

pub fn update_valid(&mut self, valid: Valid)

Updates valid to the values it’s supposed to have.

This method takes a Valid, updating the Account’s Valid accordingly.

This method (along with change_valid) is intended to be used with methods that can make an account invalid to correctly update they values for a future use of fix_valid.

§Examples
 //TODO(Example)
source§

impl<N: PartialEq, K: Clone + Eq + Hash, V: Clone> Account<N, K, V>

source

pub fn deep_change_activity( &mut self, new_active: bool, account_names: &mut Vec<&N> ) -> Result<bool, DeepError>

Takes a bool and changes the value of active of a child Account.

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function. change_activity in this case.

Also updates the settings, contained on the updated account, in all the affected accounts such that they contain the correct values.

§Errors

Deep functions can return DeepError’s

§Examples
use hashmap_settings::account::Account;
let mut account = Account::<String,(),()>::new(
    "New Account".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new("3_2".to_string(), true, Default::default(), Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default())
        ])
    ],
);

assert_eq!(account.deep_change_activity(false,&mut vec![&"3_2".to_string(),&"3".to_string()]), Ok(true));
assert_eq!(account, Account::new(
    "New Account".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new("3_2".to_string(), false, Default::default(), Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default())
        ])
    ],
));
source

pub fn deep_insert( &mut self, setting_name: &K, setting_value: V, account_names: &mut Vec<&N> ) -> Result<Option<V>, DeepError>

Inserts a key-value pair into the map of a child Account.

This will updated the settings of all necessary Accounts so that the parent Account remains valid

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function. insert in this case.

§Errors

Deep functions can return DeepError’s

§Examples
use std::collections::HashMap;
use hashmap_settings::account::Account;
let mut account = Account::<String,String,i32>::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new(
                "3_2".to_string(),
                true,
                HashMap::from([
                    ("answer".to_string(),42),
                    ("zero".to_string(),0),
                    ("big_number".to_string(),10000),
                ]),
                Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default()),
        ])
    ],
);

assert_eq!(account.deep_insert(&"answer".to_string(), 777, &mut vec![&"3_2".to_string(),&"3".to_string()]), Ok(Some(42)));
assert_eq!(account.deep(&mut vec![&"3_2".to_string(),&"3".to_string()])?.get(&"answer".to_string()), Some(&777));
source

pub fn deep_remove( &mut self, setting_to_remove: &K, account_names: &mut Vec<&N> ) -> Result<Option<V>, DeepError>

Removes a setting from the map, returning the value at the key if the key was previously in the map.

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function. remove in this case.

This method is a direct call to HashMap’s remove().

§Errors

Deep functions can return DeepError’s

§Examples
use std::collections::HashMap;
use hashmap_settings::account::Account;
let mut account = Account::<String,String,i32>::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new(
                "3_2".to_string(),
                true,
                HashMap::from([
                    ("answer".to_string(),42),
                    ("zero".to_string(),0),
                    ("big_number".to_string(),10000),
                ]),
                Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default()),
        ])
    ],
);

assert_eq!(account.deep_remove(&"answer".to_string(),&mut vec![&"3_2".to_string(),&"3".to_string()]), Ok(Some(42)));
assert_eq!(account.deep(&mut vec![&"3_2".to_string(),&"3".to_string()])?.get(&"int".to_string()), None);
source§

impl<N, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>

source

pub fn update_setting_returns(&mut self, setting: &K) -> Option<bool>

Updates a setting with the value its supposed to have.

Returns None if the setting isn’t present in the Account or child Accounts. Returns Some(true) if the value of the setting was updated. Returns Some(false) if the value is in the Account but was not updated.

if you don’t need the return value use update_setting as it is faster

If an Account is valid this method never returns Some(true) as this method is used to turn an invalid Account into a valid one.

§Examples
 //TODO(Example)
source§

impl<N: Clone + Eq + Hash + Incrementable + PartialEq, K, V> Account<N, K, V>

source

pub fn deep_rename( &mut self, new_name: &N, account_names: &mut Vec<&N> ) -> Result<N, DeepError>

Takes a &N and updates the name of a child Account.

This can make a Account invalid if the child Account got renamed to the same name as one of it’s siblings.

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function. rename in this case.

§Errors

Deep functions can return DeepError’s

§Examples
use hashmap_settings::account::Account;
let mut account = Account::<String,(),()>::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new("3_2".to_string(), true, Default::default(), Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default())
        ])
    ],
);

assert_eq!(account.deep_rename(&"Cool Name".to_string(),&mut vec![&"3_2".to_string(),&"3".to_string()]), Ok("3_2".to_string()));
assert_eq!(account, Account::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new("Cool Name".to_string(), true, Default::default(), Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default())
        ])
    ],
));
source§

impl<N: Eq + Hash, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>

source

pub fn pop(&mut self, valid: Valid) -> Option<Self>

Removes the last element from the Vec of child Accounts and returns it, or None if it is empty.

Depending on the Valid provided it could make the parent Account invalid. Providing a Valid::new_true() will always result in a valid Account so it is recommended.

This method contains a call to Vec’s pop().

§Examples
use hashmap_settings::account::{Account,Valid};
let mut account = Account::<i32,(),()>::new(
    Default::default(),
    Default::default(),
    Default::default(),
    vec![
        Account::new(1, Default::default(), Default::default(), Default::default()),
        Account::new(2, Default::default(), Default::default(), Default::default()),
        Account::new(3, Default::default(), Default::default(), Default::default())
    ],
);
assert_eq!(account.pop(Valid::new_true()), Some(Account::new(3, Default::default(), Default::default(), Default::default())));
assert!(account ==
    Account::<i32,(),()>::new(
        Default::default(),
        Default::default(),
        Default::default(),
        vec![
            Account::new(1, Default::default(), Default::default(), Default::default()),
            Account::new(2, Default::default(), Default::default(), Default::default())
        ],
    )
)
source

pub fn deep_pop( &mut self, valid: Valid, account_names: &mut Vec<&N> ) -> Result<Option<Self>, DeepError>

Removes the last element from the Vec of child Accounts, from a child Account,and returns it, or None if it is empty.

Depending on the Valid provided it could make the parent Account invalid. Providing a Valid::new_true() will always result in a valid Account so it is recommended.

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function. pop in this case.

§Errors

Deep functions can return DeepError’s

§Examples
use hashmap_settings::account::{Account,Valid};
let mut account = Account::<String,(),()>::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new("3_2".to_string(), true, Default::default(), vec![
                    Account::new("3_2.1".to_string(), true, Default::default(), Default::default()),
                ]),
            Account::new("3_3".to_string(), true, Default::default(), Default::default()),
        ])
    ],
);

assert_eq!(account.deep_pop(Valid::new_true(), &mut vec![&"3_2".to_string(),&"3".to_string()])
    ,Ok(Some(Account::new("3_2.1".to_string(), true, Default::default(), Default::default())))
);


assert_eq!(account ,
    Account::<String,(),()>::new(
        "Old Name".to_string(),
        Default::default(),
        Default::default(),
        vec![
            Account::new("1".to_string(), true, Default::default(), Default::default()),
            Account::new("2".to_string(), true, Default::default(), Default::default()),
            Account::new("3".to_string(), true, Default::default(), vec![
                Account::new("3_1".to_string(), true, Default::default(), Default::default()),
                Account::new("3_2".to_string(), true, Default::default(), Default::default()),
                Account::new("3_3".to_string(), true, Default::default(), Default::default()),
            ])
        ],
    )
);

source§

impl<N: Clone + Eq + Hash + Incrementable, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>

source

pub fn new( name: N, active: bool, settings: HashMap<K, V>, accounts: Vec<Self> ) -> Self

Creates a new valid account

This lets you create an Account that is sure to be fully valid including it’s child Accounts or an error is returned.

It’s recommend that parent Accounts are made with new_valid but child Accounts are made with with new to avoid repeated validity checks.

§Examples
use hashmap_settings::account::Account;
let account = Account::<String,(),()>::new(
    "New Account".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), Default::default())
    ],
);
assert_eq!(account, Account::<String,(),()>::new(
    "New Account".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), Default::default())
    ],
));
source

pub fn fix_valid(&mut self, valid: Valid)

Makes an invalid Account valid

This method fixes the Account according to the specified Valid Account to make it valid

This method is called by new when an Account is created.

§Examples
 //TODO(Example)
source§

impl<N: Clone + Eq + Hash + Incrementable + PartialEq, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>

source

pub fn push(&mut self, account: Self, valid: Valid)

Appends an Account to the back of the Vec of child Accounts.

This child Account settings will be added to the settings of the main Account that push was called on.

If the inserted Account is inactive the new settings won’t be updated.

Depending on the Valid provided it could make the parent Account invalid. Providing a Valid::new_true() will always result in a valid Account so it is recommended.

§Panics

Panics if the new capacity exceeds isize::MAX bytes.

§Examples
use hashmap_settings::account::{Account,Valid};
let mut account = Account::<i32,(),()>::new(
    Default::default(),
    Default::default(),
    Default::default(),
    vec![
        Account::new(1, Default::default(), Default::default(), Default::default()),
        Account::new(2, Default::default(), Default::default(), Default::default())
    ],
);
account.push(Account::new(3, Default::default(), Default::default(), Default::default()), Valid::new_true());
assert!(account ==
    Account::new(
        Default::default(),
        Default::default(),
        Default::default(),
        vec![
            Account::new(1, Default::default(), Default::default(), Default::default()),
            Account::new(2, Default::default(), Default::default(), Default::default()),
            Account::new(3, Default::default(), Default::default(), Default::default())
        ],
    )
);
source

pub fn deep_push( &mut self, account: Self, valid: Valid, account_names: &mut Vec<&N> ) -> Option<DeepError>

Appends an Account to the back of the Vec of child Accounts of a child Account.

This will updated the settings of all necessary Accounts so that the parent Account remains valid

Part of the deep functions group that accept a Vec of &N to identify the child Account to run the function. push in this case.

Depending on the Valid provided it could make the parent Account invalid. Providing a Valid::new_true() will always result in a valid Account so it is recommended.

§Errors

Deep functions can return DeepError’s

§Examples
use hashmap_settings::account::{Account,Valid};
let mut account = Account::<String,(),()>::new(
    "Old Name".to_string(),
    Default::default(),
    Default::default(),
    vec![
        Account::new("1".to_string(), true, Default::default(), Default::default()),
        Account::new("2".to_string(), true, Default::default(), Default::default()),
        Account::new("3".to_string(), true, Default::default(), vec![
            Account::new("3_1".to_string(), true, Default::default(), Default::default()),
            Account::new("3_2".to_string(), true, Default::default(), Default::default()),
            Account::new("3_3".to_string(), true, Default::default(), Default::default()),
        ])
    ],
);

assert_eq!(account.deep_push(
    Account::new("3_2.1".to_string(), true, Default::default(), Default::default()),
    Valid::new_true(),
    &mut vec![&"3_2".to_string(),&"3".to_string()])
, None);


assert_eq!(account ,
    Account::<String,(),()>::new(
        "Old Name".to_string(),
        Default::default(),
        Default::default(),
        vec![
            Account::new("1".to_string(), true, Default::default(), Default::default()),
            Account::new("2".to_string(), true, Default::default(), Default::default()),
            Account::new("3".to_string(), true, Default::default(), vec![
                Account::new("3_1".to_string(), true, Default::default(), Default::default()),
                Account::new("3_2".to_string(), true, Default::default(), vec![
                    Account::new("3_2.1".to_string(), true, Default::default(), Default::default()),
                ]),
                Account::new("3_3".to_string(), true, Default::default(), Default::default()),
            ])
        ],
    )
);

Trait Implementations§

source§

impl<N: Clone, K: Clone, V: Clone> Clone for Account<N, K, V>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<N: Debug, K: Debug, V: Debug> Debug for Account<N, K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<N: Default, K, V> Default for Account<N, K, V>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<N: PartialEq, K: Eq + Hash, V: PartialEq> PartialEq for Account<N, K, V>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<N: Setting + Clone + Debug + PartialEq, K: Setting + Clone + Debug + Eq + Hash, V: Setting + Clone + Debug + PartialEq> Setting for Account<N, K, V>

source§

fn stg(self) -> Stg
where Self: Setting + Sized,

turns a type implementing Setting into a Stg Read more

Auto Trait Implementations§

§

impl<N, K, V> RefUnwindSafe for Account<N, K, V>

§

impl<N, K, V> Send for Account<N, K, V>
where K: Send, N: Send, V: Send,

§

impl<N, K, V> Sync for Account<N, K, V>
where K: Sync, N: Sync, V: Sync,

§

impl<N, K, V> Unpin for Account<N, K, V>
where K: Unpin, N: Unpin, V: Unpin,

§

impl<N, K, V> UnwindSafe for Account<N, K, V>
where K: UnwindSafe, N: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> DynEq for T
where T: Any + PartialEq,

source§

fn as_any(&self) -> &(dyn Any + 'static)

source§

fn dyn_eq(&self, other: &(dyn DynEq + 'static)) -> bool

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.