Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wilyle committed Sep 27, 2023
1 parent 53dd92f commit f5b574d
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 83 deletions.
9 changes: 6 additions & 3 deletions build_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::{path::Path, env, fs};
use std::{env, fs, path::Path};

pub fn copy_to_build_out_dir<P: AsRef<Path>>(source: P, dest_filename: String) {
const OUT_DIR: &str = "OUT_DIR";
Expand All @@ -12,5 +12,8 @@ pub fn copy_to_build_out_dir<P: AsRef<Path>>(source: P, dest_filename: String) {

fs::copy(&source, destination).unwrap();

println!("cargo:rerun-if-changed={}", source.as_ref().to_str().unwrap());
}
println!(
"cargo:rerun-if-changed={}",
source.as_ref().to_str().unwrap()
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct InMemoryMockCloudAdapter {}
impl CloudAdapter for InMemoryMockCloudAdapter {
/// Creates a new instance of a CloudAdapter with default settings
fn create_new() -> Result<Self, CloudAdapterError> {
Ok(Self{})
Ok(Self {})
}

/// Sends the signal to the cloud
Expand Down
21 changes: 13 additions & 8 deletions common/src/config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::{path::Path, env};
use std::{env, path::Path};

use config::{ConfigError, File};
use home::home_dir;
Expand All @@ -16,7 +16,7 @@ const FREYJA_HOME: &str = "FREYJA_HOME";
/// Uses `{config_file_name}.default.{config_file_ext}` as the base configuration,
/// then searches for overrides named `{config_file_name}.{config_file_ext}` in the current directory and `$FREYJA_HOME`.
/// If `$FREYJA_HOME` is not set, it defaults to `$HOME/.freyja`.
///
///
/// # Arguments
/// - `config_file_name`: The config file name without an extension. This is used to construct the file names to search for
/// - `config_file_ext`: The config file extension. This is used to construct the file names to search for
Expand All @@ -29,10 +29,10 @@ pub fn read_from_files<TConfig, TError, TPath, TIoErrorHandler, TDeserializeErro
default_config_path: TPath,
io_error_handler: TIoErrorHandler,
deserialize_error_handler: TDeserializeErrorHandler,
) -> Result<TConfig, TError>
) -> Result<TConfig, TError>
where
TConfig: for<'a> Deserialize<'a>,
TPath : AsRef<Path>,
TPath: AsRef<Path>,
TIoErrorHandler: Fn(std::io::Error) -> TError,
TDeserializeErrorHandler: FnOnce(ConfigError) -> TError,
{
Expand All @@ -52,11 +52,14 @@ where
Path::new(&freyja_home)
.join(CONFIG_DIR)
.join(overrides_filename)
},
}
Err(_) => {
// $HOME/.freyja/config/mapping_client_config.json
home_dir()
.ok_or(io_error_handler(std::io::Error::new(std::io::ErrorKind::Other, "Could not retrieve home directory")))?
.ok_or(io_error_handler(std::io::Error::new(
std::io::ErrorKind::Other,
"Could not retrieve home directory",
)))?
.join(DOT_FREYJA_DIR)
.join(CONFIG_DIR)
.join(overrides_filename)
Expand All @@ -70,5 +73,7 @@ where
.build()
.unwrap();

config_store.try_deserialize().map_err(deserialize_error_handler)
}
config_store
.try_deserialize()
.map_err(deserialize_error_handler)
}
2 changes: 1 addition & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
// SPDX-License-Identifier: MIT

pub mod config_utils;
pub mod signal_store;
pub mod retry_utils;
pub mod signal_store;
2 changes: 1 addition & 1 deletion mapping_clients/in_memory_mock_mapping_client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fn main() {
.unwrap()
.join(RES_DIR_NAME)
.join(DEFAULT_CONFIG_FILE);

copy_to_build_out_dir(config_path, DEFAULT_CONFIG_FILE.to_string());
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::sync::atomic::{AtomicU8, Ordering};
use std::env;
use std::sync::atomic::{AtomicU8, Ordering};

use async_trait::async_trait;

Expand Down Expand Up @@ -46,7 +46,7 @@ impl MappingClient for InMemoryMockMappingClient {
CONFIG_EXT,
env!("OUT_DIR"),
MappingClientError::io,
MappingClientError::deserialize
MappingClientError::deserialize,
)?;

Self::from_config(config)
Expand Down Expand Up @@ -112,41 +112,43 @@ mod in_memory_mock_mapping_client_tests {

#[tokio::test]
async fn check_for_work_returns_correct_values() {
let config = Config { values: vec![
ConfigItem {
begin: 0,
end: None,
value: DigitalTwinMapEntry {
source: String::from("always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
let config = Config {
values: vec![
ConfigItem {
begin: 0,
end: None,
value: DigitalTwinMapEntry {
source: String::from("always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
},
},
},
ConfigItem {
begin: 10,
end: None,
value: DigitalTwinMapEntry {
source: String::from("delayed-activaction"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
ConfigItem {
begin: 10,
end: None,
value: DigitalTwinMapEntry {
source: String::from("delayed-activaction"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
},
},
},
ConfigItem {
begin: 0,
end: Some(20),
value: DigitalTwinMapEntry {
source: String::from("not-always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
ConfigItem {
begin: 0,
end: Some(20),
value: DigitalTwinMapEntry {
source: String::from("not-always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
},
},
},
]};
],
};

let uut = InMemoryMockMappingClient::from_config(config).unwrap();

Expand All @@ -165,41 +167,43 @@ mod in_memory_mock_mapping_client_tests {

#[tokio::test]
async fn get_mapping_returns_correct_values() {
let config = Config { values: vec![
ConfigItem {
begin: 0,
end: None,
value: DigitalTwinMapEntry {
source: String::from("always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
let config = Config {
values: vec![
ConfigItem {
begin: 0,
end: None,
value: DigitalTwinMapEntry {
source: String::from("always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
},
},
},
ConfigItem {
begin: 10,
end: None,
value: DigitalTwinMapEntry {
source: String::from("delayed-activation"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
ConfigItem {
begin: 10,
end: None,
value: DigitalTwinMapEntry {
source: String::from("delayed-activation"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
},
},
},
ConfigItem {
begin: 0,
end: Some(20),
value: DigitalTwinMapEntry {
source: String::from("not-always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
ConfigItem {
begin: 0,
end: Some(20),
value: DigitalTwinMapEntry {
source: String::from("not-always-active"),
target: HashMap::new(),
interval_ms: 0,
conversion: Conversion::None,
emit_on_change: false,
},
},
},
]};
],
};

let uut = InMemoryMockMappingClient::from_config(config).unwrap();

Expand Down
2 changes: 1 addition & 1 deletion mocks/mock_mapping_service/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fn main() {
.unwrap()
.join(RES_DIR_NAME)
.join(DEFAULT_CONFIG_FILE);

copy_to_build_out_dir(config_path, DEFAULT_CONFIG_FILE.to_string());
}
6 changes: 4 additions & 2 deletions mocks/mock_mapping_service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ async fn main() {
.filter(None, LevelFilter::Info)
.target(Target::Stdout)
.init();

let config = config_utils::read_from_files(
CONFIG_FILE,
CONFIG_EXT,
env!("OUT_DIR"),
|e| log::error!("{}", e),
|e| log::error!("{}", e)).unwrap();
|e| log::error!("{}", e),
)
.unwrap();

const SERVER_ENDPOINT: &str = "127.0.0.1:8888";

Expand Down

0 comments on commit f5b574d

Please sign in to comment.