1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
//! Strong typed xml, based on xmlparser.
//!
//! ## Quick Start
//!
//! ```rust
//! use std::borrow::Cow;
//! use hard_xml::{XmlRead, XmlWrite};
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent<'a> {
//! #[xml(attr = "attr1")]
//! attr1: Cow<'a, str>,
//! #[xml(attr = "attr2")]
//! attr2: Option<Cow<'a, str>>,
//! #[xml(child = "child")]
//! child: Vec<Child<'a>>,
//! }
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "child")]
//! struct Child<'a> {
//! #[xml(text)]
//! text: Cow<'a, str>,
//! }
//!
//! assert_eq!(
//! (Parent { attr1: "val".into(), attr2: None, child: vec![] }).to_string().unwrap(),
//! r#"<parent attr1="val"/>"#
//! );
//!
//! assert_eq!(
//! Parent::from_str(r#"<parent attr1="val" attr2="val"><child></child></parent>"#).unwrap(),
//! Parent { attr1: "val".into(), attr2: Some("val".into()), child: vec![Child { text: "".into() }] }
//! );
//! ```
//!
//! ## Attributes
//!
//! ### `#[xml(strict(...))]`
//!
//! Opt-in to stricter input handling.
//!
//! ```rust
//! #[derive(hard_xml::XmlWrite, hard_xml::XmlRead, PartialEq, Debug)]
//! #[xml(tag = "ex")]
//! #[xml(strict(unknown_attribute, unknown_element))]
//! struct Ex {
//! // ...
//! }
//! ```
//!
//! #### Strict Options
//!
//! ##### `strict(unknown_attribute)`
//!
//! Fail to parse if an unknown attribute is encountered.
//!
//! ```rust
//! # use hard_xml::XmlRead;
//! #[derive(Debug, hard_xml::XmlRead)]
//! #[xml(tag = "ex")]
//! #[xml(strict(unknown_attribute))]
//! struct Ex {
//! }
//!
//! assert_eq!(
//! Ex::from_str("<ex foo='bar'/>").unwrap_err().to_string(),
//! r#"unknown field "foo" in element "Ex""#);
//! ```
//!
//! ##### `strict(unknown_element)`
//!
//! Fail to parse if an unknown child element is encountered.
//!
//! ```rust
//! # use hard_xml::XmlRead;
//! #[derive(Debug, hard_xml::XmlRead)]
//! #[xml(tag = "ex")]
//! #[xml(strict(unknown_element))]
//! struct Ex {
//! }
//!
//! assert_eq!(
//! Ex::from_str("<ex><unknown/></ex>").unwrap_err().to_string(),
//! r#"unknown field "unknown" in element "Ex""#);
//! ```
//!
//! ### `#[xml(tag = "")]`
//!
//! Specifies the xml tag of a struct or an enum variant.
//!
//! ```rust
//! # use hard_xml::{XmlRead, XmlWrite};
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent {}
//!
//! assert_eq!(
//! (Parent {}).to_string().unwrap(),
//! r#"<parent/>"#
//! );
//!
//! assert_eq!(
//! Parent::from_str(r#"<parent/>"#).unwrap(),
//! Parent {}
//! );
//! ```
//!
//! ```rust
//! # use hard_xml::{XmlRead, XmlWrite};
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "tag1")]
//! struct Tag1 {}
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "tag2")]
//! struct Tag2 {}
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! enum Tag {
//! #[xml(tag = "tag1")]
//! Tag1(Tag1),
//! #[xml(tag = "tag2")]
//! Tag2(Tag2),
//! }
//!
//! assert_eq!(
//! (Tag::Tag1(Tag1 {})).to_string().unwrap(),
//! r#"<tag1/>"#
//! );
//!
//! assert_eq!(
//! Tag::from_str(r#"<tag2></tag2>"#).unwrap(),
//! Tag::Tag2(Tag2 {})
//! );
//! ```
//!
//! ### `#[xml(attr = "")]`
//!
//! Specifies that a struct field is attribute. Support
//! `Cow<str>`, `Option<Cow<str>>`, `T` and `Option<T>`
//! where `T: FromStr + Display`.
//!
//! ```rust
//! use hard_xml::{XmlRead, XmlWrite};
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent {
//! #[xml(attr = "attr")]
//! attr: usize
//! }
//!
//! assert_eq!(
//! (Parent { attr: 42 }).to_string().unwrap(),
//! r#"<parent attr="42"/>"#
//! );
//!
//! assert_eq!(
//! Parent::from_str(r#"<parent attr="48"></parent>"#).unwrap(),
//! Parent { attr: 48 }
//! );
//! ```
//!
//! ### `#[xml(attr = "" with = "")]`
//!
//! Parse and serialize the field with the module specified as an argument. That module must provide the following interface given that the fields is of type `T`.
//!
//! ```ignore
//! mod some_module_name {
//! fn from_xml(mode: &str) -> hard_xml::XmlResult<T>;
//! fn to_xml(mode: &T) -> hard_xml::XmlResult<impl std::convert::AsRef<str>>;
//! }
//! ```
//!
//! #### Example
//!
//! ```rust
//! # use hard_xml::{XmlRead, XmlWrite};
//! #
//! mod parse_drive_mode {
//! pub fn from_xml(mode: &str) -> hard_xml::XmlResult<bool> {
//! match mode {
//! "agile" => Ok(false),
//! "fast" => Ok(true),
//! _ => Err(hard_xml::XmlError::FromStr(
//! format!("Expected mode to be agile or fast, got {mode:?}").into())),
//! }
//! }
//!
//! pub fn to_xml(mode: &bool) -> hard_xml::XmlResult<&'static str> {
//! match mode {
//! false => Ok("agile"),
//! true => Ok("fast"),
//! }
//! }
//! }
//!
//! #[derive(XmlRead, XmlWrite, PartialEq, Debug)]
//! #[xml(tag = "spaceship")]
//! struct Spaceship {
//! #[xml(attr = "drive_mode", with = "parse_drive_mode")]
//! drive_mode: bool,
//! }
//!
//! assert_eq!(
//! Spaceship::from_str(r#"<spaceship drive_mode="agile"/>"#).unwrap(),
//! Spaceship {
//! drive_mode: false,
//! }
//! );
//!
//! // match with a reversed string in from_xml of withmod.
//! assert_eq!(
//! Spaceship {
//! drive_mode: true,
//! }.to_string().unwrap(),
//! r#"<spaceship drive_mode="fast"/>"#,
//! );
//! ```
//!
//! ### `#[xml(child = "")]`
//!
//! Specifies that a struct field is a child element. Support
//! `T`, `Option<T>`, `Vec<T>` where `T: XmlRead + XmlWrite`.
//!
//! ```rust
//! use hard_xml::{XmlRead, XmlWrite};
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "tag1")]
//! struct Tag1 {}
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "tag2")]
//! struct Tag2 {}
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "tag3")]
//! struct Tag3 {}
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! enum Tag12 {
//! #[xml(tag = "tag1")]
//! Tag1(Tag1),
//! #[xml(tag = "tag2")]
//! Tag2(Tag2),
//! }
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent {
//! #[xml(child = "tag3")]
//! tag3: Vec<Tag3>,
//! #[xml(child = "tag1", child = "tag2")]
//! tag12: Option<Tag12>
//! }
//!
//! assert_eq!(
//! (Parent { tag3: vec![Tag3 {}], tag12: None }).to_string().unwrap(),
//! r#"<parent><tag3/></parent>"#
//! );
//!
//! assert_eq!(
//! Parent::from_str(r#"<parent><tag2></tag2></parent>"#).unwrap(),
//! Parent { tag3: vec![], tag12: Some(Tag12::Tag2(Tag2 {})) }
//! );
//! ```
//!
//! ### `#[xml(text)]`
//!
//! Specifies that a struct field is text content.
//! Support `Cow<str>`, `Vec<Cow<str>>`, `Option<Cow<str>>`,
//! `T`, `Vec<T>`, `Option<T>` where `T: FromStr + Display`.
//!
//! ```rust
//! use std::borrow::Cow;
//! use hard_xml::{XmlRead, XmlWrite};
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent<'a> {
//! #[xml(text)]
//! content: Cow<'a, str>,
//! }
//!
//! assert_eq!(
//! (Parent { content: "content".into() }).to_string().unwrap(),
//! r#"<parent>content</parent>"#
//! );
//!
//! assert_eq!(
//! Parent::from_str(r#"<parent></parent>"#).unwrap(),
//! Parent { content: "".into() }
//! );
//! ```
//!
//! ### `#[xml(flatten_text = "")]`
//!
//! Specifies that a struct field is child text element.
//! Support `Cow<str>`, `Vec<Cow<str>>`, `Option<Cow<str>>`,
//! `T`, `Vec<T>`, `Option<T>` where `T: FromStr + Display`.
//!
//! ```rust
//! use std::borrow::Cow;
//! use hard_xml::{XmlRead, XmlWrite};
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent<'a> {
//! #[xml(flatten_text = "child")]
//! content: Cow<'a, str>,
//! }
//!
//! assert_eq!(
//! (Parent { content: "content".into() }).to_string().unwrap(),
//! r#"<parent><child>content</child></parent>"#
//! );
//!
//! assert_eq!(
//! Parent::from_str(r#"<parent><child></child></parent>"#).unwrap(),
//! Parent { content: "".into() }
//! );
//! ```
//!
//! ### `#[xml(cdata)]`
//!
//! Specifies a CDATA text. Should be used together with `text` or `flatten_text`.
//!
//! > `#[xml(cdata)]` only changes the behavior of writing,
//! > text field without `#[xml(cdata)]` can still works with cdata tag.
//!
//! ```rust
//! use std::borrow::Cow;
//! use hard_xml::{XmlRead, XmlWrite};
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent<'a> {
//! #[xml(text, cdata)]
//! content: Cow<'a, str>,
//! }
//!
//! assert_eq!(
//! (Parent { content: "content".into() }).to_string().unwrap(),
//! r#"<parent><![CDATA[content]]></parent>"#
//! );
//! ```
//!
//! ```rust
//! use std::borrow::Cow;
//! use hard_xml::{XmlRead, XmlWrite};
//!
//! #[derive(XmlWrite, XmlRead, PartialEq, Debug)]
//! #[xml(tag = "parent")]
//! struct Parent<'a> {
//! #[xml(flatten_text = "code", cdata)]
//! content: Cow<'a, str>,
//! }
//!
//! assert_eq!(
//! (Parent { content: r#"hello("deities!");"#.into() }).to_string().unwrap(),
//! r#"<parent><code><![CDATA[hello("deities!");]]></code></parent>"#
//! );
//! ```
//!
//! ### `#[xml(default)]`
//!
//! Use `Default::default()` if the value is not present when reading.
//!
//! ```rust
//! use std::borrow::Cow;
//! use hard_xml::XmlRead;
//!
//! #[derive(XmlRead, PartialEq, Debug)]
//! #[xml(tag = "root")]
//! struct Root {
//! #[xml(default, attr = "attr")]
//! attr: bool,
//! }
//!
//! assert_eq!(
//! Root::from_str(r#"<root/>"#).unwrap(),
//! Root { attr: false }
//! );
//!
//! assert_eq!(
//! Root::from_str(r#"<root attr="1"/>"#).unwrap(),
//! Root { attr: true }
//! );
//! ```
#[cfg(feature = "log")]
mod log;
#[cfg(not(feature = "log"))]
mod noop_log;
#[cfg(feature = "log")]
#[doc(hidden)]
pub mod lib {
pub use log;
}
mod xml_error;
mod xml_escape;
mod xml_read;
mod xml_reader;
mod xml_unescape;
mod xml_write;
mod xml_writer;
pub use self::xml_error::{XmlError, XmlResult};
pub use self::xml_read::{XmlRead, XmlReadOwned};
pub use self::xml_reader::XmlReader;
pub use self::xml_write::XmlWrite;
pub use self::xml_writer::XmlWriter;
pub use hard_xml_derive::{XmlRead, XmlWrite};
pub use xmlparser;
pub mod utils {
pub use super::xml_escape::xml_escape;
pub use super::xml_unescape::xml_unescape;
}