xmpp-rs/xso-proc/src/field/namespace.rs

82 lines
2.2 KiB
Rust
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! 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})
}
}