Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
424 changes: 291 additions & 133 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ slog = "2"
slog-async = "2"
slog-term = "2"
rustc-hash = "2"
hocon = { version = "0.9", default-features = false }
toml = "1"
humantime = "2"
byte-unit = "5"
hierarchical_hash_wheel_timer = "1.4"
owning_ref = "0.4"
futures = "0.3"
Expand Down
13 changes: 8 additions & 5 deletions core/src/component/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::*;

use crate::net::buffers::{BufferConfig, ChunkAllocator, ChunkRef};
use crate::{
config::Config,
net::buffers::{BufferConfig, ChunkAllocator, ChunkRef},
};
use std::task::Poll;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -33,7 +36,7 @@ struct ComponentContextInner<CD: ComponentTraits> {
pub(super) component: Weak<Component<CD>>,
logger: KompactLogger,
actor_ref: ActorRef<CD::Message>,
config: Arc<Hocon>,
config: Arc<Config>,
id: Uuid,
}

Expand Down Expand Up @@ -154,15 +157,15 @@ where
/// Handled::Ok
/// }
/// }
/// let default_values = r#"{ a = 7 }"#;
/// let default_values = r#"a = 7"#;
/// let mut conf = KompactConfig::default();
/// conf.load_config_str(default_values);
/// let system = conf.build().expect("system");
/// let c = system.create(ConfigComponent::new);
/// system.start(&c);
/// system.await_termination();
/// ```
pub fn config(&self) -> &Hocon {
pub fn config(&self) -> &Config {
self.inner_ref().config.as_ref()
}

Expand Down Expand Up @@ -353,7 +356,7 @@ where
/// If buffers have already been initialized, explicitly or implicitly, the method does nothing.
///
/// A custom [BufferConfig](net::buffers::BufferConfig) may be specified, if `None` is given the
/// actor will first try to parse the configuration from the system wide `Hocon`-Configuration,
/// actor will first try to parse the configuration from the system wide configuration,
/// if that fails, the default config will be used.
///
/// A custom [ChunkAllocator](net::buffers::ChunkAllocator) may also be specified.
Expand Down
1 change: 0 additions & 1 deletion core/src/component/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use hocon::Hocon;
use std::{
cell::RefCell,
fmt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ pub struct StringValue;
impl ConfigValueType for StringValue {
type Value = String;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
conf.as_string()
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))
}

fn config_string(value: Self::Value) -> String {
format!(r#""{}""#, value)
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::string(value)
}
}

Expand All @@ -21,13 +21,13 @@ pub struct IntegerValue;
impl ConfigValueType for IntegerValue {
type Value = i64;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
conf.as_i64()
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))
}

fn config_string(value: Self::Value) -> String {
format!("{}", value)
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::integer(value)
}
}

Expand All @@ -36,13 +36,13 @@ pub struct RealValue;
impl ConfigValueType for RealValue {
type Value = f64;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
conf.as_f64()
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))
}

fn config_string(value: Self::Value) -> String {
format!("{}", value)
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::real(value)
}
}

Expand All @@ -51,13 +51,13 @@ pub struct BooleanValue;
impl ConfigValueType for BooleanValue {
type Value = bool;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
conf.as_bool()
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))
}

fn config_string(value: Self::Value) -> String {
format!("{}", value)
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::boolean(value)
}
}

Expand All @@ -66,13 +66,13 @@ pub struct BytesValue;
impl ConfigValueType for BytesValue {
type Value = u64;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
conf.as_bytes()
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))
}

fn config_string(value: Self::Value) -> String {
format!(r#""{}B""#, value)
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::string(format!("{}B", value))
}
}

Expand All @@ -81,13 +81,13 @@ pub struct DurationValue;
impl ConfigValueType for DurationValue {
type Value = Duration;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
conf.as_duration()
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))
}

fn config_string(value: Self::Value) -> String {
format!(r#""{}ms""#, value.as_millis())
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::string(humantime::format_duration(value).to_string())
}
}

Expand All @@ -105,8 +105,8 @@ impl<T: ConfigValueType> Default for ArrayOfValues<T> {
impl<T: ConfigValueType> ConfigValueType for ArrayOfValues<T> {
type Value = Vec<T::Value>;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
if let Hocon::Array(values) = conf {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
if let ConfigValueInner::Array(values) = &conf.inner {
values
.iter()
.try_fold(Vec::with_capacity(values.len()), |mut acc, c| {
Expand All @@ -120,9 +120,8 @@ impl<T: ConfigValueType> ConfigValueType for ArrayOfValues<T> {
}
}

fn config_string(value: Self::Value) -> String {
let formatted: Vec<String> = value.into_iter().map(T::config_string).collect();
format!("[{}]", formatted.join(", "))
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::array(value.into_iter().map(T::into_config_value).collect())
}
}

Expand Down Expand Up @@ -153,7 +152,7 @@ mod tests {

#[test]
fn test_bytes() {
let conf = str_conf("size = 1.5KiB");
let conf = str_conf(r#"size = "1.5KiB""#);
let res = BytesValue::from_conf(&conf["size"]);
assert_eq!(Ok(1536), res);
}
Expand All @@ -165,7 +164,7 @@ mod tests {

#[test]
fn test_duration() {
let conf = str_conf("time = 3days");
let conf = str_conf(r#"time = "3days""#);
let res = DurationValue::from_conf(&conf["time"]);
assert_eq!(Ok(Duration::from_secs(3 * 24 * 60 * 60)), res);
}
Expand Down
16 changes: 8 additions & 8 deletions core/src/config/converters_for_other_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ pub struct UsizeValue;
impl ConfigValueType for UsizeValue {
type Value = usize;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
let res = conf
.as_i64()
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))?;
let ures: usize = res.try_into()?;
Ok(ures)
}

fn config_string(value: Self::Value) -> String {
format!("{}", value)
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::integer(value.try_into().expect("usize should fit into i64"))
}
}

Expand All @@ -23,14 +23,14 @@ pub struct F32Value;
impl ConfigValueType for F32Value {
type Value = f32;

fn from_conf(conf: &Hocon) -> Result<Self::Value, ConfigError> {
fn from_conf(conf: &ConfigValue) -> Result<Self::Value, ConfigError> {
conf.as_f64()
.map(|v| v as f32) // this is safe...only loses accuracy
.map(|v| v as f32)
.ok_or_else(|| ConfigError::expected::<Self::Value>(conf))
}

fn config_string(value: Self::Value) -> String {
format!("{}", value)
fn into_config_value(value: Self::Value) -> ConfigValue {
ConfigValue::real(value.into())
}
}

Expand All @@ -41,7 +41,7 @@ mod tests {

#[test]
fn test_whole_bytes() {
let conf = str_conf("size = 1.5KiB");
let conf = str_conf(r#"size = "1.5KiB""#);
let res = BytesValue::from_conf(&conf["size"]);
assert_eq!(Ok(1536u64), res);
}
Expand Down
Loading
Loading