优化配置数据访问方法

This commit is contained in:
shenjack 2024-02-25 17:29:43 +08:00
parent 7b8db8136b
commit ae181425e0
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -1,8 +1,8 @@
use pyo3::prelude::*; use pyo3::prelude::*;
use rust_socketio::asynchronous::Client; use rust_socketio::asynchronous::Client;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use tracing::{debug, info, warn};
use toml::Value as TomlValue; use toml::Value as TomlValue;
use tracing::{debug, info, warn};
use crate::client::{delete_message, send_message, IcalinguaStatus}; use crate::client::{delete_message, send_message, IcalinguaStatus};
use crate::data_struct::messages::{ use crate::data_struct::messages::{
@ -220,7 +220,6 @@ impl ConfigRequestPy {
pub fn py_new(path: String) -> Self { Self { path } } pub fn py_new(path: String) -> Self { Self { path } }
} }
#[derive(Clone)] #[derive(Clone)]
#[pyclass] #[pyclass]
#[pyo3(name = "ConfigData")] #[pyo3(name = "ConfigData")]
@ -232,44 +231,27 @@ pub struct ConfigDataPy {
impl ConfigDataPy { impl ConfigDataPy {
pub fn __getitem__(self_: PyRef<'_, Self>, key: String) -> Option<Py<PyAny>> { pub fn __getitem__(self_: PyRef<'_, Self>, key: String) -> Option<Py<PyAny>> {
match self_.data.get(&key) { match self_.data.get(&key) {
Some(value) => { Some(value) => match value {
match value { TomlValue::String(s) => Some(s.into_py(self_.py())),
TomlValue::String(s) => { TomlValue::Integer(i) => Some(i.into_py(self_.py())),
let py_value = s.into_py(self_.py()); TomlValue::Float(f) => Some(f.into_py(self_.py())),
Some(py_value) TomlValue::Boolean(b) => Some(b.into_py(self_.py())),
},
TomlValue::Integer(i) => {
let py_value = i.into_py(self_.py());
Some(py_value)
},
TomlValue::Float(f) => {
let py_value = f.into_py(self_.py());
Some(py_value)
},
TomlValue::Boolean(b) => {
let py_value = b.into_py(self_.py());
Some(py_value)
},
TomlValue::Array(a) => { TomlValue::Array(a) => {
let new_self = Self::new(TomlValue::Array(a.clone())); let new_self = Self::new(TomlValue::Array(a.clone()));
let py_value = new_self.into_py(self_.py()); let py_value = new_self.into_py(self_.py());
Some(py_value) Some(py_value)
}, }
TomlValue::Table(t) => { TomlValue::Table(t) => {
let new_self = Self::new(TomlValue::Table(t.clone())); let new_self = Self::new(TomlValue::Table(t.clone()));
let py_value = new_self.into_py(self_.py()); let py_value = new_self.into_py(self_.py());
Some(py_value) Some(py_value)
}, }
_ => None, _ => None,
} },
}
None => None, None => None,
} }
} }
pub fn have_key(&self, key: String) -> bool { pub fn have_key(&self, key: String) -> bool { self.data.get(&key).is_some() }
self.data.get(&key).is_some()
}
} }
impl ConfigDataPy { impl ConfigDataPy {