Skip to content

Commit

Permalink
feat: Add: 1.domain::Value converter;2.config Reader registry;3.Toml …
Browse files Browse the repository at this point in the history
…config Reader.

Signed-off-by: photowey <[email protected]>
  • Loading branch information
photowey committed Jul 10, 2024
1 parent 5010af3 commit b51709d
Show file tree
Hide file tree
Showing 13 changed files with 433 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:

- name: Run tests
#run: cargo test --verbose --test integration_tests -- --show-output
run: cargo test --verbose -- --show-output
run: cargo test --verbose --features tomls -- --show-output
5 changes: 5 additions & 0 deletions crates/core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ pub const SIGMA_CORE_CONFIG_FILE_SEARCH_PATHS_DEFAULT: &str = ".,configs,resourc

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

pub const SIGMA_CONFIG_READER_TOML: &str = "TOML";
pub const SIGMA_CONFIG_READER_TOML_SUFFIX: &str = "toml";

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

/// 9320: A dream moment for Manchester City's forward `Agüero`.
pub const SIGMA_WEB_SERVER_PORT_DEFAULT: u32 = 9320;
11 changes: 11 additions & 0 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ version = "0.1.0"
edition = "2021"

[dependencies]
omigacore = { path = "../core" }
chrono = "0.4"
toml = { version = "0.8", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }

[features]
# tomls == toml suffix
tomls = ["toml"]

# https://docs.rs/about/metadata
[package.metadata.docs.rs]
features = ["tomls"]
1 change: 1 addition & 0 deletions crates/env/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

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

pub mod converter;
pub mod domain;
pub mod error;
137 changes: 137 additions & 0 deletions crates/env/src/core/converter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* 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.
*/

// core/converter

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

use chrono::NaiveDateTime;

use crate::core::domain::{Array, Table, Value};
use crate::core::error::ConfigError;

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

pub struct ValueConverter;

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

impl ValueConverter {
pub fn try_datetime(rvt: Result<&Value, ConfigError>) -> Option<&NaiveDateTime> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_string(rvt: Result<&Value, ConfigError>) -> Option<&String> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_str(rvt: Result<&Value, ConfigError>) -> Option<&str> {
match rvt {
Ok(Value::String(s)) => Some(s),
_ => None,
}
}

pub fn try_bool(rvt: Result<&Value, ConfigError>) -> Option<&bool> {
match rvt {
Ok(Value::Boolean(b)) => Some(b),
_ => None,
}
}

pub fn try_nested(rvt: Result<&Value, ConfigError>) -> Option<&Table> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_array(rvt: Result<&Value, ConfigError>) -> Option<&Array> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_int_u128(rvt: Result<&Value, ConfigError>) -> Option<&u128> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_int_u64(rvt: Result<&Value, ConfigError>) -> Option<&u64> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_int_u32(rvt: Result<&Value, ConfigError>) -> Option<&u32> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_int_i128(rvt: Result<&Value, ConfigError>) -> Option<&i128> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_int_i64(rvt: Result<&Value, ConfigError>) -> Option<&i64> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_int_i32(rvt: Result<&Value, ConfigError>) -> Option<&i32> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_float64(rvt: Result<&Value, ConfigError>) -> Option<&f64> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_float32(rvt: Result<&Value, ConfigError>) -> Option<&f32> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}

pub fn try_none(rvt: Result<&Value, ConfigError>) -> Option<&()> {
match rvt {
Ok(v) => v.into(),
_ => None,
}
}
}
5 changes: 5 additions & 0 deletions crates/env/src/core/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ pub type Array = Vec<Value>;

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

pub type OmigaTable = Table;
pub type OmigaValue = Value;

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

#[derive(Debug, PartialEq, Clone)]
pub enum Value {
Nested(Table),
Expand Down
4 changes: 4 additions & 0 deletions crates/env/src/environment.rs → crates/env/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use crate::core::domain::Value;
use crate::core::error::ConfigError;
use crate::reader::ConfigReader;

pub mod standard;

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

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

pub trait Environment {
Expand Down
19 changes: 19 additions & 0 deletions crates/env/src/env/standard.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.
*/

// env/standard

// ----------------------------------------------------------------
5 changes: 4 additions & 1 deletion crates/env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// ----------------------------------------------------------------

pub mod core;
pub mod environment;
pub mod env;
pub mod reader;
pub mod standard;

#[cfg(test)]
mod tests;
5 changes: 5 additions & 0 deletions crates/env/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ use std::path::PathBuf;
use crate::core::domain::Table;
use crate::core::error::ReadError;

mod registry;

#[cfg(feature = "tomls")]
pub mod toml;

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

pub trait ConfigReader {
Expand Down
70 changes: 70 additions & 0 deletions crates/env/src/reader/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.
*/

// reader/registry

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

use std::collections::HashMap;

use crate::reader::ConfigReader;

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

pub trait ReaderRegistry {
fn register(&mut self, reader: Box<dyn ConfigReader>);
fn try_acquire(&self, suffix: &str) -> Option<&dyn ConfigReader>;
fn try_acquires(&self) -> Vec<&dyn ConfigReader>;
}

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

pub struct ConfigReaderRegistry {
readers: HashMap</*suffix*/ String, Box<dyn ConfigReader>>,
}

impl ConfigReaderRegistry {
pub fn new() -> Self {
Self {
readers: HashMap::new(),
}
}
}

impl Default for ConfigReaderRegistry {
fn default() -> Self {
ConfigReaderRegistry::new()
}
}

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

impl ReaderRegistry for ConfigReaderRegistry {
fn register(&mut self, reader: Box<dyn ConfigReader>) {
self.readers.insert(reader.suffix(), reader);
}

fn try_acquire(&self, suffix: &str) -> Option<&dyn ConfigReader> {
self.readers.get(suffix).map(|r| r.as_ref())
}

fn try_acquires(&self) -> Vec<&dyn ConfigReader> {
self.readers
.values()
.map(|r| r.as_ref() as &dyn ConfigReader)
.collect()
}
}
Loading

0 comments on commit b51709d

Please sign in to comment.