Struct hashmap_settings::Account
source · 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:
-
name: Name of type
N
,
§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.
-
name
: Get an account’s name -
rename
: Rename anAccount
-
deep_rename
: Rename a childAccount
§Active
If a child Account
is inactive it’s settings will be ignore by the parent Account
.
-
active
: Get an account’s activity state -
change_activity
: Change the activity -
deep_change_activity
: Change the activity of one of the childAccounts
§Settings
A HashMap
holding Settings. Contains all the settings present in the
child Accounts but can contain settings that aren’t in them.
-
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
: Returnstrue
if theAccount
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 childAccounts
-
accounts
: Return aVec
of names of the childAccounts
. -
len
: Returns the number of elements in theVec
. -
is_empty
: Returnstrue
if theVec
contains no elements. -
push
: Appends anAccount
to the back of theVec
. -
deep_push
: Appends anAccount
to the back of theVec
of a childAccount
. -
pop
: Removes the last element from a vector and returns it, orNone
if it is empty. -
deep_pop
: Removes the last element from a vector of a childAccount
§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 childrenAccounts
have duplicated names. -
settings: An
Account
is invalid if it doesn’t contain all settings present in it’s childrenAccounts
. -
accounts: An
Account
is invalid if it’s childrenAccounts
are themselves invalid.
If all the fields are true then the Account
is valid.
An Account can be temporary made invalid for:
-
using deep_mut for something that isn’t covered by the other deep functions.
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>
impl<N, K, V> Account<N, K, V>
sourcepub fn new_unchecked(
name: N,
active: bool,
settings: HashMap<K, V>,
accounts: Vec<Self>,
valid: Valid
) -> Self
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)
);
sourcepub const fn name(&self) -> &N
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");
sourcepub const fn active(&self) -> bool
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());
sourcepub fn change_activity(&mut self, new_active: bool) -> bool
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());
sourcepub const fn hashmap(&self) -> &HashMap<K, V>
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),
])
);
sourcepub fn keys(&self) -> Keys<'_, K, V>
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.
sourcepub fn capacity(&self) -> usize
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);
sourcepub const fn accounts(&self) -> &Vec<Self>
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())
],
);
sourcepub fn accounts_names(&self) -> Vec<&N>
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"]);
sourcepub fn len(&self) -> usize
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);
sourcepub fn is_empty(&self) -> bool
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());
sourcepub fn get_mut_account(&mut self, index: usize) -> Option<&mut Self>
pub fn get_mut_account(&mut self, index: usize) -> Option<&mut Self>
sourcepub fn change_valid(&mut self, new_valid: Valid) -> bool
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));
sourcepub fn rename(&mut self, new_name: N) -> N
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>
impl<N: PartialEq, K, V> Account<N, K, V>
sourcepub fn deep(&self, account_names: &mut Vec<&N>) -> Result<&Self, DeepError>
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 Account
s
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));
sourcepub fn deep_mut(
&mut self,
account_names: &mut Vec<&N>
) -> Result<&mut Self, DeepError>
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>
impl<N, K: Eq + Hash, V> Account<N, K, V>
sourcepub fn get(&self, setting_name: &K) -> Option<&V>
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);
sourcepub fn insert(&mut self, setting_name: K, setting_value: V) -> Option<V>
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);
sourcepub fn remove(&mut self, setting_to_remove: &K) -> Option<V>
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);
sourcepub fn contains_key(&self, setting_name: &K) -> bool
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>
impl<N, K: Clone + Eq + Hash, V: Clone> Account<N, K, V>
sourcepub fn update_setting(&mut self, setting: &K)
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)
sourcepub fn update_vec(&mut self, settings: &Vec<&K>)
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)
sourcepub fn update_all_settings(&mut self)
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>
impl<N: Eq + Hash, K: Eq + Hash, V: PartialEq> Account<N, K, V>
sourcepub fn update_valid(&mut self, valid: Valid)
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>
impl<N: PartialEq, K: Clone + Eq + Hash, V: Clone> Account<N, K, V>
sourcepub fn deep_change_activity(
&mut self,
new_active: bool,
account_names: &mut Vec<&N>
) -> Result<bool, DeepError>
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())
])
],
));
sourcepub fn deep_insert(
&mut self,
setting_name: &K,
setting_value: V,
account_names: &mut Vec<&N>
) -> Result<Option<V>, DeepError>
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));
sourcepub fn deep_remove(
&mut self,
setting_to_remove: &K,
account_names: &mut Vec<&N>
) -> Result<Option<V>, DeepError>
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>
impl<N, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>
sourcepub fn update_setting_returns(&mut self, setting: &K) -> Option<bool>
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>
impl<N: Clone + Eq + Hash + Incrementable + PartialEq, K, V> Account<N, K, V>
sourcepub fn deep_rename(
&mut self,
new_name: &N,
account_names: &mut Vec<&N>
) -> Result<N, DeepError>
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>
impl<N: Eq + Hash, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>
sourcepub fn pop(&mut self, valid: Valid) -> Option<Self>
pub fn pop(&mut self, valid: Valid) -> Option<Self>
Removes the last element from the Vec
of child Account
s 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())
],
)
)
sourcepub fn deep_pop(
&mut self,
valid: Valid,
account_names: &mut Vec<&N>
) -> Result<Option<Self>, DeepError>
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 Account
s, 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>
impl<N: Clone + Eq + Hash + Incrementable, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>
sourcepub fn new(
name: N,
active: bool,
settings: HashMap<K, V>,
accounts: Vec<Self>
) -> Self
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§impl<N: Clone + Eq + Hash + Incrementable + PartialEq, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>
impl<N: Clone + Eq + Hash + Incrementable + PartialEq, K: Clone + Eq + Hash, V: Clone + PartialEq> Account<N, K, V>
sourcepub fn push(&mut self, account: Self, valid: Valid)
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())
],
)
);
sourcepub fn deep_push(
&mut self,
account: Self,
valid: Valid,
account_names: &mut Vec<&N>
) -> Option<DeepError>
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()),
])
],
)
);