-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add: 1.domain::Value converter;2.config Reader registry;3.Toml …
…config Reader. Signed-off-by: photowey <[email protected]>
- Loading branch information
Showing
13 changed files
with
433 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
// ---------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.