diff --git a/Cargo.toml b/Cargo.toml index dfc5967..81fe253 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ members = [ "crates/core", "crates/ctx", "crates/env", + "crates/factory", "crates/macroattr", "crates/macrofn", "crates/omiga", @@ -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" diff --git a/crates/bean/src/aware.rs b/crates/bean/src/aware.rs new file mode 100644 index 0000000..5f51994 --- /dev/null +++ b/crates/bean/src/aware.rs @@ -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 {} diff --git a/crates/bean/src/bean.rs b/crates/bean/src/bean.rs new file mode 100644 index 0000000..e4b3b2f --- /dev/null +++ b/crates/bean/src/bean.rs @@ -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; diff --git a/crates/bean/src/error.rs b/crates/bean/src/error.rs new file mode 100644 index 0000000..d751e27 --- /dev/null +++ b/crates/bean/src/error.rs @@ -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 {} diff --git a/crates/bean/src/factory.rs b/crates/bean/src/factory.rs new file mode 100644 index 0000000..474f3e1 --- /dev/null +++ b/crates/bean/src/factory.rs @@ -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(&self, name: &str, component: T); + fn get(&self, name: &str) -> Result, BeanError>; +} + +// ---------------------------------------------------------------- + +pub trait BeanFactoryAware: Aware {} diff --git a/crates/bean/src/lib.rs b/crates/bean/src/lib.rs index 0014352..c8ef129 100644 --- a/crates/bean/src/lib.rs +++ b/crates/bean/src/lib.rs @@ -17,3 +17,8 @@ // omigabean // ---------------------------------------------------------------- + +pub mod aware; +pub mod bean; +pub mod error; +pub mod factory; diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index 9c318bd..c46bd67 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -23,7 +23,6 @@ use std::fmt; // ---------------------------------------------------------------- -#[allow(dead_code)] // tmp #[derive(Debug, PartialEq)] pub enum OmigaError { Runtime(String), diff --git a/crates/core/src/helper.rs b/crates/core/src/helper.rs new file mode 100644 index 0000000..67d81f8 --- /dev/null +++ b/crates/core/src/helper.rs @@ -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; diff --git a/crates/core/src/helper/predicate.rs b/crates/core/src/helper/predicate.rs new file mode 100644 index 0000000..da03a02 --- /dev/null +++ b/crates/core/src/helper/predicate.rs @@ -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 + +// ---------------------------------------------------------------- diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 0d94674..127fc93 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -21,6 +21,8 @@ pub mod clock; pub mod collection; pub mod constants; +pub mod error; +pub mod helper; pub mod model; // ---------------------------------------------------------------- diff --git a/crates/core/src/tests.rs b/crates/core/src/tests.rs index c576da0..1fb0b9c 100644 --- a/crates/core/src/tests.rs +++ b/crates/core/src/tests.rs @@ -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] diff --git a/crates/core/src/tests/helper_tests.rs b/crates/core/src/tests/helper_tests.rs new file mode 100644 index 0000000..2a18383 --- /dev/null +++ b/crates/core/src/tests/helper_tests.rs @@ -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 + +// ---------------------------------------------------------------- diff --git a/crates/ctx/Cargo.toml b/crates/ctx/Cargo.toml index 4488c4b..6f19159 100644 --- a/crates/ctx/Cargo.toml +++ b/crates/ctx/Cargo.toml @@ -5,3 +5,5 @@ edition = "2021" license = "Apache-2.0" [dependencies] +omigabean = { version = "0.1", path = "../bean" } +dashmap = { workspace = true } diff --git a/crates/ctx/src/ctx.rs b/crates/ctx/src/ctx.rs new file mode 100644 index 0000000..af68608 --- /dev/null +++ b/crates/ctx/src/ctx.rs @@ -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 {} diff --git a/crates/ctx/src/ctx/standard.rs b/crates/ctx/src/ctx/standard.rs new file mode 100644 index 0000000..de35144 --- /dev/null +++ b/crates/ctx/src/ctx/standard.rs @@ -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>>, + initializing: DashMap, +} + +// ---------------------------------------------------------------- + +impl StandardApplicationContext { + pub fn new() -> Self { + Self { + ctx: DashMap::new(), + initializing: DashMap::new(), + } + } +} + +// ---------------------------------------------------------------- + +impl ApplicationContext for StandardApplicationContext {} + +impl BeanFactory for StandardApplicationContext { + fn register(&self, name: &str, bean: T) { + self.ctx + .insert(name.to_string(), Arc::new(Mutex::new(bean))); + } + + fn get(&self, name: &str) -> Result, 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::() + .ok_or_else(|| BeanError::CastFailed(name.to_string()))?; + + return Ok(Arc::new(downcasted.clone())); + } + + Err(BeanError::NotFound(name.to_string())) + } +} diff --git a/crates/ctx/src/lib.rs b/crates/ctx/src/lib.rs index 22c6757..7fa6e1b 100644 --- a/crates/ctx/src/lib.rs +++ b/crates/ctx/src/lib.rs @@ -17,3 +17,8 @@ // omigactx // ---------------------------------------------------------------- + +pub mod ctx; + +#[cfg(test)] +mod tests; diff --git a/crates/ctx/src/tests.rs b/crates/ctx/src/tests.rs new file mode 100644 index 0000000..b588859 --- /dev/null +++ b/crates/ctx/src/tests.rs @@ -0,0 +1,50 @@ +/* + * 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 + +// ---------------------------------------------------------------- + +use omigabean::factory::BeanFactory; + +use crate::ctx::standard::StandardApplicationContext; + +// ---------------------------------------------------------------- + +#[derive(Debug, Clone)] +struct HelloService { + value: i32, +} + +impl HelloService { + pub fn new(value: i32) -> Self { + Self { value } + } + + pub fn say_hello(&self) -> i32 { + self.value + } +} + +// ---------------------------------------------------------------- + +#[test] +fn test_ctx() { + let ctx = StandardApplicationContext::new(); + ctx.register("hello_service", HelloService::new(10086)); + let hello_service = ctx.get::("hello_service").unwrap(); + assert_eq!(10086, hello_service.say_hello()); +} diff --git a/crates/factory/Cargo.toml b/crates/factory/Cargo.toml new file mode 100644 index 0000000..6dd59a4 --- /dev/null +++ b/crates/factory/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "omigafactory" +version = "0.1.0" +edition = "2021" +license = "Apache-2.0" + +[dependencies] diff --git a/crates/factory/src/lib.rs b/crates/factory/src/lib.rs new file mode 100644 index 0000000..2d41c57 --- /dev/null +++ b/crates/factory/src/lib.rs @@ -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. + */ + +// omigafactory + +// ---------------------------------------------------------------- diff --git a/crates/omiga/src/lib.rs b/crates/omiga/src/lib.rs index 08232eb..9fe7c40 100644 --- a/crates/omiga/src/lib.rs +++ b/crates/omiga/src/lib.rs @@ -14,7 +14,7 @@ * limitations under the License. */ -// lib +// omiga // ----------------------------------------------------------------