Skip to content

Commit

Permalink
feat: Add: 1.StandardApplicationContext(ApplicationContext+BeanFactory)
Browse files Browse the repository at this point in the history
Signed-off-by: photowey <[email protected]>
  • Loading branch information
photowey committed Jul 21, 2024
1 parent bcf409c commit 6ade66b
Show file tree
Hide file tree
Showing 20 changed files with 399 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"crates/core",
"crates/ctx",
"crates/env",
"crates/factory",
"crates/macroattr",
"crates/macrofn",
"crates/omiga",
Expand All @@ -27,7 +28,8 @@ quote = "1.0"
proc-macro2 = "1.0"
# ----------------------------------------------------------------
chrono = "0.4"
chronounit = "0.3"
toml = { version = "0.8" }
serde = { version = "1.0", features = ["derive"] }
clap = { version = "4.0", features = ["derive"] }
chronounit = "0.3"
dashmap = "6.0"
21 changes: 21 additions & 0 deletions crates/bean/src/aware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// aware

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

pub trait Aware {}
26 changes: 26 additions & 0 deletions crates/bean/src/bean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// bean

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

use std::any::Any;

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

pub type Bean = (dyn Any + Send + Sync);
pub type Boolean = bool;
53 changes: 53 additions & 0 deletions crates/bean/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// error

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

use std::error::Error;
use std::fmt;

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

#[derive(Debug, PartialEq)]
pub enum BeanError {
CircularDependency(String),
NotFound(String),
CastFailed(String),
}

impl fmt::Display for BeanError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BeanError::CircularDependency(message) => {
write!(f, "Omiga: circular dependency error, message:[{}]", message)
}
BeanError::NotFound(message) => {
write!(f, "Omiga: bean not found error, message:[{}]", message)
}
BeanError::CastFailed(message) => {
write!(
f,
"Omiga: component cast to `Bean` error, message:[{}]",
message
)
}
}
}
}

impl Error for BeanError {}
36 changes: 36 additions & 0 deletions crates/bean/src/factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// factory

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

use std::any::Any;
use std::sync::Arc;

use crate::aware::Aware;
use crate::error::BeanError;

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

pub trait BeanFactory {
fn register<T: 'static + Any + Send + Sync + Clone>(&self, name: &str, component: T);
fn get<T: 'static + Any + Send + Sync + Clone>(&self, name: &str) -> Result<Arc<T>, BeanError>;
}

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

pub trait BeanFactoryAware: Aware {}
5 changes: 5 additions & 0 deletions crates/bean/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@
// omigabean

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

pub mod aware;
pub mod bean;
pub mod error;
pub mod factory;
1 change: 0 additions & 1 deletion crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::fmt;

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

#[allow(dead_code)] // tmp
#[derive(Debug, PartialEq)]
pub enum OmigaError {
Runtime(String),
Expand Down
21 changes: 21 additions & 0 deletions crates/core/src/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// helper

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

pub mod predicate;
19 changes: 19 additions & 0 deletions crates/core/src/helper/predicate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// helper/predicate

// ----------------------------------------------------------------
2 changes: 2 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
pub mod clock;
pub mod collection;
pub mod constants;
pub mod error;
pub mod helper;
pub mod model;

// ----------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions crates/core/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use crate::clock::timestamp::now;
use crate::collection::merger::merge_tables;
use crate::collection::table::{Table, Value};

#[cfg(test)]
mod helper_tests;

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

#[test]
Expand Down
19 changes: 19 additions & 0 deletions crates/core/src/tests/helper_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// tests/helper_tests

// ----------------------------------------------------------------
2 changes: 2 additions & 0 deletions crates/ctx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ edition = "2021"
license = "Apache-2.0"

[dependencies]
omigabean = { version = "0.1", path = "../bean" }
dashmap = { workspace = true }
27 changes: 27 additions & 0 deletions crates/ctx/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// ctx

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

use omigabean::factory::BeanFactory;

pub mod standard;

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

pub trait ApplicationContext: BeanFactory {}
80 changes: 80 additions & 0 deletions crates/ctx/src/ctx/standard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright © 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#![allow(dead_code)]

// standard

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

use std::any::Any;
use std::sync::{Arc, Mutex};

use dashmap::DashMap;

use omigabean::bean::{Bean, Boolean};
use omigabean::error::BeanError;
use omigabean::factory::BeanFactory;

use crate::ctx::ApplicationContext;

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

pub struct StandardApplicationContext {
ctx: DashMap<String, Arc<Mutex<Bean>>>,
initializing: DashMap<String, Boolean>,
}

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

impl StandardApplicationContext {
pub fn new() -> Self {
Self {
ctx: DashMap::new(),
initializing: DashMap::new(),
}
}
}

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

impl ApplicationContext for StandardApplicationContext {}

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)));
}

fn get<T: 'static + Any + Send + Sync + Clone>(&self, name: &str) -> Result<Arc<T>, BeanError> {
if self.initializing.contains_key(name) {
return Err(BeanError::CircularDependency(name.to_string()));
}

if let Some(bean) = self.ctx.get(name) {
let lock = bean
.lock()
.map_err(|_| BeanError::CastFailed(name.to_string()))?;
let downcasted = lock
.downcast_ref::<T>()
.ok_or_else(|| BeanError::CastFailed(name.to_string()))?;

return Ok(Arc::new(downcasted.clone()));
}

Err(BeanError::NotFound(name.to_string()))
}
}
5 changes: 5 additions & 0 deletions crates/ctx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@
// omigactx

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

pub mod ctx;

#[cfg(test)]
mod tests;
Loading

0 comments on commit 6ade66b

Please sign in to comment.