Debian Codegraph @

src/common.rs

raw symbols SCIP
1 //! Contains common types and functions used throughout the library.
2
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
4
5 /// Represents a position inside some textual document.
6 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
7 pub struct TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# {
8 /// Row, counting from 0
9 pub rowrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#row.: u64,
10 /// Column, counting from 0
11 pub columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column.: u64,
12 }
13
14 impl TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# {
15 /// Creates a new position initialized to the beginning of the document
16 #[inline]
17 #[must_use]
18 pub fn newrust-analyzer cargo xml-rs 0.8.19 common/impl#[TextPosition]new().() -> TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# {
19 TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# { rowrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#row.: 0, columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column.: 0 }
20 }
21
22 /// Advances the position in a line
23 #[inline]
24 pub fn advancerust-analyzer cargo xml-rs 0.8.19 common/impl#[TextPosition]advance().(&mut self, count: u8) {
25 self.columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u8>`]from().(count);
26 }
27
28 /// Advances the position in a line to the next tab position
29 #[inline]
30 pub fn advance_to_tabrust-analyzer cargo xml-rs 0.8.19 common/impl#[TextPosition]advance_to_tab().(&mut self, width: u8) {
31 let width = u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u8>`]from().(width);
32 self.columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Sub<Self>`]sub().self.columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem().width;
33 }
34
35 /// Advances the position to the beginning of the next line
36 #[inline]
37 pub fn new_linerust-analyzer cargo xml-rs 0.8.19 common/impl#[TextPosition]new_line().(&mut self) {
38 self.columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column. = 0;
39 self.rowrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#row. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1;
40 }
41 }
42
43 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# {
44 #[cold]
45 fn fmtrust-analyzer cargo xml-rs 0.8.19 common/impl#[TextPosition][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
46 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}:{}", self.rowrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#row. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). 1, self.columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). 1)
47 }
48 }
49
50 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# {
51 #[inline]
52 fn fmtrust-analyzer cargo xml-rs 0.8.19 common/impl#[TextPosition][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
53 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{}:{}", self.rowrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#row. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). 1, self.columnrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#column. +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). 1)
54 }
55 }
56
57 /// Get the position in the document corresponding to the object
58 ///
59 /// This trait is implemented by parsers, lexers and errors.
60 pub trait Positionrust-analyzer cargo xml-rs 0.8.19 common/Position# {
61 /// Returns the current position or a position corresponding to the object.
62 fn positionrust-analyzer cargo xml-rs 0.8.19 common/Position#position().(&self) -> TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition#;
63 }
64
65 impl Positionrust-analyzer cargo xml-rs 0.8.19 common/Position# for TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# {
66 #[inline]
67 fn positionrust-analyzer cargo xml-rs 0.8.19 common/impl#[TextPosition][Position]position().(&self) -> TextPositionrust-analyzer cargo xml-rs 0.8.19 common/TextPosition# {
68 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self
69 }
70 }
71
72 /// XML version enumeration.
73 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:)]
74 pub enum XmlVersionrust-analyzer cargo xml-rs 0.8.19 common/XmlVersion# {
75 /// XML version 1.0.
76 Version10rust-analyzer cargo xml-rs 0.8.19 common/XmlVersion#Version10#,
77
78 /// XML version 1.1.
79 Version11rust-analyzer cargo xml-rs 0.8.19 common/XmlVersion#Version11#,
80 }
81
82 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for XmlVersionrust-analyzer cargo xml-rs 0.8.19 common/XmlVersion# {
83 fn fmtrust-analyzer cargo xml-rs 0.8.19 common/impl#[XmlVersion][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
84 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
85 XmlVersionrust-analyzer cargo xml-rs 0.8.19 common/XmlVersion#::Version10rust-analyzer cargo xml-rs 0.8.19 common/XmlVersion#Version10# => "1.0",
86 XmlVersionrust-analyzer cargo xml-rs 0.8.19 common/XmlVersion#::Version11rust-analyzer cargo xml-rs 0.8.19 common/XmlVersion#Version11# => "1.1",
87 }.fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[str][Display]fmt().(f)
88 }
89 }
90
91 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for XmlVersionrust-analyzer cargo xml-rs 0.8.19 common/XmlVersion# {
92 #[cold]
93 fn fmtrust-analyzer cargo xml-rs 0.8.19 common/impl#[XmlVersion][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
94 fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#::fmtrust-analyzer cargo xml-rs 0.8.19 common/impl#[XmlVersion][Display]fmt().(self, f)
95 }
96 }
97
98 /// Checks whether the given character is a white space character (`S`)
99 /// as is defined by XML 1.1 specification, [section 2.3][1].
100 ///
101 /// [1]: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn
102 #[must_use]
103 #[inline]
104 pub fn is_whitespace_charrust-analyzer cargo xml-rs 0.8.19 common/is_whitespace_char().(c: char) -> bool {
105 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, '\x20' | '\x0a' | '\x09' | '\x0d')
106 }
107
108 /// Checks whether the given string is compound only by white space
109 /// characters (`S`) using the previous `is_whitespace_char` to check
110 /// all characters of this string
111 pub fn is_whitespace_strrust-analyzer cargo xml-rs 0.8.19 common/is_whitespace_str().(s: &str) -> bool {
112 s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#all().(is_whitespace_charrust-analyzer cargo xml-rs 0.8.19 common/is_whitespace_char().)
113 }
114
115 #[must_use] pub fn is_xml10_charrust-analyzer cargo xml-rs 0.8.19 common/is_xml10_char().(c: char) -> bool {
116 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, '\u{09}' | '\u{0A}' | '\u{0D}' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..)
117 }
118
119 #[must_use] pub fn is_xml11_charrust-analyzer cargo xml-rs 0.8.19 common/is_xml11_char().(c: char) -> bool {
120 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, '\u{01}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..)
121 }
122
123 #[must_use] pub fn is_xml11_char_not_restrictedrust-analyzer cargo xml-rs 0.8.19 common/is_xml11_char_not_restricted().(c: char) -> bool {
124 is_xml11_charrust-analyzer cargo xml-rs 0.8.19 common/is_xml11_char().(c) && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, '\u{01}'..='\u{08}' | '\u{0B}'..='\u{0C}' | '\u{0E}'..='\u{1F}' | '\u{7F}'..='\u{84}' | '\u{86}'..='\u{9F}')
125 }
126
127 /// Checks whether the given character is a name start character (`NameStartChar`)
128 /// as is defined by XML 1.1 specification, [section 2.3][1].
129 ///
130 /// [1]: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn
131 #[must_use]
132 pub fn is_name_start_charrust-analyzer cargo xml-rs 0.8.19 common/is_name_start_char().(c: char) -> bool {
133 match c {
134 ':' | 'A'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'Z' | '_' | 'a'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'z' |
135 '\u{C0}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{D6}' | '\u{D8}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{F6}' | '\u{F8}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{2FF}' |
136 '\u{370}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{37D}' | '\u{37F}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{1FFF}' |
137 '\u{200C}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{200D}' | '\u{2070}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{218F}' |
138 '\u{2C00}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{2FEF}' | '\u{3001}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{D7FF}' |
139 '\u{F900}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{FDCF}' | '\u{FDF0}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{FFFD}' |
140 '\u{10000}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{EFFFF}' => true,
141 _ => false
142 }
143 }
144
145 /// Checks whether the given character is a name character (`NameChar`)
146 /// as is defined by XML 1.1 specification, [section 2.3][1].
147 ///
148 /// [1]: http://www.w3.org/TR/2006/REC-xml11-20060816/#sec-common-syn
149 #[must_use]
150 pub fn is_name_charrust-analyzer cargo xml-rs 0.8.19 common/is_name_char().(c: char) -> bool {
151 match c {
152 _ if is_name_start_charrust-analyzer cargo xml-rs 0.8.19 common/is_name_start_char().(c) => true,
153 '-' | '.' | '0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'9' | '\u{B7}' |
154 '\u{300}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{36F}' | '\u{203F}'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'\u{2040}' => true,
155 _ => false
156 }
157 }