Files
huike-back/huike_push_info/src/lib.rs
T

81 lines
2.6 KiB
Rust

//! Accessors for domain keys stored in [`htycommons::models::PushInfo::extra`].
#![forbid(unsafe_code)]
use htycommons::models::PushInfo;
use serde_json::{Map, Value};
fn value_to_opt_string(v: &Value) -> Option<String> {
match v {
Value::Null => None,
Value::String(s) => Some(s.clone()),
Value::Number(n) => Some(n.to_string()),
Value::Bool(b) => Some(b.to_string()),
Value::Array(_) | Value::Object(_) => Some(v.to_string()),
}
}
fn map_get_str(map: &Map<String, Value>, key: &str) -> Option<String> {
map.get(key).and_then(value_to_opt_string)
}
/// Read business / template keys from the flattened `extra` map (Jsonb round-trip compatible).
pub trait PushInfoExt {
fn extra_map(&self) -> &Map<String, Value>;
fn daka_id(&self) -> Option<String> {
map_get_str(self.extra_map(), "daka_id")
}
fn jihua_id(&self) -> Option<String> {
map_get_str(self.extra_map(), "jihua_id")
}
fn lianxi_id(&self) -> Option<String> {
map_get_str(self.extra_map(), "lianxi_id")
}
fn piyue_id(&self) -> Option<String> {
map_get_str(self.extra_map(), "piyue_id")
}
fn qumu_name(&self) -> Option<String> {
map_get_str(self.extra_map(), "qumu_name")
}
fn qumu_section_name(&self) -> Option<String> {
map_get_str(self.extra_map(), "qumu_section_name")
}
fn resource_note_group_id(&self) -> Option<String> {
map_get_str(self.extra_map(), "resource_note_group_id")
}
fn remark(&self) -> Option<String> {
map_get_str(self.extra_map(), "remark")
}
fn first(&self) -> Option<String> {
map_get_str(self.extra_map(), "first")
}
fn reject_reason(&self) -> Option<String> {
map_get_str(self.extra_map(), "reject_reason")
}
fn clazz_id(&self) -> Option<String> {
map_get_str(self.extra_map(), "clazz_id").or_else(|| map_get_str(self.extra_map(), "kecheng_id"))
}
fn clazz_name(&self) -> Option<String> {
map_get_str(self.extra_map(), "clazz_name")
.or_else(|| map_get_str(self.extra_map(), "kecheng_name"))
}
fn student_name(&self) -> Option<String> {
map_get_str(self.extra_map(), "student_name")
}
fn teacher_name(&self) -> Option<String> {
map_get_str(self.extra_map(), "teacher_name")
}
fn start_from(&self) -> Option<String> {
map_get_str(self.extra_map(), "start_from")
}
fn end_by(&self) -> Option<String> {
map_get_str(self.extra_map(), "end_by")
}
}
impl PushInfoExt for PushInfo {
fn extra_map(&self) -> &Map<String, Value> {
&self.extra
}
}