From f111d6772dfdf071af9f2cade05259e630dd3d90 Mon Sep 17 00:00:00 2001 From: Justin Moon Date: Sat, 2 Jul 2022 00:13:49 -0400 Subject: [PATCH] Plugin config options with no defaults --- plugins/src/lib.rs | 30 +++++++++++++++++++----------- plugins/src/options.rs | 15 +++++++++++++-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/plugins/src/lib.rs b/plugins/src/lib.rs index 8c034a4a6..a437defab 100644 --- a/plugins/src/lib.rs +++ b/plugins/src/lib.rs @@ -5,6 +5,7 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; extern crate log; use log::trace; use messages::Configuration; +use options::ConfigOption; use std::collections::HashMap; use std::future::Future; use std::pin::Pin; @@ -14,7 +15,6 @@ use tokio::sync::Mutex; use tokio_stream::StreamExt; use tokio_util::codec::FramedRead; use tokio_util::codec::FramedWrite; -use options::ConfigOption; pub mod codec; pub mod logging; @@ -25,7 +25,6 @@ extern crate serde_json; pub mod options; - /// Need to tell us about something that went wrong? Use this error /// type to do that. Use this alias to be safe from future changes in /// our internal error handling, since we'll implement any necessary @@ -329,16 +328,25 @@ where // Match up the ConfigOptions and fill in their values if we // have a matching entry. for opt in self.options.iter_mut() { - if let Some(val) = call.options.get(opt.name()) { - opt.value = Some(match (opt.default(), &val) { - (OValue::String(_), JValue::String(s)) => OValue::String(s.clone()), - (OValue::Integer(_), JValue::Number(n)) => OValue::Integer(n.as_i64().unwrap()), - (OValue::Boolean(_), JValue::Bool(n)) => OValue::Boolean(*n), + let val = call.options.get(opt.name()); + opt.value = match (&opt, &opt.default(), &val) { + (_, OValue::String(_), Some(JValue::String(s))) => Some(OValue::String(s.clone())), + (_, OValue::OptString, Some(JValue::String(s))) => Some(OValue::String(s.clone())), + (_, OValue::OptString, None) => None, - // It's ok to panic, if we get here Core Lightning - // has not enforced the option type. - (_, _) => panic!("Mismatching types in options: {:?} != {:?}", opt, val), - }); + (_, OValue::Integer(_), Some(JValue::Number(s))) => { + Some(OValue::Integer(s.as_i64().unwrap())) + } + (_, OValue::OptInteger, Some(JValue::Number(s))) => { + Some(OValue::Integer(s.as_i64().unwrap())) + } + (_, OValue::OptInteger, None) => None, + + (_, OValue::Boolean(_), Some(JValue::Bool(s))) => Some(OValue::Boolean(*s)), + (_, OValue::OptBoolean, Some(JValue::Bool(s))) => Some(OValue::Boolean(*s)), + (_, OValue::OptBoolean, None) => None, + + (o, _, _) => panic!("Type mismatch for option {:?}", o), } } diff --git a/plugins/src/options.rs b/plugins/src/options.rs index 587686c24..ace8f1079 100644 --- a/plugins/src/options.rs +++ b/plugins/src/options.rs @@ -1,11 +1,14 @@ use serde::ser::{SerializeStruct, Serializer}; -use serde::{Serialize}; +use serde::Serialize; #[derive(Clone, Debug)] pub enum Value { String(String), Integer(i64), Boolean(bool), + OptString, + OptInteger, + OptBoolean, } /// An stringly typed option that is passed to @@ -45,11 +48,19 @@ impl Serialize for ConfigOption { s.serialize_field("type", "int")?; s.serialize_field("default", i)?; } - Value::Boolean(b) => { s.serialize_field("type", "bool")?; s.serialize_field("default", b)?; } + Value::OptString => { + s.serialize_field("type", "string")?; + } + Value::OptInteger => { + s.serialize_field("type", "int")?; + } + Value::OptBoolean => { + s.serialize_field("type", "bool")?; + } } s.serialize_field("description", &self.description)?;