use crate::{Object, Value};
use std::{
collections::BTreeMap,
ops::{Deref, DerefMut},
};
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct User(BTreeMap<String, Value>);
impl Default for User {
fn default() -> Self {
Self::new()
}
}
impl Object for User {
fn into_parts(self) -> (sys::Value, BTreeMap<String, Value>) {
(unsafe { sys::value_new_object() }, self.0)
}
}
impl Deref for User {
type Target = BTreeMap<String, Value>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for User {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl User {
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn new() -> Self {
Self(BTreeMap::new())
}
pub fn insert<S: Into<String>, V: Into<Value>>(&mut self, key: S, value: V) {
self.deref_mut().insert(key.into(), value.into());
}
pub fn set(self) {
let user = self.into_raw();
unsafe { sys::set_user(user) }
}
}
#[test]
fn user() {
User::new().set();
let mut user = User::new();
user.insert("test", "test");
user.set()
}