cal_core/
routing.rs

1use std::collections::HashMap;
2use serde::{Deserialize, Deserializer, Serialize};
3use serde_json::Value;
4use crate::RecordReference;
5
6#[derive(Serialize, Deserialize, Clone)]
7#[serde(rename_all = "camelCase")]
8pub struct Proxy {
9    #[serde(deserialize_with = "object_id_as_string", rename = "_id")]
10    pub id:String,
11    pub username: Option<String>,
12    pub password: Option<String>,
13    pub port: u16,
14    pub priority: u8,
15    pub inbound: bool,
16    pub outbound: bool,
17    pub active: bool,
18    pub ip: String,
19    pub proxy_type: String,
20    pub zone: String,
21    pub description: String,
22    pub country_code:String,
23    #[serde(default)]
24    pub headers: HashMap<String, String>,
25}
26
27
28#[derive(Serialize, Deserialize, Clone)]
29#[serde(rename_all = "camelCase")]
30pub struct Route {
31    #[serde(deserialize_with = "object_id_as_string", rename = "_id")]
32    pub id:String,
33    pub caller:String,
34    pub called:String,
35    pub diversion: Option<String>,
36    pub paid: Option<String>,
37    pub from_domain:Option<String>,
38    pub obp: RecordReference,
39    #[serde(default)]
40    pub headers: HashMap<String, String>,
41}
42pub fn object_id_as_string<'de, D>(deserializer: D) -> Result<String, D::Error>
43where
44    D: Deserializer<'de>,
45{
46    let value = Value::deserialize(deserializer)?;
47    if value.is_string() {
48        Ok(value.as_str().unwrap().to_string())
49    } else {
50        match value.get("$oid") {
51            Some(v) => Ok(v.as_str().unwrap().to_string()),
52            None => Err(serde::de::Error::custom("Oid is required")),
53        }
54    }
55}