ruwebframe 0.1.1

A brief description of your crate.
Documentation
use crate::rubase::{BaseEntity, ruentity};
use heck::ToSnakeCase;
use rbatis::rbatis_codegen::ops::AsProxy;
use rbatis::rbdc::Uuid;
use serde::{Deserialize, Serialize};
use std::path::Path;

#[derive(Debug, Default, Deserialize, Serialize, Clone)]
pub struct StructInfo {
    pub pathFile: String,
    pub pathPkg: String,
    pub structName: String,
    pub structPkg: String,

    pub ifSingle: bool,
    pub ifDefault: bool, // new fn is default
    pub structIniFile: String,
    pub existIniFile: bool,

    pub single_bean_name: String,
    pub modFile: String,

    pub ifExistsInitMod: bool,
    pub ifExistsInitUse: bool,
}

impl BaseEntity for StructInfo {}

impl StructInfo {
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }
    pub fn default(structName: String, pathPkg: String, pathFile: String, ifSingle: bool) -> Self {
        Self {
            ..Default::default()
        }
    }
    pub fn set_struct_name(&mut self, name: String) {
        self.structName = name;
        self.single_bean_name = self.build_single_bean_name();
    }
    pub fn set_path_pkg(&mut self, pkg: String) {
        self.pathPkg = pkg;
    }
    pub fn set_path_file(&mut self, file: String) {
        self.pathFile = file;
    }
    pub fn set_if_single(&mut self, single: bool) {
        self.ifSingle = single;
    }
    pub fn build_struct_ini_file(&self) -> String {
        let mut ret = String::new();
        ret.push_str(self.structName.to_lowercase().as_str());

        ret.push_str("_init.rs");

        ret
    }
    pub fn set_struct_ini_file(&mut self, ini: String) {
        self.structIniFile = ini;
        self.existIniFile = self.check_ini_file_exist();
    }
    pub fn get_struct_ini_file(&self) -> &str {
        &self.structIniFile
    }

    pub fn check_ini_file_exist(&self) -> bool {
        let ini_path = self.build_struct_ini_pathfile(); // 例如: ./src/rupage/page/pageresult_init.rs
        println!("ini_path:{}", ini_path);
        Path::new(&ini_path).exists() // 检查文件是否存在
    }
    //./src\\rupage\\page\\pageresult.rs=>./src\\rupage\\page\\ pageresult_init.rs
    pub fn build_struct_ini_pathfile(&self) -> String {
        let mut p = self.get_ini_path() + "/";
        p.push_str(self.structIniFile.as_str());

        p
    }
    pub fn get_ini_path(&self) -> String {
        // 方式1:使用标准库 std::path

        let p = Path::new(self.pathFile.as_str());
        let dir = p.parent(); // ./src/rupage/page
        let stem = p.file_stem(); // pageresult (去扩展名)
        let ext = p.extension(); // rs
        dir.unwrap().to_string_lossy().to_string()
    }

    pub fn set_ini_file_exist(&mut self, exist: bool) {
        self.existIniFile = exist;
    }

    pub fn build_single_bean_name(&self) -> String {
        // 检查文件是否存在
        let mut ret = String::new();
        ret.push_str(self.structPkg.as_str());
        ret.push_str(":");
        ret.push_str(self.structName.as_str());

        let uuid = Uuid::new();
        ret.push_str(":");
        ret.push_str(&uuid.to_string());
        if self.ifSingle {
            ret.push_str(":single");
        }
        ret
    }
    // "pathPkg": "./src\\database",   "pathFile": "./src\\database\\database.rs",
    pub fn build_struct_pkg(&self) -> Option<String> {
        let mut path_pkg = self.pathPkg.as_str().trim_start_matches("./src\\");
        path_pkg = path_pkg.trim_start_matches(".\\src\\");
        let pkg = path_pkg.replace("\\", "::") ;
        let path_file = Path::new(&self.pathFile).file_prefix()?;         //./src\\rudomain\\rudb\\dbdao
        Some(format!("{}::{}", pkg.as_str(), path_file.to_string_lossy(),))
    }
    pub fn set_struct_pkg(&mut self, pkg: String) {
        self.structPkg = pkg;
    }

    pub fn build_struct_mod_file(&self) -> String {
        let mut p = self.get_ini_path() + "/";
        p.push_str("mod.rs");

        p
    }
    
    pub fn struct_ini_pkg(&self) -> String {
         
        self.structIniFile.as_str().replace(".rs", "")  
    }
    pub fn init_name(&self) -> String {
        self.structName.to_lowercase()+"_init"
    }
    pub fn parse_mod_content(& mut self, content: &str) -> String {
        let mut c=content.trim().to_string();
        let lines = content.split("\n");
        let init_name=self.init_name();
        for line in lines {
             if line.contains("mod " ) && line.contains(init_name.as_str()) {
                 self.ifExistsInitMod=true;
             }
            if line.contains("use " ) && line.contains(init_name.as_str()) {
                self.ifExistsInitUse=true;
            }
        }
        if !self.ifExistsInitMod {
            c.push_str("\n  pub mod ");
            c.push_str(&init_name);
            c.push_str(";");
        }
        if !self.ifExistsInitUse {
            c.push_str("\n  pub use ");
            c.push_str(&init_name);
            c.push_str("::*;");
        }

        c
    }
}