commit 2516de8e583b0d65d31cb7a33294d28d067f8e56
parent bb5869f9b38815a998eb67e4bfcf622d9166f9a0
Author: MTRNord <mtrnord1@gmail.com>
Date: Fri, 8 Jan 2021 15:50:20 +0100
fix: use User defined config and use a constraint instead of using a generic
Diffstat:
5 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/example-bot/src/commands/hello_world.rs b/example-bot/src/commands/hello_world.rs
@@ -1,15 +1,18 @@
+use crate::config::Config;
use crate::errors::Error;
use matrix_sdk::events::{room::message::MessageEventContent, AnyMessageEventContent};
use mrsbfh::commands::command;
-use mrsbfh::config::Config;
#[command(help = "`!hello_world` - Prints \"hello world\".")]
-pub async fn hello_world<C: Config>(
+pub async fn hello_world<'a>(
tx: mrsbfh::Sender,
- _config: C,
+ _config: Config<'a>,
_sender: String,
mut _args: Vec<&str>,
-) -> Result<(), Error> {
+) -> Result<(), Error>
+where
+ Config<'a>: mrsbfh::config::Loader + Clone,
+{
let content =
AnyMessageEventContent::RoomMessage(MessageEventContent::notice_plain("Hello World!"));
diff --git a/example-bot/src/commands/mod.rs b/example-bot/src/commands/mod.rs
@@ -1,3 +1,4 @@
+use crate::config::Config;
use crate::errors::Error;
use mrsbfh::commands::command_generate;
diff --git a/example-bot/src/main.rs b/example-bot/src/main.rs
@@ -1,6 +1,6 @@
use crate::config::Config;
use clap::Clap;
-use mrsbfh::config::Config as _;
+use mrsbfh::config::Loader;
use std::error::Error;
use tracing::*;
diff --git a/mrsbfh-macros/src/lib.rs b/mrsbfh-macros/src/lib.rs
@@ -10,7 +10,7 @@ use syn::spanned::Spanned;
///
/// ```compile_fail
/// #[command(help = "Description")]
-/// async fn hello_world<C: mrsbfh::config::Config>(mut tx: mrsbfh::Sender, config: C, sender: String, mut args: Vec<&str>) -> Result<(), Box<dyn std::error::Error>> {}
+/// async fn hello_world(mut tx: mrsbfh::Sender, config: Config, sender: String, mut args: Vec<&str>) -> Result<(), Box<dyn std::error::Error>> where Config: mrsbfh::config::Loader + Clone {}
/// ```
#[proc_macro_attribute]
pub fn command(args: TokenStream, input: TokenStream) -> TokenStream {
@@ -156,7 +156,7 @@ pub fn command_generate(args: TokenStream, input: TokenStream) -> TokenStream {
Ok(())
}
- pub async fn match_command<C: mrsbfh::config::Config + Clone>(cmd: &str, config: C, tx: mrsbfh::Sender, sender: String, args: Vec<&str>,) -> Result<(), Error> {
+ pub async fn match_command<'a>(cmd: &str, config: Config<'a>, tx: mrsbfh::Sender, sender: String, args: Vec<&str>,) -> Result<(), Error> where Config<'a>: mrsbfh::config::Loader + Clone {
match cmd {
#(#commands)*
"help" => {
@@ -180,7 +180,7 @@ pub fn config_derive(input: TokenStream) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let expanded = quote! {
- impl #impl_generics mrsbfh::config::Config for #name #ty_generics #where_clause {
+ impl #impl_generics mrsbfh::config::Loader for #name #ty_generics #where_clause {
fn load<P: AsRef<std::path::Path> + std::fmt::Debug>(path: P) -> Result<Self, mrsbfh::errors::ConfigError> {
let contents = std::fs::read_to_string(path)?;
let config: Self = mrsbfh::serde_yaml::from_str(&contents)?;
diff --git a/mrsbfh/src/config.rs b/mrsbfh/src/config.rs
@@ -37,7 +37,7 @@ use std::path::Path;
use crate::errors::ConfigError;
pub use mrsbfh_macros::ConfigDerive;
-pub trait Config {
+pub trait Loader {
fn load<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Result<Self, ConfigError>
where
Self: Sized + Serialize + DeserializeOwned;