Skip to content

Commit

Permalink
Merge pull request #93 from rneswold/pull-request
Browse files Browse the repository at this point in the history
Miscellaneous commits
  • Loading branch information
rneswold authored Aug 11, 2023
2 parents 4e5450d + e7c6a77 commit 9ead1af
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 48 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
/target

# These are local files of the author that shouldn't get included with
# the project.

CHAT-GPT.md
test.sh
watch.sh
build-dev.sh
build-release.sh
publish-api.sh
publish-backends.sh
publish-drivers.sh
publish-drmemd.sh
run-clippy.sh
run-tests.sh
test-publish-api.sh
test-publish-backends.sh
test-publish-drivers.sh
test-publish-drmemd.sh
11 changes: 5 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions backends/drmem-db-redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ pub mod config;

fn xlat_err(e: redis::RedisError) -> Error {
match e.kind() {
redis::ErrorKind::ResponseError
| redis::ErrorKind::ClusterDown
| redis::ErrorKind::CrossSlot
| redis::ErrorKind::MasterDown
| redis::ErrorKind::IoError => Error::DbCommunicationError,

redis::ErrorKind::AuthenticationFailed
| redis::ErrorKind::InvalidClientConfig => Error::AuthenticationError,

Expand All @@ -57,13 +51,15 @@ fn xlat_err(e: redis::RedisError) -> Error {
| redis::ErrorKind::TryAgain
| redis::ErrorKind::ClientError
| redis::ErrorKind::ExtensionError
| redis::ErrorKind::ReadOnly => Error::OperationError,
| redis::ErrorKind::ReadOnly => {
Error::OperationError("backend is read-only".to_owned())
}

redis::ErrorKind::NoScriptError
| redis::ErrorKind::Moved
| redis::ErrorKind::Ask => Error::NotFound,

_ => Error::UnknownError,
_ => Error::BackendError(format!("{}", &e)),
}
}

Expand Down
6 changes: 4 additions & 2 deletions backends/drmem-db-simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl Store for SimpleStore {
)),
}
} else {
Err(Error::OperationError)
Err(Error::OperationError(format!("{} is read-only", &name)))
}
} else {
Err(Error::NotFound)
Expand Down Expand Up @@ -457,7 +457,9 @@ impl Store for SimpleStore {
}
}
} else {
Err(Error::OperationError)
Err(Error::OperationError(
"unable to lock reading channel".to_owned(),
))
}
} else {
Err(Error::NotFound)
Expand Down
2 changes: 1 addition & 1 deletion drivers/drmem-drv-ntp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl driver::API for Instance {
return Ok(Box::new(Instance { sock, seq: 1 }));
}
}
Err(Error::OperationError)
Err(Error::OperationError("couldn't create socket".to_owned()))
};

Box::pin(fut)
Expand Down
2 changes: 1 addition & 1 deletion drivers/drmem-drv-weather-wu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Instance {
))
} else {
error!("couldn't determine public API key");
Err(Error::UnknownError)
Err(Error::NotFound)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions drmem-api/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module defines types and interfaces that internal clients use
//! to interact with the core of DrMem. The primary, internal client
//! is the GraphQL interface.
//! Defines types and interfaces that internal clients use to interact
//! with the core of DrMem. The primary, internal client is the
//! GraphQL interface, but asks in logic blocks also use this module.
//!
//! Any new, internal tasks that need access to device readings or
//! wish to set the value of the device need to have a
Expand Down
4 changes: 2 additions & 2 deletions drmem-api/src/driver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! This module defines types and interfaces that drivers use to
//! interact with the core of DrMem.
//! Defines types and interfaces that drivers use to interact with the
//! core of DrMem.
use crate::types::{
device::{Base, Name, Path, Value},
Expand Down
3 changes: 2 additions & 1 deletion drmem-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! This crate is used by various, internal tasks of `drmemd`.
//! This crate is used by internal tasks of `drmemd` as well as
//! hardware drivers.
//!
//! The interfaces and types defined in this crate are useful for
//! those wishing to write a new back-end storage module or a driver
Expand Down
3 changes: 2 additions & 1 deletion drmem-api/src/types/device/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! This module defines types related to devices.
//! Defines types related to devices.
use std::{pin::Pin, time};
use tokio_stream::Stream;

Expand Down
24 changes: 9 additions & 15 deletions drmem-api/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ pub enum Error {
/// An invalid value was provided.
InvArgument(String),

/// Returned when a communication error occurred with the backend
/// database. Each backend will have its own recommendations on
/// how to recover.
DbCommunicationError,
/// A general error returned by the backend storage. The string
/// will have more information about the error.
BackendError(String),

/// Communication was disrupted due to one end not following a
/// protocol.
Expand All @@ -51,7 +50,7 @@ pub enum Error {

/// The requested operation couldn't complete. The description
/// field will have more information for the user.
OperationError,
OperationError(String),

/// A bad parameter was given in a configuration or a
/// configuration was missing a required parameter.
Expand All @@ -60,10 +59,6 @@ pub enum Error {
/// There was a problem parsing a string. The associated string
/// will describe how the parsing failed.
ParseError(String),

/// A dependent library introduced a new error that hasn't been
/// properly mapped in DrMem. This needs to be reported as a bug.
UnknownError,
}

impl std::error::Error for Error {}
Expand All @@ -80,19 +75,18 @@ impl fmt::Display for Error {
write!(f, "{} is missing peer", detail)
}
Error::TypeError => write!(f, "incorrect type"),
Error::InvArgument(s) => write!(f, "{}", s),
Error::DbCommunicationError => {
write!(f, "db communication error")
Error::InvArgument(v) => write!(f, "{}", &v),
Error::BackendError(v) => {
write!(f, "backend error: {}", &v)
}
Error::ProtocolError(v) => write!(f, "protocol error: {}", &v),
Error::AuthenticationError => write!(f, "permission error"),
Error::TimeoutError => write!(f, "timeout"),
Error::OperationError => {
write!(f, "couldn't complete operation")
Error::OperationError(v) => {
write!(f, "couldn't complete operation: {}", &v)
}
Error::BadConfig(v) => write!(f, "config error: {}", &v),
Error::ParseError(v) => write!(f, "parse error: {}", &v),
Error::UnknownError => write!(f, "unhandled error"),
}
}
}
Expand Down
130 changes: 128 additions & 2 deletions drmemd/src/driver/drv_cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Instance {
// drmem-api crate indicating the max sample rate?

if (50..=3_600_000).contains(millis) {
Ok(time::Duration::from_millis(*millis as u64 / 2))
Ok(time::Duration::from_millis(*millis as u64))
} else {
Err(Error::BadConfig(String::from("'millis' out of range")))
}
Expand Down Expand Up @@ -188,7 +188,7 @@ impl Instance {
// If the output is already at the desired value,
// don't emit it again.

if &self.disabled != &self.enabled[self.index] {
if self.disabled != self.enabled[self.index] {
Some(self.disabled.clone())
} else {
None
Expand Down Expand Up @@ -327,6 +327,132 @@ impl driver::API for Instance {
#[cfg(test)]
mod tests {
use super::*;
use drmem_api::driver::API;
use std::time::Duration;

#[tokio::test]
async fn test_cfg() {
{
let mut cfg = DriverConfig::new();

cfg.insert("millis".to_owned(), toml::value::Value::Integer(500));
cfg.insert(
"enabled_at_boot".to_owned(),
toml::value::Value::Boolean(true),
);
cfg.insert(
"disabled".to_owned(),
toml::value::Value::Boolean(false),
);
cfg.insert(
"enabled".to_owned(),
toml::value::Value::Array(vec![
toml::value::Value::Boolean(true),
toml::value::Value::Boolean(false),
]),
);

let inst = Instance::create_instance(&cfg).await.unwrap();

assert_eq!(inst.enabled_at_boot, true);
assert_eq!(inst.millis, Duration::from_millis(500));
assert_eq!(inst.disabled, device::Value::Bool(false));
assert_eq!(
inst.enabled,
vec![device::Value::Bool(true), device::Value::Bool(false)]
);
}

{
let mut cfg = DriverConfig::new();

cfg.insert("millis".to_owned(), toml::value::Value::Integer(500));
cfg.insert(
"disabled".to_owned(),
toml::value::Value::Boolean(false),
);
cfg.insert(
"enabled".to_owned(),
toml::value::Value::Array(vec![
toml::value::Value::Boolean(true),
toml::value::Value::Boolean(false),
]),
);

let inst = Instance::create_instance(&cfg).await.unwrap();

assert_eq!(inst.enabled_at_boot, false);
assert_eq!(inst.millis, Duration::from_millis(500));
assert_eq!(inst.disabled, device::Value::Bool(false));
assert_eq!(
inst.enabled,
vec![device::Value::Bool(true), device::Value::Bool(false)]
);
}

{
let mut cfg = DriverConfig::new();

cfg.insert(
"disabled".to_owned(),
toml::value::Value::Boolean(false),
);
cfg.insert(
"enabled".to_owned(),
toml::value::Value::Array(vec![
toml::value::Value::Boolean(true),
toml::value::Value::Boolean(false),
]),
);

assert!(Instance::create_instance(&cfg).await.is_err());
}

{
let mut cfg = DriverConfig::new();

cfg.insert("millis".to_owned(), toml::value::Value::Integer(500));
cfg.insert(
"enabled".to_owned(),
toml::value::Value::Array(vec![
toml::value::Value::Boolean(true),
toml::value::Value::Boolean(false),
]),
);

assert!(Instance::create_instance(&cfg).await.is_err());
}

{
let mut cfg = DriverConfig::new();

cfg.insert("millis".to_owned(), toml::value::Value::Integer(500));
cfg.insert(
"disabled".to_owned(),
toml::value::Value::Boolean(false),
);

assert!(Instance::create_instance(&cfg).await.is_err());
}

{
let mut cfg = DriverConfig::new();

cfg.insert("millis".to_owned(), toml::value::Value::Integer(500));
cfg.insert(
"disabled".to_owned(),
toml::value::Value::Boolean(false),
);
cfg.insert(
"enabled".to_owned(),
toml::value::Value::Array(vec![toml::value::Value::Boolean(
true,
)]),
);

assert!(Instance::create_instance(&cfg).await.is_err());
}
}

#[test]
fn test_state_changes() {
Expand Down
5 changes: 4 additions & 1 deletion drmemd/src/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,10 @@ pub fn server(
.with(
warp::cors()
.allow_any_origin()
.allow_headers(vec!["content-type"])
.allow_headers(vec![
"content-type",
"Access-Control-Allow-Origin",
])
.allow_methods(vec!["OPTIONS", "GET", "POST"])
.max_age(Duration::from_secs(3_600)),
);
Expand Down
4 changes: 2 additions & 2 deletions drmemd/src/logic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Output {

// Attempts to set the associated device to a new value.

pub async fn send(&mut self, value: device::Value) -> () {
pub async fn send(&mut self, value: device::Value) {
// Only attempt the setting if the device isn't set to the
// value.

Expand Down Expand Up @@ -226,7 +226,7 @@ impl Node {
// problem and return an error.

error!("all inputs have closed ... terminating");
Err(Error::OperationError)
Err(Error::OperationError("no available inputs".to_owned()))
}

// Starts a new instance of a logic node.
Expand Down
Loading

0 comments on commit 9ead1af

Please sign in to comment.