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
//! serde deserialization. Fields map to attributes, child elements
//! (`Vec<T>` for repeats) or `$text`; an attribute wins over a same-named child.
use Read;
use ;
use crate::;
pub use ;
pub const TEXT_FIELD: &str = "$text";
/// Deserializes `T` from an element's attributes and optional text.
///
/// Useful when an [`Event::StartTag`](crate::Event::StartTag) is already at
/// hand. Child elements are not available here: use
/// [`AbxParser::deserialize_next`](crate::AbxParser::deserialize_next) for types
/// with child-element fields.
///
/// # Errors
///
/// Returns [`AbxError::Deserialization`] if the attributes do not match `T`.
///
/// # Examples
///
/// ```
/// use android_abx::{Attribute, AttributeValue};
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Pkg {
/// name: String,
/// version: Option<u32>,
/// }
///
/// let attributes = [Attribute {
/// name: "name".into(),
/// value: AttributeValue::String("com.example".into()),
/// }];
/// let pkg: Pkg = android_abx::from_element(&attributes, None)?;
/// assert_eq!(pkg.name, "com.example");
/// assert_eq!(pkg.version, None);
/// # Ok::<(), android_abx::AbxError>(())
/// ```
/// Deserializes the root element of an ABX document into `T`.
///
/// The root's tag name is not checked. See
/// [Deserializing with serde](crate#deserializing-with-serde) for how fields are
/// matched.
///
/// # Errors
///
/// Returns a parse error if the input is malformed, or
/// [`AbxError::Deserialization`] if the document has no root element or it does
/// not match `T`.
///
/// # Examples
///
/// ```
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct Pkg {
/// name: String,
/// version: u32,
/// }
///
/// # let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/simple_pkg.abx"));
/// let pkg: Pkg = android_abx::from_slice(data)?;
/// assert_eq!(pkg.name, "com.example.chat");
/// assert_eq!(pkg.version, 3);
/// # Ok::<(), android_abx::AbxError>(())
/// ```
/// Deserializes the root element of an ABX document read from `reader` into `T`.
///
/// See [`from_slice`].
///
/// # Errors
///
/// Same as [`from_slice`], plus [`AbxError::Io`] if reading fails.
/// Deserializes the root element of an ABX file into `T`.
///
/// See [`from_slice`].
///
/// # Errors
///
/// Same as [`from_slice`], plus [`AbxError::Io`] if the file cannot be opened or
/// read.
///
/// # Examples
///
/// ```no_run
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct User {
/// id: u32,
/// name: String,
/// }
///
/// let user: User = android_abx::from_file("/data/system/users/0.xml")?;
/// # Ok::<(), android_abx::AbxError>(())
/// ```