Skip to content

Commit

Permalink
feat: Add: 1.ApplicationContext add register_x and predicate_x functi…
Browse files Browse the repository at this point in the history
…ons.

Signed-off-by: photowey <[email protected]>
  • Loading branch information
photowey committed Jul 21, 2024
1 parent 6ade66b commit e2e75ab
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 3 deletions.
26 changes: 26 additions & 0 deletions crates/bean/src/bean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,29 @@ use std::any::Any;

pub type Bean = (dyn Any + Send + Sync);
pub type Boolean = bool;

// ----------------------------------------------------------------

pub enum Booleans {
TRUE(Boolean),
FALSE(Boolean),
}

// ----------------------------------------------------------------

impl Booleans {
pub fn value(&self) -> Boolean {
match self {
Booleans::TRUE(value) => *value,
Booleans::FALSE(value) => *value,
}
}

pub fn value_of(&self, value: Boolean) -> Booleans {
if value {
return Booleans::TRUE(value);
}

return Booleans::FALSE(value);
}
}
8 changes: 7 additions & 1 deletion crates/ctx/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@

// ----------------------------------------------------------------

use omigabean::bean::Boolean;
use omigabean::factory::BeanFactory;

pub mod standard;

// ----------------------------------------------------------------

pub trait ApplicationContext: BeanFactory {}
pub trait ApplicationContext: BeanFactory {
fn register_initializing(&self, name: &str) -> Option<Boolean>;
fn register_initialized(&self, name: &str) -> Option<Boolean>;
fn predicate_initializing(&self, name: &str) -> Option<Boolean>;
fn predicate_initialized(&self, name: &str) -> Option<Boolean>;
}
35 changes: 34 additions & 1 deletion crates/ctx/src/ctx/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,45 @@ impl StandardApplicationContext {

// ----------------------------------------------------------------

impl ApplicationContext for StandardApplicationContext {}
impl ApplicationContext for StandardApplicationContext {
fn register_initializing(&self, name: &str) -> Option<Boolean> {
self.initializing.insert(name.to_string(), true)
}

fn register_initialized(&self, name: &str) -> Option<Boolean> {
if self.initializing.contains_key(name) {
self.initializing.remove(name);
return Some(true);
}

None
}

fn predicate_initializing(&self, name: &str) -> Option<Boolean> {
if self.initializing.contains_key(name) {
return Some(true);
}

None
}

fn predicate_initialized(&self, name: &str) -> Option<Boolean> {
if self.ctx.contains_key(name) {
return Some(true);
}

None
}
}

// ----------------------------------------------------------------

impl BeanFactory for StandardApplicationContext {
fn register<T: 'static + Any + Send + Sync + Clone>(&self, name: &str, bean: T) {
self.ctx
.insert(name.to_string(), Arc::new(Mutex::new(bean)));
// register initialized
self.register_initialized(name);
}

fn get<T: 'static + Any + Send + Sync + Clone>(&self, name: &str) -> Result<Arc<T>, BeanError> {
Expand Down
88 changes: 87 additions & 1 deletion crates/ctx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use omigabean::factory::BeanFactory;

use crate::ctx::standard::StandardApplicationContext;
use crate::ctx::ApplicationContext;

// ----------------------------------------------------------------

Expand All @@ -42,9 +43,94 @@ impl HelloService {
// ----------------------------------------------------------------

#[test]
fn test_ctx() {
fn test_ctx_register() {
let ctx = StandardApplicationContext::new();
ctx.register("hello_service", HelloService::new(10086));
}

#[test]
fn test_ctx_get() {
let ctx = StandardApplicationContext::new();
ctx.register("hello_service", HelloService::new(10086));
let hello_service = ctx.get::<HelloService>("hello_service").unwrap();
assert_eq!(10086, hello_service.say_hello());
}

// ----------------------------------------------------------------

#[test]
fn test_ctx_register_initializing() {
let ctx = StandardApplicationContext::new();

let rvt = ctx.register_initializing("hello_service");
assert!(rvt.is_none());

let rvt_1 = ctx.predicate_initializing("hello_service");
assert!(rvt_1.is_some());
}

#[test]
fn test_ctx_register_initialized() {
let ctx = StandardApplicationContext::new();

let rvt = ctx.register_initializing("hello_service");
assert!(rvt.is_none());

let rvt2 = ctx.register_initialized("hello_service");
assert!(rvt2.is_some());

let rvt_1 = ctx.predicate_initializing("hello_service");
assert!(rvt_1.is_none());
}

#[test]
fn test_ctx_predicate_initializing() {
let ctx = StandardApplicationContext::new();

let rvt_1 = ctx.register_initializing("hello_service");
assert!(rvt_1.is_none());

let rvt_1_1 = ctx.predicate_initializing("hello_service");
assert!(rvt_1_1.is_some());

let rvt_2 = ctx.register_initialized("hello_service");
assert!(rvt_2.is_some());

let rvt_2_1 = ctx.predicate_initializing("hello_service");
assert!(rvt_2_1.is_none());
}

#[test]
fn test_ctx_register_bean_with_initialized() {
let ctx = StandardApplicationContext::new();

let rvt_1 = ctx.register_initializing("hello_service");
assert!(rvt_1.is_none());

let rvt_1_1 = ctx.predicate_initializing("hello_service");
assert!(rvt_1_1.is_some());

ctx.register("hello_service", HelloService::new(10086));

let rvt_2 = ctx.predicate_initializing("hello_service");
assert!(rvt_2.is_none());
}

#[test]
fn test_ctx_predicate_initialized() {
let ctx = StandardApplicationContext::new();

let rvt_1 = ctx.register_initializing("hello_service");
assert!(rvt_1.is_none());

let rvt_1_1 = ctx.predicate_initializing("hello_service");
assert!(rvt_1_1.is_some());

ctx.register("hello_service", HelloService::new(10086));

let rvt_2 = ctx.predicate_initializing("hello_service");
assert!(rvt_2.is_none());

let rvt_3 = ctx.predicate_initialized("hello_service");
assert!(rvt_3.is_some());
}

0 comments on commit e2e75ab

Please sign in to comment.