Skip to main content

Form

Struct Form 

Source
pub struct Form<'a> { /* private fields */ }
Expand description

A result element, described but not yet built.

use bumpalo::Bump;
use xsd_schema::compose::{AttrValue, Composer, Content, Form, Name};
use xsd_schema::document::SerializeOptions;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

let mut title = Form::new(Name::local("title"));
title.push(Content::Text("One".to_string()));

let mut book = Form::new(Name::local("book"));
book.attr(Name::local("id"), AttrValue::text("b1"));
book.push(Content::Element(title));

let doc = c.build(book)?;
assert_eq!(
    doc.to_xml(&SerializeOptions::default())?,
    r#"<book id="b1"><title>One</title></book>"#,
);

Implementations§

Source§

impl<'a> Form<'a>

Source

pub fn new(name: Name) -> Self

An element with no declarations, attributes or content.

use xsd_schema::compose::{Form, Name};

let form = Form::new(Name::local("result"));
assert_eq!(form.name().local_name(), "result");
assert!(form.content().is_empty());
Source

pub fn declare(&mut self, prefix: &str, uri: &str) -> &mut Self

Declares a namespace on this element, in scope for it and its descendants.

An empty prefix declares the default namespace; an empty prefix with an empty URI undeclares it. The declaration shadows the composer’s namespace table for this subtree, and is dropped from the output only when it is redundant.

use bumpalo::Bump;
use xsd_schema::compose::{Composer, Form, Name};
use xsd_schema::document::SerializeOptions;
use xsd_schema::namespace::NameTable;

let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);

let mut form = Form::new(Name::prefixed("p", "root"));
form.declare("p", "urn:x");

assert_eq!(
    c.build(form)?.to_xml(&SerializeOptions::default())?,
    r#"<p:root xmlns:p="urn:x"/>"#,
);
Source

pub fn attr(&mut self, name: Name, value: AttrValue<'a>) -> &mut Self

Adds an attribute.

Attributes are written in the order they are added. Two attributes with the same expanded name are a duplicate, and the later one wins.

use xsd_schema::compose::{AttrValue, Form, Name};

let mut form = Form::new(Name::local("book"));
form.attr(Name::local("id"), AttrValue::text("b1"))
    .attr(Name::local("year"), AttrValue::text(1999));
assert_eq!(form.attributes().len(), 2);
Source

pub fn push(&mut self, content: Content<'a>) -> &mut Self

Appends one content item.

use xsd_schema::compose::{Content, Form, IntoContent, Name};

let mut form = Form::new(Name::local("p"));
form.push(Content::Text("a".to_string()))
    .push("b".into_content());
assert_eq!(form.content().len(), 2);
Source

pub fn name(&self) -> &Name

The element’s name.

use xsd_schema::compose::{Form, Name};

let form = Form::new(Name::prefixed("p", "root"));
assert_eq!(form.name().to_string(), "p:root");
Source

pub fn declarations(&self) -> &[(String, String)]

The namespace declarations the author asked for, in order.

use xsd_schema::compose::{Form, Name};

let mut form = Form::new(Name::local("root"));
form.declare("p", "urn:x");
assert_eq!(form.declarations(), [("p".to_string(), "urn:x".to_string())]);
Source

pub fn attributes(&self) -> &[(Name, AttrValue<'a>)]

The attributes, in order.

use xsd_schema::compose::{AttrValue, Form, Name};

let mut form = Form::new(Name::local("book"));
form.attr(Name::local("id"), AttrValue::text("b1"));
assert_eq!(form.attributes()[0].0.local_name(), "id");
Source

pub fn content(&self) -> &[Content<'a>]

The content items, in order.

use xsd_schema::compose::{Content, Form, Name};

let mut form = Form::new(Name::local("p"));
form.push(Content::Text("hi".to_string()));
assert!(matches!(form.content()[0], Content::Text(ref t) if t == "hi"));

Trait Implementations§

Source§

impl<'a> Clone for Form<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Form<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> IntoContent<'a> for Form<'a>

Source§

fn into_content(self) -> Content<'a>

Converts self into one content item.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Form<'a>

§

impl<'a> !Send for Form<'a>

§

impl<'a> !Sync for Form<'a>

§

impl<'a> !UnwindSafe for Form<'a>

§

impl<'a> Forget for Form<'a>

§

impl<'a> Freeze for Form<'a>

§

impl<'a> Move for Form<'a>

§

impl<'a> Unpin for Form<'a>
where *const u8 is !null: Unpin, &'a BufferDocument<'a>: Unpin, &'a SharedClock: Unpin, NonZero<u32>: Unpin, Arc<QNameParts>: Unpin, Box<XmlValue>: Unpin,

§

impl<'a> UnsafeUnpin for Form<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.