Compare commits

..

No commits in common. "30650401891db0a6e624ec360dcd39f784ceab61" and "b0c1c7d966a68e545aa5a01f038915efe14a26a6" have entirely different histories.

8 changed files with 29 additions and 162 deletions

52
Cargo.lock generated
View File

@ -160,19 +160,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"libc",
"num-integer",
"num-traits",
"time",
"winapi",
]
[[package]]
name = "concurrent-queue"
version = "1.2.2"
@ -680,7 +667,7 @@ checksum = "713d550d9b44d89174e066b7a6217ae06234c10cb47819a88290d2b353c31799"
dependencies = [
"libc",
"log",
"wasi 0.11.0+wasi-snapshot-preview1",
"wasi",
"windows-sys",
]
@ -713,25 +700,6 @@ dependencies = [
"version_check",
]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
@ -1051,7 +1019,6 @@ name = "sampad"
version = "0.1.0"
dependencies = [
"bitvec",
"chrono",
"hidapi",
"rumqttc",
"serde",
@ -1303,17 +1270,6 @@ dependencies = [
"once_cell",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]]
name = "tinyvec"
version = "1.6.0"
@ -1554,12 +1510,6 @@ dependencies = [
"try-lock",
]
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View File

@ -11,4 +11,3 @@ serde = "1"
serde_dhall = "0.11"
tracing = "0.1.35"
tracing-subscriber = "0.3.14"
chrono = "0.4.19"

View File

@ -1,13 +0,0 @@
use std::error::Error;
use std::process::Command;
fn main() -> Result<(), Box<dyn Error>> {
let hash = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.output()?
.stdout;
println!("cargo:rustc-env=VERSION={}", std::str::from_utf8(&hash)?);
Ok(())
}

View File

@ -1,26 +1,16 @@
let T = ./types.dhall
{ mqtt_servers.default = { host = "1.2.3.4", user = Some "username", pass = Some
"password" }
, layers.default
=
{ default_mapping = Mapping.Print "no mapping!"
, mappings =
let sometopic = "homeassistant/switch/something/set"
in { mqtt_servers.default
=
{ host = "1.2.3.4", user = Some "username", pass = Some "password" }
, layers.default
=
{ default_mapping = T.Action.Print "no mapping!"
, mappings =
let sometopic = "homeassistant/switch/something/set"
in { key_0 =
T.Mapping.Trigger
(T.Action.MQTTPub { topic = sometopic, payload = "ON" })
, key_1 =
T.Mapping.Trigger
(T.Action.MQTTPub { topic = sometopic, payload = "OFF" })
, key_2 =
T.Mapping.TriggerMulti
[ T.Action.Print "Doing a thing"
, T.Action.MQTTPub
{ topic = "thing/foo/bar", payload = "boop" }
]
}
}
}
in { key_0 =
Mapping.Trigger (Action.MQTTPub { topic = sometopic, payload = "ON" })
, key_1 =
Mapping.Trigger
(Action.MQTTPub { topic = sometopic, payload = "OFF" })
}
}
}

View File

@ -34,7 +34,6 @@
buildInputs = with pkgs; [
# Dhall (config) stuff
dhall
dhall-json
dhall-lsp-server
# Rust!
(rust-bin.stable.latest.default.override {

View File

@ -1,7 +1,5 @@
use crate::StdError;
use chrono::Utc;
use serde::Deserialize;
use serde_dhall::{SimpleType, StaticType};
use serde_dhall::StaticType;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
@ -26,11 +24,7 @@ impl fmt::Display for Action {
}
}
// Newtype wrapper around a Vec of actions.
// serde(transparent) and the imp StaticType below ensures we ignore the outer struct and
// transparently deserialize into the inner vec.
#[derive(Deserialize, Debug)]
#[serde(transparent)]
#[derive(Deserialize, StaticType, Debug)]
pub struct Actions(Vec<Action>);
impl Actions {
@ -46,12 +40,6 @@ impl fmt::Display for Actions {
}
}
impl StaticType for Actions {
fn static_type() -> SimpleType {
SimpleType::List(Box::new(Action::static_type()))
}
}
#[derive(Deserialize, StaticType, Debug)]
pub enum Mapping {
NOP, // Do nothing.
@ -99,25 +87,16 @@ pub struct Config {
impl Config {
pub fn from_file(p: &Path) -> Result<Self, Box<dyn Error>> {
serde_dhall::from_file(p)
match serde_dhall::from_file(p)
.with_builtin_type("Mapping".to_string(), Mapping::static_type())
.with_builtin_type("Action".to_string(), Action::static_type())
.parse::<Self>()
.map_err(|e| e.into())
}
pub fn generate_prelude() -> StdError<()> {
let mut m = HashMap::new();
m.insert("Action".to_string(), Action::static_type());
m.insert("Mapping".to_string(), Mapping::static_type());
println!(
"-- Sampad Dhall types, autogenerated by git#{} on {}",
option_env!("VERSION").unwrap_or("unknown"),
Utc::now(),
);
println!(
"{{ Action = {}, Mapping = {} }}",
Action::static_type(),
Mapping::static_type()
);
Ok(())
{
Ok(c) => Ok(c),
Err(e) => {
println!("{}", e);
Err(Box::new(e))
}
}
}
}

View File

@ -5,14 +5,11 @@ use config::{Action, Config, Mapping};
use hidapi::HidApi;
use rumqttc::{Client, MqttOptions, QoS};
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::path::Path;
use std::thread;
use tracing::{event, Level};
type StdError<T> = Result<T, Box<dyn Error>>;
struct State<'a> {
mqtt_servers: HashMap<String, Client>,
conf: &'a Config,
@ -110,19 +107,7 @@ impl<'a> State<'a> {
}
}
fn main() -> StdError<()> {
let args: Vec<_> = env::args().collect();
if let [prog, args @ ..] = &args[..] {
if args == ["generate-dhall"] {
return Config::generate_prelude();
} else if args.len() > 0 {
eprintln!("Usage: {} [generate-dhall]", prog);
return Err("Invalid usage".into());
}
} else {
unreachable!();
}
fn main() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt::init();
event!(Level::INFO, "Starting...");
let conf = Config::from_file(Path::new("./config.dhall"))?;

View File

@ -1,22 +0,0 @@
-- Sampad Dhall types, autogenerated by git#96d9ca3 on 2022-07-11 13:54:53.099283885 UTC
{ Action =
< ActivateLayer : Text
| MQTTPub : { payload : Text, topic : Text }
| Print : Text
>
, Mapping =
< NOP
| Passthrough
| Trigger :
< ActivateLayer : Text
| MQTTPub : { payload : Text, topic : Text }
| Print : Text
>
| TriggerMulti :
List
< ActivateLayer : Text
| MQTTPub : { payload : Text, topic : Text }
| Print : Text
>
>
}