use serde::{Deserialize, Serialize};
use crate::utils::error::SCIMError;
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct EnterpriseUser {
#[serde(rename = "employeeNumber", skip_serializing_if = "Option::is_none")]
pub employee_number: Option<String>,
#[serde(rename = "costCenter", skip_serializing_if = "Option::is_none")]
pub cost_center: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub organization: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub division: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub department: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub manager: Option<Manager>,
}
impl TryFrom<&str> for EnterpriseUser {
type Error = SCIMError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
serde_json::from_str(value).map_err(SCIMError::DeserializationError)
}
}
impl EnterpriseUser {
pub fn validate(&self) -> Result<(), SCIMError> {
if self.employee_number.is_none() {
return Err(SCIMError::MissingRequiredField(
"employee_number".to_string(),
));
}
if self.cost_center.is_none() {
return Err(SCIMError::MissingRequiredField("cost_center".to_string()));
}
if self.organization.is_none() {
return Err(SCIMError::MissingRequiredField("organization".to_string()));
}
if self.division.is_none() {
return Err(SCIMError::MissingRequiredField("division".to_string()));
}
if self.department.is_none() {
return Err(SCIMError::MissingRequiredField("department".to_string()));
}
if self.manager.is_none() {
return Err(SCIMError::MissingRequiredField("manager".to_string()));
}
Ok(())
}
pub fn serialize(&self) -> Result<String, SCIMError> {
serde_json::to_string(&self).map_err(SCIMError::SerializationError)
}
pub fn deserialize(json: &str) -> Result<Self, SCIMError> {
serde_json::from_str(json).map_err(SCIMError::DeserializationError)
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Manager {
pub value: Option<String>,
#[serde(rename = "$ref")]
pub r#ref: Option<String>,
#[serde(rename = "displayName")]
pub display_name: Option<String>,
}