use serde_json::{Map, Value as JsonValue};
pub trait JsonApplicable {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool;
}
impl JsonApplicable for String {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool {
if !self.is_empty() {
if let JsonValue::Object(map) = json {
map.insert(key.to_string(), JsonValue::String(self.clone()));
true
} else {
false
}
} else {
false
}
}
}
impl JsonApplicable for &str {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool {
if !self.is_empty() {
if let JsonValue::Object(map) = json {
map.insert(key.to_string(), JsonValue::String((*self).to_string()));
true
} else {
false
}
} else {
false
}
}
}
impl JsonApplicable for Option<String> {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool {
if let Some(value) = self {
value.apply_json(json, key)
} else {
false
}
}
}
impl JsonApplicable for Option<&str> {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool {
if let Some(value) = self {
value.apply_json(json, key)
} else {
false
}
}
}
impl JsonApplicable for u16 {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool {
if *self > 0 {
if let JsonValue::Object(map) = json {
map.insert(key.to_string(), JsonValue::Number((*self).into()));
true
} else {
false
}
} else {
false
}
}
}
impl JsonApplicable for u32 {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool {
if *self > 0 {
if let JsonValue::Object(map) = json {
map.insert(key.to_string(), JsonValue::Number((*self).into()));
true
} else {
false
}
} else {
false
}
}
}
pub trait TriboolExt<T> {
fn define(&self, default: Option<T>) -> Option<T>
where
T: Clone;
fn apply_to_json(&self, obj: &mut Map<String, JsonValue>, key: &str) -> bool
where
T: Into<JsonValue> + Clone;
fn apply_to_json_value(&self, obj: &mut JsonValue, key: &str) -> bool
where
T: Into<JsonValue> + Clone;
}
pub trait OptionSetExt<T> {
fn set_if_some(&mut self, src: Option<T>);
}
impl<T> OptionSetExt<T> for Option<T> {
fn set_if_some(&mut self, src: Option<T>) {
if let Some(value) = src {
*self = Some(value);
}
}
}
impl<T> TriboolExt<T> for Option<T> {
fn define(&self, default: Option<T>) -> Option<T>
where
T: Clone,
{
match self {
Some(value) => Some(value.clone()),
None => default,
}
}
fn apply_to_json(&self, obj: &mut Map<String, JsonValue>, key: &str) -> bool
where
T: Into<JsonValue> + Clone,
{
match self {
Some(value) => {
let json_value = value.clone().into();
obj.insert(key.to_string(), json_value);
true
}
None => false,
}
}
fn apply_to_json_value(&self, obj: &mut JsonValue, key: &str) -> bool
where
T: Into<JsonValue> + Clone,
{
if let JsonValue::Object(map) = obj {
self.apply_to_json(map, key)
} else {
false
}
}
}
impl JsonApplicable for Option<bool> {
fn apply_json(&self, json: &mut JsonValue, key: &str) -> bool {
if let Some(value) = self {
if let JsonValue::Object(map) = json {
map.insert(key.to_string(), JsonValue::Bool(*value));
true
} else {
false
}
} else {
false
}
}
}
pub trait BoolTriboolExt: TriboolExt<bool> {
fn get_or(&self, default: bool) -> bool;
fn is_undef(&self) -> bool;
fn get_str(&self) -> String;
}
impl BoolTriboolExt for Option<bool> {
fn get_or(&self, default: bool) -> bool {
self.unwrap_or(default)
}
fn is_undef(&self) -> bool {
self.is_none()
}
fn get_str(&self) -> String {
match self {
Some(true) => "true".to_string(),
Some(false) => "false".to_string(),
None => String::new(),
}
}
}