Add optional quote support for Entity

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-10-29 13:48:15 +01:00
parent 45ddd638f8
commit f2ca095fb6
Signed by: pep
GPG Key ID: DEDA74AEECA9D0F2
2 changed files with 57 additions and 3 deletions

View File

@ -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" }

View File

@ -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<Name, Entity>;
#[derive(Debug, Clone, PartialEq)]