use crate::rubase::{BaseEntity};
use rbatis::rbdc::Uuid;
use serde::{Deserialize, Serialize};
use std::path::Path;
use heck::ToSnakeCase;
pub const PKG_ROOT_DIR: &str = "./src";
#[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, 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 InitNew(
&self,
ifSingle: bool,
ifDefault: bool,
structName: String,
fileName: String,
) -> Self {
let mut structInfo = self.clone();
structInfo.set_if_single(ifSingle);
structInfo.ifDefault = ifDefault;
structInfo.set_struct_name(structName);
structInfo.set_path_file(fileName.to_string());
structInfo.set_struct_ini_file(structInfo.build_struct_ini_file());
structInfo.set_path_pkg(structInfo.get_ini_path());
structInfo.set_struct_pkg(structInfo.build_struct_pkg().unwrap());
structInfo.modFile = structInfo.build_struct_mod_file();
structInfo
}
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_snake_case().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(); println!("ini_path:{}", ini_path);
Path::new(&ini_path).exists() }
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 {
let p = Path::new(self.pathFile.as_str());
let dir = p.parent();
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("*");
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
}
pub fn build_struct_pkg(&self) -> Option<String> {
let mut path_pkg = self.pathPkg.as_str().trim_start_matches(PKG_ROOT_DIR);
path_pkg = path_pkg.trim_start_matches(".\\src\\");
let pkg = &path_pkg.replace("\\", "::"); let path_file = Path::new(&self.pathFile).file_prefix()?;
Some(format!("{}::{}", &pkg[2..], 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_snake_case() + "_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
}
}