From f2ca095fb66a6f75ccbc06c9919f9b8c97dbd980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 29 Oct 2023 13:48:15 +0100 Subject: [PATCH] Add optional quote support for Entity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- Cargo.toml | 12 +++++++++--- src/types.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f85311..e633725 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,13 @@ minidom = "*" xmpp-parsers = "0.20" nom_locate = "4.0.0" rand = "0.8" +quote = { version = "1.0", optional = true } +proc-macro2 = { version = "1.0", optional = true } -# [patch.crates-io] -# jid = { path = "../xmpp-rs/jid" } -# minidom = { path = "../xmpp-rs/minidom" } +[features] +quote = ["dep:quote", "dep:proc-macro2", "jid/quote"] + +[patch.crates-io] +jid = { git = "https://gitlab.com/xmpp-rs/xmpp-rs" } +minidom = { git = "https://gitlab.com/xmpp-rs/xmpp-rs" } +xmpp-parsers = { git = "https://gitlab.com/xmpp-rs/xmpp-rs" } diff --git a/src/types.rs b/src/types.rs index 26f39ac..22310d8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -7,6 +7,10 @@ use std::collections::HashMap; use jid::Jid; +#[cfg(feature = "quote")] +use proc_macro2::TokenStream; +#[cfg(feature = "quote")] +use quote::{quote, ToTokens}; #[derive(Debug, PartialEq)] pub enum VariableAttr { @@ -67,11 +71,55 @@ impl Client { } } +#[cfg(feature = "quote")] +impl ToTokens for Client { + fn to_tokens(&self, tokens: &mut TokenStream) { + let jid = &self.jid; + let password = &self.password; + let custom_host = { + let tmp = &self.custom_host; + if self.custom_host.is_none() { + quote! { None } + } else { + quote! { #tmp } + } + }; + let custom_port = { + let tmp = &self.custom_port; + if self.custom_port.is_none() { + quote! { None } + } else { + quote! { #tmp } + } + }; + + let t = quote! { + Client { + jid: #jid, + password: String::from(#password), + custom_host: #custom_host, + custom_port: #custom_port, + } + }; + + tokens.extend(t); + } +} + #[derive(Debug, Clone, PartialEq)] pub enum Entity { Client(Client), } +#[cfg(feature = "quote")] +impl ToTokens for Entity { + fn to_tokens(&self, tokens: &mut TokenStream) { + tokens.extend(match self { + Entity::Client(client) => quote! { Entity::Client(#client) }, + }); + } +} + pub type Context = HashMap; #[derive(Debug, Clone, PartialEq)]