From e1d512e82f5087e441ffb7db6800a8a4c0c74006 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 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 | 4 +++- src/types.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 4f85311..2b2e935 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,11 +8,13 @@ description = "Reimplementation of the Scansion project in Rust" [dependencies] nom = "7.1" -jid = { version = "*", features = [ "minidom" ] } +jid = { version = "*", features = [ "minidom", "quote" ] } minidom = "*" xmpp-parsers = "0.20" nom_locate = "4.0.0" rand = "0.8" +proc-macro2 = "*" +quote = "1.0" # [patch.crates-io] # jid = { path = "../xmpp-rs/jid" } diff --git a/src/types.rs b/src/types.rs index 26f39ac..97ad1ee 100644 --- a/src/types.rs +++ b/src/types.rs @@ -7,6 +7,8 @@ use std::collections::HashMap; use jid::Jid; +use proc_macro2::TokenStream; +use quote::{quote, ToTokens}; #[derive(Debug, PartialEq)] pub enum VariableAttr { @@ -67,11 +69,37 @@ impl Client { } } +impl ToTokens for Client { + fn to_tokens(&self, tokens: &mut TokenStream) { + let custom_host = if self.custom_host.is_none() { quote! { None } } else { quote! { #self.custom_host } }; + let custom_port = if self.custom_port.is_none() { quote! { None } } else { quote! { #self.custom_port } }; + + let t = quote! { + Client { + jid: #self.jid, + password: String::from(#self.password), + custom_host: #custom_host, + custom_port: #custom_port, + } + }; + + tokens.extend(t); + } +} + #[derive(Debug, Clone, PartialEq)] pub enum Entity { Client(Client), } +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)]