Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! `json_comments` is a library to strip out comments from JSON-like test. By processing text
2 //! through a [`StripComments`] adapter first, it is possible to use a standard JSON parser (such
3 //! as [serde_json](https://crates.io/crates/serde_json) with quasi-json input that contains
4 //! comments.
5 //!
6 //! In fact, this code makes few assumptions about the input and could probably be used to strip
7 //! comments out of other types of code as well, provided that strings use double quotes and
8 //! backslashes are used for escapes in strings.
9 //!
10 //! The following types of comments are supported:
11 //! - C style block comments (`/* ... */`)
12 //! - C style line comments (`// ...`)
13 //! - Shell style line comments (`# ...`)
14 //!
15 //! ## Example using serde_json
16 //!
17 //! ```
18 //! use serde_json::{Result, Value};
19 //! use json_comments::StripComments;
20 //!
21 //! # fn main() -> Result<()> {
22 //! // Some JSON input data as a &str. Maybe this comes form the user.
23 //! let data = r#"
24 //! {
25 //! "name": /* full */ "John Doe",
26 //! "age": 43,
27 //! "phones": [
28 //! "+44 1234567", // work phone
29 //! "+44 2345678" // home phone
30 //! ]
31 //! }"#;
32 //!
33 //! // Strip the comments from the input (use `as_bytes()` to get a `Read`).
34 //! let stripped = StripComments::new(data.as_bytes());
35 //! // Parse the string of data into serde_json::Value.
36 //! let v: Value = serde_json::from_reader(stripped)?;
37 //!
38 //! println!("Please call {} at the number {}", v["name"], v["phones"][0]);
39 //!
40 //! # Ok(())
41 //! # }
42 //! ```
43 //!
44 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#};
45
46 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, 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:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
47 enum Staterust-analyzer cargo json_comments 0.2.2 State# {
48 Toprust-analyzer cargo json_comments 0.2.2 State#Top#,
49 InStringrust-analyzer cargo json_comments 0.2.2 State#InString#,
50 StringEscaperust-analyzer cargo json_comments 0.2.2 State#StringEscape#,
51 InCommentrust-analyzer cargo json_comments 0.2.2 State#InComment#,
52 InBlockCommentrust-analyzer cargo json_comments 0.2.2 State#InBlockComment#,
53 MaybeCommentEndrust-analyzer cargo json_comments 0.2.2 State#MaybeCommentEnd#,
54 InLineCommentrust-analyzer cargo json_comments 0.2.2 State#InLineComment#,
55 }
56
57 use Staterust-analyzer cargo json_comments 0.2.2 State#::*;
58
59 /// A [`Read`] that transforms another [`Read`] so that it changes all comments to spaces so that a downstream json parser
60 /// (such as json-serde) doesn't choke on them.
61 ///
62 /// The supported comments are:
63 /// - C style block comments (`/* ... */`)
64 /// - C style line comments (`// ...`)
65 /// - Shell style line comments (`# ...`)
66 ///
67 /// ## Example
68 /// ```
69 /// use json_comments::StripComments;
70 /// use std::io::Read;
71 ///
72 /// let input = r#"{
73 /// // c line comment
74 /// "a": "comment in string /* a */",
75 /// ## shell line comment
76 /// } /** end */"#;
77 ///
78 /// let mut stripped = String::new();
79 /// StripComments::new(input.as_bytes()).read_to_string(&mut stripped).unwrap();
80 ///
81 /// assert_eq!(stripped, "{
82 /// \n\"a\": \"comment in string /* a */\",
83 /// \n} ");
84 ///
85 /// ```
86 ///
87 pub struct StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#<T: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#> {
88 innerrust-analyzer cargo json_comments 0.2.2 StripComments#inner.: T,
89 staterust-analyzer cargo json_comments 0.2.2 StripComments#state.: Staterust-analyzer cargo json_comments 0.2.2 State#,
90 settingsrust-analyzer cargo json_comments 0.2.2 StripComments#settings.: CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#,
91 }
92
93 impl<T> StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#<T>
94 where
95 T: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#,
96 {
97 pub fn newrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`]new().(input: T) -> Selfrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`] {
98 Selfrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`] {
99 innerrust-analyzer cargo json_comments 0.2.2 StripComments#inner.: input,
100 staterust-analyzer cargo json_comments 0.2.2 StripComments#state.: Toprust-analyzer cargo json_comments 0.2.2 State#Top#,
101 settingsrust-analyzer cargo json_comments 0.2.2 StripComments#settings.: CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#::defaultrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings][Default]default().(),
102 }
103 }
104
105 /// Create a new `StripComments` with settings which may be different from the default.
106 ///
107 /// This is useful if you wish to disable allowing certain kinds of comments.
108 #[inline]
109 pub fn with_settingsrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`]with_settings().(settings: CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#, input: T) -> Selfrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`] {
110 Selfrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`] {
111 innerrust-analyzer cargo json_comments 0.2.2 StripComments#inner.: input,
112 staterust-analyzer cargo json_comments 0.2.2 StripComments#state.: Toprust-analyzer cargo json_comments 0.2.2 State#Top#,
113 settingsrust-analyzer cargo json_comments 0.2.2 StripComments#settings.,
114 }
115 }
116 }
117
118 macro_rules! invalid_datarust-analyzer cargo json_comments 0.2.2 invalid_data! {
119 () => {
120 return Err(ErrorKind::InvalidData.into())
121 };
122 }
123
124 impl<T> Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# for StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#<T>
125 where
126 T: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#,
127 {
128 fn readrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`][Read]read().(&mut self, buf: &mut [u8]) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> {
129 let count = self.innerrust-analyzer cargo json_comments 0.2.2 StripComments#inner..readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read().(buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
130 if count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 {
131 for c in buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#count]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() {
132 self.staterust-analyzer cargo json_comments 0.2.2 StripComments#state. = match self.staterust-analyzer cargo json_comments 0.2.2 StripComments#state. {
133 Toprust-analyzer cargo json_comments 0.2.2 State#Top# => toprust-analyzer cargo json_comments 0.2.2 top().(c, &self.settingsrust-analyzer cargo json_comments 0.2.2 StripComments#settings.),
134 InStringrust-analyzer cargo json_comments 0.2.2 State#InString# => in_stringrust-analyzer cargo json_comments 0.2.2 in_string().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c),
135 StringEscaperust-analyzer cargo json_comments 0.2.2 State#StringEscape# => InStringrust-analyzer cargo json_comments 0.2.2 State#InString#,
136 InCommentrust-analyzer cargo json_comments 0.2.2 State#InComment# => in_commentrust-analyzer cargo json_comments 0.2.2 in_comment().(c, &self.settingsrust-analyzer cargo json_comments 0.2.2 StripComments#settings.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
137 InBlockCommentrust-analyzer cargo json_comments 0.2.2 State#InBlockComment# => in_block_commentrust-analyzer cargo json_comments 0.2.2 in_block_comment().(c),
138 MaybeCommentEndrust-analyzer cargo json_comments 0.2.2 State#MaybeCommentEnd# => maybe_comment_endrust-analyzer cargo json_comments 0.2.2 maybe_comment_end().(c),
139 InLineCommentrust-analyzer cargo json_comments 0.2.2 State#InLineComment# => in_line_commentrust-analyzer cargo json_comments 0.2.2 in_line_comment().(c),
140 }
141 }
142 } else if self.staterust-analyzer cargo json_comments 0.2.2 StripComments#state. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Toprust-analyzer cargo json_comments 0.2.2 State#Top# && self.staterust-analyzer cargo json_comments 0.2.2 StripComments#state. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().InLineCommentrust-analyzer cargo json_comments 0.2.2 State#InLineComment# {
143 invalid_datarust-analyzer cargo json_comments 0.2.2 invalid_data!!();
144 }
145 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(count)
146 }
147 }
148
149 /// Settings for `StripComments`
150 ///
151 /// The default is for all comment types to be enabled.
152 #[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:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
153 pub struct CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings# {
154 /// True if c-style block comments (`/* ... */`) are allowed
155 block_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#block_comments.: bool,
156 /// True if c-style `//` line comments are allowed
157 slash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#slash_line_comments.: bool,
158 /// True if shell-style `#` line comments are allowed
159 hash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#hash_line_comments.: bool,
160 }
161
162 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings# {
163 fn defaultrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings][Default]default().() -> Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings][Default] {
164 Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings][Default]::allrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]all().()
165 }
166 }
167
168 impl CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings# {
169 /// Enable all comment Styles
170 pub const fn allrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]all().() -> Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings] {
171 Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings] {
172 block_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#block_comments.: true,
173 slash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#slash_line_comments.: true,
174 hash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#hash_line_comments.: true,
175 }
176 }
177 /// Only allow line comments starting with `#`
178 pub const fn hash_onlyrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]hash_only().() -> Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings] {
179 Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings] {
180 hash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#hash_line_comments.: true,
181 block_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#block_comments.: false,
182 slash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#slash_line_comments.: false,
183 }
184 }
185 /// Only allow "c-style" comments.
186 ///
187 /// Specifically, line comments beginning with `//` and
188 /// block comment like `/* ... */`.
189 pub const fn c_stylerust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]c_style().() -> Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings] {
190 Selfrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings] {
191 block_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#block_comments.: true,
192 slash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#slash_line_comments.: true,
193 hash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#hash_line_comments.: false,
194 }
195 }
196
197 /// Create a new `StripComments` for `input`, using these settings.
198 ///
199 /// Transform `input` into a [`Read`] that strips out comments.
200 /// The types of comments to support are determined by the configuration of
201 /// `self`.
202 ///
203 /// ## Examples
204 ///
205 /// ```
206 /// use json_comments::CommentSettings;
207 /// use std::io::Read;
208 ///
209 /// let input = r#"{
210 /// // c line comment
211 /// "a": "b"
212 /// /** multi line
213 /// comment
214 /// */ }"#;
215 ///
216 /// let mut stripped = String::new();
217 /// CommentSettings::c_style().strip_comments(input.as_bytes()).read_to_string(&mut stripped).unwrap();
218 ///
219 /// assert_eq!(stripped, "{
220 /// \n\"a\": \"b\"
221 /// }");
222 /// ```
223 ///
224 /// ```
225 /// use json_comments::CommentSettings;
226 /// use std::io::Read;
227 ///
228 /// let input = r#"{
229 /// ## shell line comment
230 /// "a": "b"
231 /// }"#;
232 ///
233 /// let mut stripped = String::new();
234 /// CommentSettings::hash_only().strip_comments(input.as_bytes()).read_to_string(&mut stripped).unwrap();
235 ///
236 /// assert_eq!(stripped, "{
237 /// \n\"a\": \"b\"\n}");
238 /// ```
239 #[inline]
240 pub fn strip_commentsrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]strip_comments().<I: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#>(self, input: I) -> StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#<I> {
241 StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#::with_settingsrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`]with_settings().(self, input)
242 }
243 }
244
245 fn toprust-analyzer cargo json_comments 0.2.2 top().(c: &mut u8, settings: &CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#) -> Staterust-analyzer cargo json_comments 0.2.2 State# {
246 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c {
247 b'"' => InStringrust-analyzer cargo json_comments 0.2.2 State#InString#,
248 b'/' => {
249 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c = b' ';
250 InCommentrust-analyzer cargo json_comments 0.2.2 State#InComment#
251 }
252 b'#' if settings.hash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#hash_line_comments. => {
253 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c = b' ';
254 InLineCommentrust-analyzer cargo json_comments 0.2.2 State#InLineComment#
255 }
256 _ => Toprust-analyzer cargo json_comments 0.2.2 State#Top#,
257 }
258 }
259
260 fn in_stringrust-analyzer cargo json_comments 0.2.2 in_string().(c: u8) -> Staterust-analyzer cargo json_comments 0.2.2 State# {
261 match c {
262 b'"' => Toprust-analyzer cargo json_comments 0.2.2 State#Top#,
263 b'\\' => StringEscaperust-analyzer cargo json_comments 0.2.2 State#StringEscape#,
264 _ => InStringrust-analyzer cargo json_comments 0.2.2 State#InString#,
265 }
266 }
267
268 fn in_commentrust-analyzer cargo json_comments 0.2.2 in_comment().(c: &mut u8, settings: &CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Staterust-analyzer cargo json_comments 0.2.2 State#> {
269 let new_state = match c {
270 b'*' if settings.block_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#block_comments. => InBlockCommentrust-analyzer cargo json_comments 0.2.2 State#InBlockComment#,
271 b'/' if settings.slash_line_commentsrust-analyzer cargo json_comments 0.2.2 CommentSettings#slash_line_comments. => InLineCommentrust-analyzer cargo json_comments 0.2.2 State#InLineComment#,
272 _ => invalid_datarust-analyzer cargo json_comments 0.2.2 invalid_data!!(),
273 };
274 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c = b' ';
275 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(new_state)
276 }
277
278 fn in_block_commentrust-analyzer cargo json_comments 0.2.2 in_block_comment().(c: &mut u8) -> Staterust-analyzer cargo json_comments 0.2.2 State# {
279 let old = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c;
280 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c = b' ';
281 if old rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'*' {
282 MaybeCommentEndrust-analyzer cargo json_comments 0.2.2 State#MaybeCommentEnd#
283 } else {
284 InBlockCommentrust-analyzer cargo json_comments 0.2.2 State#InBlockComment#
285 }
286 }
287
288 fn maybe_comment_endrust-analyzer cargo json_comments 0.2.2 maybe_comment_end().(c: &mut u8) -> Staterust-analyzer cargo json_comments 0.2.2 State# {
289 let old = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c;
290 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c = b' ';
291 match old {
292 b'/' => Toprust-analyzer cargo json_comments 0.2.2 State#Top#,
293 b'*' => MaybeCommentEndrust-analyzer cargo json_comments 0.2.2 State#MaybeCommentEnd#,
294 _ => InBlockCommentrust-analyzer cargo json_comments 0.2.2 State#InBlockComment#,
295 }
296 }
297
298 fn in_line_commentrust-analyzer cargo json_comments 0.2.2 in_line_comment().(c: &mut u8) -> Staterust-analyzer cargo json_comments 0.2.2 State# {
299 if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'\n' {
300 Toprust-analyzer cargo json_comments 0.2.2 State#Top#
301 } else {
302 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().c = b' ';
303 InLineCommentrust-analyzer cargo json_comments 0.2.2 State#InLineComment#
304 }
305 }
306
307 #[cfg(test)]
308 mod testsrust-analyzer cargo json_comments 0.2.2 tests/ {
309 use superrust-analyzer cargo json_comments 0.2.2 crate/::*;
310 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#};
311
312 fn strip_stringrust-analyzer cargo json_comments 0.2.2 tests/strip_string().(input: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
313 let mut out = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
314 let count = StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#::newrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`]new().(input.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
315 .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut out)
316 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
317 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(count, input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().());
318 out
319 }
320
321 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
322 fn block_commentsrust-analyzer cargo json_comments 0.2.2 tests/block_comments().() {
323 let json = r#"{/* Comment */"hi": /** abc */ "bye"}"#;
324 let stripped = strip_stringrust-analyzer cargo json_comments 0.2.2 tests/strip_string().(json);
325 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stripped, r#"{ "hi": "bye"}"#);
326 }
327
328 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
329 fn block_comments_with_possible_endrust-analyzer cargo json_comments 0.2.2 tests/block_comments_with_possible_end().() {
330 let json = r#"{/* Comment*PossibleEnd */"hi": /** abc */ "bye"}"#;
331 let stripped = strip_stringrust-analyzer cargo json_comments 0.2.2 tests/strip_string().(json);
332 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
333 stripped,
334 r#"{ "hi": "bye"}"#
335 );
336 }
337
338 // See https://github.com/tmccombs/json-comments-rs/issues/12
339 // Make sure we can parse a block comment that ends with more than one "*"
340 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
341 fn doc_commentrust-analyzer cargo json_comments 0.2.2 tests/doc_comment().() {
342 let json = r##"/** C **/ { "foo": 123 }"##;
343 let stripped = strip_stringrust-analyzer cargo json_comments 0.2.2 tests/strip_string().(json);
344 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stripped, r##" { "foo": 123 }"##);
345 }
346
347 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
348 fn line_commentsrust-analyzer cargo json_comments 0.2.2 tests/line_comments().() {
349 let json = r#"{
350 // line comment
351 "a": 4,
352 # another
353 }"#;
354
355 let expected = "{
356 \n \"a\": 4,
357 \n }";
358
359 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(strip_stringrust-analyzer cargo json_comments 0.2.2 tests/strip_string().(json), expected);
360 }
361
362 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
363 fn incomplete_stringrust-analyzer cargo json_comments 0.2.2 tests/incomplete_string().() {
364 let json = r#""foo"#;
365 let mut stripped = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
366
367 let err = StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#::newrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`]new().(json.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
368 .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut stripped)
369 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
370 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#);
371 }
372
373 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
374 fn incomplete_commentrust-analyzer cargo json_comments 0.2.2 tests/incomplete_comment().() {
375 let json = r#"/* foo "#;
376 let mut stripped = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
377
378 let err = StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#::newrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`]new().(json.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
379 .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut stripped)
380 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
381 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#);
382 }
383
384 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
385 fn incomplete_comment2rust-analyzer cargo json_comments 0.2.2 tests/incomplete_comment2().() {
386 let json = r#"/* foo *"#;
387 let mut stripped = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
388
389 let err = StripCommentsrust-analyzer cargo json_comments 0.2.2 StripComments#::newrust-analyzer cargo json_comments 0.2.2 impl#[`StripComments<T>`]new().(json.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
390 .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut stripped)
391 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
392 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#);
393 }
394
395 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
396 fn no_hash_commentsrust-analyzer cargo json_comments 0.2.2 tests/no_hash_comments().() {
397 let json = r#"# bad comment
398 {"a": "b"}"#;
399 let mut stripped = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
400 CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#::c_stylerust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]c_style().()
401 .strip_commentsrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]strip_comments().(json.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
402 .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut stripped)
403 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
404 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stripped, json);
405 }
406
407 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
408 fn no_slash_line_commentsrust-analyzer cargo json_comments 0.2.2 tests/no_slash_line_comments().() {
409 let json = r#"// bad comment
410 {"a": "b"}"#;
411 let mut stripped = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
412 let err = CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#::hash_onlyrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]hash_only().()
413 .strip_commentsrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]strip_comments().(json.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
414 .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut stripped)
415 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
416 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#);
417 }
418
419 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
420 fn no_block_commentsrust-analyzer cargo json_comments 0.2.2 tests/no_block_comments().() {
421 let json = r#"/* bad comment */ {"a": "b"}"#;
422 let mut stripped = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
423 let err = CommentSettingsrust-analyzer cargo json_comments 0.2.2 CommentSettings#::hash_onlyrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]hash_only().()
424 .strip_commentsrust-analyzer cargo json_comments 0.2.2 impl#[CommentSettings]strip_comments().(json.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())
425 .read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_string().(&mut stripped)
426 .unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().();
427 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#);
428 }
429 }