ruwebframe 0.1.8

a simple webframe for rust actix-web, based on rudi and rbatis.
Documentation
use crate::rudi::difactroy::find_bean_di_factroy;
use crate::rudi::{DiExample, find_bean_di_example};

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rubase::get_self::GetSelf;
    use crate::rulog;
    use std::fmt::{Debug, Display};
    use std::sync::Arc;
    use crate::rucfg::find_bean_ru_config;
    use crate::rulog::ru_log;

    #[test]
    fn test_01_example() {
        let example = find_bean_di_example().unwrap();
        rulog::info2("{}", example.id);
    }
    #[test]
    fn test_02_make_di() {
        find_bean_di_factroy().unwrap().lock().unwrap().make_di();
    }

    pub fn make_di_generic<T, F>(finder: F)
    where
        F: FnOnce() -> Option<Arc<T>>,
        T: std::fmt::Display + Default + Clone, // 根据需要调整trait约束
    {
        let example = finder().unwrap();
        println!("{}", example);

        let mut data = example;
        println!("{}", data);

        if let Some(mut_ref) = Arc::get_mut(&mut data) {
            // 这里可以根据需要修改值
            // 由于泛型限制,我们可能需要传入修改逻辑
            println!("modified: {}", mut_ref);
        }
    }

    pub fn make_di_with_modifier<T, F, M>(finder: F, modifier: M)
    where
        F: FnOnce() -> Option<Arc<T>>,
        M: FnOnce(&mut T),
        T: std::fmt::Debug,
    {
        let example = finder().unwrap();
        println!("{:#?}", example);

        let mut data = example;
        println!("{:#?}", data);

        if let Some(mut_ref) = Arc::get_mut(&mut data) {
            modifier(mut_ref);
            println!("modified: {:#?}", mut_ref);
        }
    }
    #[test]
    fn test_03_make_di() {
        make_di_with_modifier(
            find_bean_di_example,
            |data| {
                data.id = 111;
            }, // 修改逻辑作为参数传入
        );
    }
    fn get_di_value<T, F>(finder: F) -> Result<Arc<T>, Box<dyn std::error::Error>>
    where
        F: FnOnce() -> Option<Arc<T>>,
        T: Display,
    {
        let example = finder().ok_or("Failed to find DI example")?;
        println!("original: {}", example);
        Ok(example)
    }

    // 第二步:修改值
    fn modify_di_value<T>(
        value: Arc<T>,
        modifier: impl FnOnce(&mut T),
    ) -> Result<Arc<T>, Box<dyn std::error::Error>>
    where
        T: Display,
    {
        let mut data = value;
        if let Some(mut_ref) = Arc::get_mut(&mut data) {
            modifier(mut_ref);
            println!("modified: {}", mut_ref);
        }
        Ok(data)
    }

    fn make_di_generic_result<T, F, M>(
        finder: F,
        modifier: M,
    ) -> Result<Arc<T>, Box<dyn std::error::Error>>
    where
        F: FnOnce() -> Option<Arc<T>>,
        M: FnOnce(&mut T),
        T: Debug,
    {
        let example = finder().ok_or("Failed to find DI example")?;

        let mut data = example;
        if let Some(mut_ref) = Arc::get_mut(&mut data) {
            modifier(mut_ref);
        }

        Ok(data)
    }

    #[test]
    fn test_04_make_di() {
        let mut ff = find_bean_di_example().unwrap().get_self();
        ff.id = 222;
        println!("modified: {:#?}", ff);
    }
    #[test]
    fn test_05_make_di() {
        let de = find_bean_di_factroy_mutex().unwrap();

        let mut data = de.lock().unwrap();

        data.id = 222;
        println!("modified: {:#?}", data);
    }
  

    #[test]
    fn test_07_make_di() {
        ru_log::info("/conf handler called");
        let c = find_bean_ru_config().unwrap().lock().unwrap().read_clone();
        //HttpResponse::Ok()
        // .json(c )

        let json_str = serde_json::to_string(&c.web).unwrap();
        ru_log::info2("conf response length:", json_str.len());
    }
}

use crate::rudi::dicontainer::direg;
use ctor::ctor;

use std::sync::{Arc, LazyLock, Mutex};
use uuid::Uuid;

const SINGLE_BEAN_NAME_mutex: &str =
    "mutex1::*rudi::DiExample::difactroy:DiFactroy:75b5f715-2ce7-4322-9429-0e9b0cebbf23:single}";

pub fn find_bean_di_factroy_mutex() -> Option<Arc<Mutex<DiExample>>> {
    direg::get_bean::<Mutex<DiExample>>(SINGLE_BEAN_NAME_mutex)
}

pub fn register_bean_difactroy_mutex() {
    direg::register_singleton(SINGLE_BEAN_NAME_mutex, DiExample::new_mutex);
}

/// 单例 Bean 的统一 trait
pub trait BeanSingleton: Sized + Send + Sync + 'static {
    /// 全局唯一标识
    const BEAN_NAME: &'static str;
    fn bean_name() -> &'static str {
        std::any::type_name::<Self>()  // "rudi::di_example::DiExample" — 天然唯一!
    }
    /// 工厂方法
    fn new_bean() -> Self;

    /// 查找(统一返回 Arc<Mutex<Self>>)
    fn find_bean() -> Option<Arc<Mutex<Self>>> {
        direg::get_bean::<Mutex<Self>>(Self::BEAN_NAME)
       // direg::get_bean::<Mutex<Self>>(Self::bean_name())
    }

    /// 注册
    fn register_bean() {
      //  direg::register_singleton(Self::bean_name(), || Mutex::new(Self::new_bean()));
        direg::register_singleton(Self::BEAN_NAME, || Mutex::new(Self::new_bean()));
    }
}