x-common-lib 0.1.8

DXMesh rust dxc develop library
Documentation
use crate::utils::file_utils;
use toml::Table;

pub struct Config {
    config_table: Table,
    config_content: String,
}

impl Config {
    pub async fn load(file_path: &str) -> Self {
        let result = file_utils::read_file_as_string(file_path).await;
        let content = result.expect("读取配置文件 dxmesh.toml 失败!");
        let table: Table = toml::from_str(&content).expect("解析配置文件失败!");
        Config {
            config_table: table,
            config_content: content,
        }
    }

    pub fn load_from_string(content: &str) -> Self {
        let table: Table = toml::from_str(&content).expect("解析配置文件失败!");
        Config {
            config_table: table,
            config_content: String::default(),
        }
    }

    pub fn get_config_content(&self) -> &str {
        &self.config_content.as_str()
    }

    #[allow(dead_code)]
    pub fn has_key(&self, key: &str) -> bool {
        self.config_table.contains_key(key)
    }

    pub fn get_table(&self, table_name: &str) -> Option<&Table> {
        let result = self.config_table.get(table_name);

        if result.is_none() {
            return None;
        }

        return result.unwrap().as_table();
    }

    pub fn get_str(&self, table_name: &str, key: &str) -> Option<&str> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let table = table.unwrap();

        let value = table.get(key);

        if value.is_none() {
            return None;
        }
        value.unwrap().as_str()
    }

    pub fn get_bool(&self, table_name: &str, key: &str) -> Option<bool> {

        let table = if table_name.is_empty() {
            &self.config_table
        } else {
            let table = self.get_table(table_name);

            if table.is_none() {
                return None;
            }

            table.unwrap()
        };

        let value = table.get(key);

        if value.is_none() {
            return None;
        }
        value.unwrap().as_bool()
    }

    #[allow(dead_code)]
    pub fn get_str_array(&self, table_name: &str, key: &str) -> Vec<&str> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return vec![];
        }

        let result = table.unwrap().get(key);

        if result.is_none() {
            return vec![];
        }

        let result = result.unwrap();

        if result.is_array() {
            let mut vec: Vec<&str> = vec![];
            let arr = result.as_array().unwrap();
            for ele in arr {
                vec.push(ele.as_str().unwrap())
            }
            return vec;
        }
        vec![result.as_str().unwrap()]
    }

    pub fn get_i32(&self, table_name: &str, key: &str) -> Option<i32> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap() as i32)
                } else {
                    let str_val = value.as_str().unwrap();
                    let i32_val = str_val.parse::<i32>();

                    return match i32_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }

    pub fn get_i64(&self, table_name: &str, key: &str) -> Option<i64> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap())
                } else {
                    let str_val = value.as_str().unwrap();
                    let i64_val = str_val.parse::<i64>();
                    return match i64_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }

    pub fn get_f32(&self, table_name: &str, key: &str) -> Option<f32> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap() as f32)
                } else {
                    let str_val = value.as_str().unwrap();
                    let f32_val = str_val.parse::<f32>();
                    return match f32_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }

    pub fn get_f64(&self, table_name: &str, key: &str) -> Option<f64> {
        let table = self.get_table(table_name);

        if table.is_none() {
            return None;
        }

        let result = table.unwrap().get(key);
        match result {
            Some(value) => {
                if value.is_integer() {
                    Some(value.as_integer().unwrap() as f64)
                } else {
                    let str_val = value.as_str().unwrap();
                    let f64_val = str_val.parse::<f64>();
                    return match f64_val {
                        Ok(val) => Some(val),
                        Err(_) => None,
                    };
                }
            }
            None => None,
        }
    }
}