use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct UserProfile {
pub id: String,
pub email: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub attributes: Option<UserAttributes>,
#[serde(skip_serializing_if = "Option::is_none")]
pub preferences: Option<UserPreferences>,
}
impl UserProfile {
pub fn new(id: String, email: String) -> Self {
Self {
id,
email: email.to_ascii_lowercase(),
attributes: None,
preferences: None,
}
}
pub fn set_id(&mut self, id: String) {
self.id = id;
}
pub fn get_id(&self) -> String {
self.id.clone()
}
pub fn set_email(&mut self, email: String) {
self.email = email.to_ascii_lowercase();
}
pub fn get_email(&self) -> &str {
&self.email
}
pub fn set_attributes(&mut self, attributes: Option<UserAttributes>) {
self.attributes = attributes;
}
pub fn get_attributes(&self) -> Option<&UserAttributes> {
self.attributes.as_ref()
}
pub fn set_preferences(&mut self, preferences: Option<UserPreferences>) {
self.preferences = preferences;
}
pub fn get_preferences(&self) -> Option<&UserPreferences> {
self.preferences.as_ref()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct UserAttributes {
#[serde(skip_serializing_if = "Option::is_none")]
pub first_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_name: Option<String>,
#[serde(flatten, default)]
pub extra: Map<String, Value>,
}
impl UserAttributes {
pub fn new() -> Self {
Self {
first_name: None,
last_name: None,
extra: Default::default(),
}
}
pub fn set_first_name(&mut self, first_name: String) {
self.first_name = Some(first_name);
}
pub fn get_first_name(&self) -> Option<&String> {
self.first_name.as_ref()
}
pub fn set_last_name(&mut self, last_name: String) {
self.last_name = Some(last_name);
}
pub fn get_last_name(&self) -> Option<&String> {
self.last_name.as_ref()
}
pub fn set_extra(&mut self, key: impl Into<String>, value: Value) {
self.extra.insert(key.into(), value);
}
pub fn get_extra(&self, key: &str) -> Option<&Value> {
self.extra.get(key)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct UserPreferences {
pub newsletter_opt_in: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub language: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<String>,
#[serde(flatten, default)]
pub extra: Map<String, Value>,
}
impl UserPreferences {
pub fn new() -> Self {
Self {
newsletter_opt_in: false,
language: None,
currency: None,
extra: Default::default(),
}
}
pub fn set_newsletter_opt_in(&mut self, opt_in: bool) {
self.newsletter_opt_in = opt_in;
}
pub fn get_newsletter_opt_in(&self) -> bool {
self.newsletter_opt_in
}
pub fn set_language(&mut self, language: String) {
self.language = Some(language);
}
pub fn get_language(&self) -> Option<&String> {
self.language.as_ref()
}
pub fn set_currency(&mut self, currency: String) {
self.currency = Some(currency);
}
pub fn get_currency(&self) -> Option<&String> {
self.currency.as_ref()
}
pub fn set_extra(&mut self, key: impl Into<String>, value: Value) {
self.extra.insert(key.into(), value);
}
pub fn get_extra(&self, key: &str) -> Option<&Value> {
self.extra.get(key)
}
}