mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git
82 lines
2.2 KiB
Rust
82 lines
2.2 KiB
Rust
//! Infrastructure for parsing dynamic namespaces.
|
||
use proc_macro2::TokenStream;
|
||
|
||
use quote::quote;
|
||
use syn::*;
|
||
|
||
use crate::error_message::ParentRef;
|
||
|
||
use super::{Field, FieldParsePart};
|
||
|
||
/// A field holding the dynamic namespace of a compound with
|
||
/// `#[xml(namespace = dyn)]`.
|
||
///
|
||
/// See also
|
||
/// [`StructNamespace::Dyn`][`crate::structs::StructNamespace::Dyn`].
|
||
///
|
||
/// Maps to `#[xml(namespace)]`.
|
||
#[derive(Debug)]
|
||
pub(crate) struct NamespaceField;
|
||
|
||
impl NamespaceField {
|
||
/// Construct a new `#[xml(namespace)]` field.
|
||
pub(super) fn new() -> Self {
|
||
Self
|
||
}
|
||
}
|
||
|
||
impl Field for NamespaceField {
|
||
fn is_namespace(&self) -> bool {
|
||
true
|
||
}
|
||
|
||
/// Construct code which copies the value from the `namespace_tempname`
|
||
/// into the value of the field.
|
||
///
|
||
/// The actual parsing is special, because it has to happen during type
|
||
/// matching, and happens in
|
||
/// [`Compound::build_try_from_element`][`crate::compound::Compound::build_try_from_element`].
|
||
fn build_try_from_element(
|
||
&self,
|
||
_container_name: &ParentRef,
|
||
container_namespace_expr: &Expr,
|
||
_tempname: Ident,
|
||
_member: &Member,
|
||
_ty: &Type,
|
||
) -> Result<FieldParsePart> {
|
||
Ok(FieldParsePart {
|
||
value: quote! { #container_namespace_expr },
|
||
..FieldParsePart::default()
|
||
})
|
||
}
|
||
|
||
/// This is a no-op, because the actual implementation is in
|
||
/// [`crate::compound::DynCompound::build_set_namespace`].
|
||
fn build_set_namespace(
|
||
&self,
|
||
_input: &Ident,
|
||
_ty: &Type,
|
||
_access: Expr,
|
||
) -> Result<TokenStream> {
|
||
// NOTE: does nothing because this is handled specially by
|
||
// Compound::build_set_namespace
|
||
Ok(quote! {})
|
||
}
|
||
|
||
/// This is a no-op.
|
||
///
|
||
/// The actual implementation of the specialities of dynamic namespace
|
||
/// fields resides in
|
||
/// [`Compound::build_into_element`][`crate::compound::Compound::build_into_element`].
|
||
fn build_into_element(
|
||
&self,
|
||
_container_name: &ParentRef,
|
||
_container_namespace_expr: &Expr,
|
||
_member: &Member,
|
||
_ty: &Type,
|
||
_access: Expr,
|
||
) -> Result<TokenStream> {
|
||
Ok(quote! {builder})
|
||
}
|
||
}
|