soml-lang 0.0.2

SOML, a config format for humans: a strict reader, serde support, and a lossless syntax tree
Documentation
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*!
A deserializer that reads a document in one pass, without building a tree, for the common case: a valid document that fits its type. It checks every rule with the same parser steps that build the tree, but it does not explain an error. After any error, `from_str` reads the document again through the tree, which gives the same error, with the same message and position, as a check of the whole document does.
*/

use super::{KeyDeserializer, NodeDeserializer};
use crate::Error;
use crate::parse::{Collection, ObjectKeys, Parser};
use serde_core::de::{self, DeserializeSeed, Deserializer, MapAccess, SeqAccess, Visitor};
use std::borrow::Cow;

/**
Reads a document into `T`, or returns `None` when that fails in any way, so that the caller reads it again through the tree.
*/
pub(crate) fn read<'de, T: de::Deserialize<'de>>(source: &'de str) -> Option<T> {
	// `from_str` checks byte order marks and raw controls before either reading path starts.
	let mut stream = Stream {
		parser: Parser::new(source),
		source,
		keys: Vec::with_capacity(32),
		depth: 0,
		failed: false,
	};

	stream.parser.start_document().ok()?;
	let value = T::deserialize(&mut stream).ok()?;
	stream.parser.end_document().ok()?;
	(!stream.failed).then_some(value)
}

/**
The error of every failed step. Nobody sees it, because the document is then read again through the tree.
*/
#[cold]
fn failure<T>(_: T) -> Error {
	de::Error::custom("the document is read again through the tree")
}

struct Stream<'de> {
	parser: Parser<'de>,
	source: &'de str,
	/**
	The keys of the objects that are being read, the innermost last, to find a duplicate.
	*/
	keys: Vec<Cow<'de, str>>,
	/**
	How many objects and arrays are open.
	*/
	depth: usize,
	/**
	Whether an error left any step. A type can swallow an error, as a `deserialize_with` that falls back to a default does, and that leaves the cursor inside a value.
	*/
	failed: bool,
}

/**
What starts at the cursor.
*/
enum Next {
	Scalar,
	Array,
	Object,
	/**
	The document's own object, which has no braces.
	*/
	BareObject,
}

impl<'de> Stream<'de> {
	#[inline]
	fn next(&self) -> Next {
		match self.parser.peek() {
			Some(b'[') => Next::Array,
			Some(b'{') => Next::Object,
			// The document is an object or an array, so it is an object without braces when it does not start with a bracket.
			_ if self.depth == 0 => Next::BareObject,
			_ => Next::Scalar,
		}
	}

	/**
	Runs a step, and remembers when it fails.
	*/
	fn watch<T>(&mut self, step: impl FnOnce(&mut Self) -> Result<T, Error>) -> Result<T, Error> {
		let result = step(self);
		self.failed |= result.is_err();
		result
	}

	/**
	Reads the value at the cursor into a tree, for the tree's deserializer. Every scalar is read this way, and so is a collection for a type that this deserializer does not read in one pass, such as `Spanned`, or that does not take a collection.
	*/
	#[inline]
	fn node(&mut self) -> Result<NodeDeserializer<'de>, Error> {
		let node = match self.next() {
			Next::BareObject => self.parser.parse_bare_object(),
			_ => self.parser.parse_value(self.depth + 1),
		}
		.map_err(failure)?;

		Ok(NodeDeserializer {
			node,
			source: Some(self.source),
		})
	}

	fn visit_array<V: Visitor<'de>>(&mut self, visitor: V) -> Result<V::Value, Error> {
		self.depth += 1;
		let start = self.parser.open_braced(self.depth).map_err(failure)?;

		let mut items = Items {
			stream: self,
			start,
			is_done: false,
		};

		// An error returns before `depth` is restored. That does no harm, because `watch` sets `failed`, and the document is then read again through the tree.
		let value = visitor.visit_seq(&mut items)?;

		// A visitor that stops early, such as a tuple's, leaves items, which is an error.
		if !items.is_done && items.has_item()? {
			return Err(failure(()));
		}

		self.depth -= 1;
		Ok(value)
	}

	fn visit_object<V: Visitor<'de>>(
		&mut self,
		visitor: V,
		is_bare: bool,
	) -> Result<V::Value, Error> {
		self.depth += 1;

		let start = if is_bare {
			None
		} else {
			Some(self.parser.open_braced(self.depth).map_err(failure)?)
		};

		let mut members = Members {
			keys: ObjectKeys::new(&self.keys),
			stream: self,
			start,
			is_first: is_bare,
			is_done: false,
			has_value: false,
		};

		// As in `visit_array`, an error returns before `keys` and `depth` are restored, which does no harm.
		let value = visitor.visit_map(&mut members)?;

		// A visitor that stops early leaves members, which is an error.
		if members.has_value || (!members.is_done && members.has_entry()?) {
			return Err(failure(()));
		}

		members.keys.end(&mut members.stream.keys);
		self.depth -= 1;
		Ok(value)
	}

	/**
	Reads the value at the cursor with `seed`. A type that returns without reading its value leaves the cursor where it was, and then whatever is there would go unchecked, so that is an error.
	*/
	fn read_value<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<T::Value, Error> {
		let start = self.parser.index;
		let value = seed.deserialize(&mut *self)?;

		if self.parser.index == start {
			return Err(failure(()));
		}

		Ok(value)
	}

	/**
	Whether the first key of the object at the cursor is `key`.
	*/
	fn first_key_is(&self, key: &str, is_bare: bool) -> bool {
		let mut parser = self.parser.clone();

		// This only looks ahead on a copy, so it skips the depth check. The read that follows checks the depth.
		if !is_bare && parser.open_braced(0).is_err() {
			return false;
		}

		parser
			.parse_entry_key()
			.is_ok_and(|(first, _)| first == key)
	}
}

/**
A type that takes no collection, so a collection is left to the tree's deserializer, which gives the type error. Ignored collections are checked through the stream without building their tree.
*/
macro_rules! deserialize_from_tree {
	($($method:ident)*) => {
		$(
			fn $method<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
				self.watch(|this| this.node()?.$method(visitor))
			}
		)*
	};
}

impl<'de> Deserializer<'de> for &mut Stream<'de> {
	type Error = Error;

	fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
		self.watch(|this| match this.next() {
			Next::Array => this.visit_array(visitor),
			Next::Object => this.visit_object(visitor, false),
			Next::BareObject => this.visit_object(visitor, true),
			Next::Scalar => this.node()?.deserialize_any(visitor),
		})
	}

	fn deserialize_bytes<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
		self.watch(|this| match this.next() {
			Next::Array => this.visit_array(visitor),
			_ => this.node()?.deserialize_bytes(visitor),
		})
	}

	fn deserialize_byte_buf<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
		self.deserialize_bytes(visitor)
	}

	fn deserialize_option<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
		self.watch(|this| match this.next() {
			Next::Scalar => this.node()?.deserialize_option(visitor),
			_ => visitor.visit_some(this),
		})
	}

	fn deserialize_unit_struct<V: Visitor<'de>>(
		self,
		_name: &'static str,
		visitor: V,
	) -> Result<V::Value, Error> {
		self.deserialize_unit(visitor)
	}

	fn deserialize_newtype_struct<V: Visitor<'de>>(
		self,
		name: &'static str,
		visitor: V,
	) -> Result<V::Value, Error> {
		self.watch(|this| match (name, this.next()) {
			// A `Value` differs from any other type only in its scalars, an instant and a duration.
			(crate::value::TOKEN, Next::Array | Next::Object | Next::BareObject) => {
				this.deserialize_any(visitor)
			}
			(
				crate::value::TOKEN
				| crate::spanned::TOKEN
				| crate::instant::TOKEN
				| crate::duration::TOKEN,
				_,
			) => this.node()?.deserialize_newtype_struct(name, visitor),
			_ => visitor.visit_newtype_struct(this),
		})
	}

	fn deserialize_seq<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
		self.watch(|this| match this.next() {
			Next::Array => this.visit_array(visitor),
			_ => this.node()?.deserialize_seq(visitor),
		})
	}

	fn deserialize_tuple<V: Visitor<'de>>(
		self,
		_length: usize,
		visitor: V,
	) -> Result<V::Value, Error> {
		self.deserialize_seq(visitor)
	}

	fn deserialize_tuple_struct<V: Visitor<'de>>(
		self,
		_name: &'static str,
		_length: usize,
		visitor: V,
	) -> Result<V::Value, Error> {
		self.deserialize_seq(visitor)
	}

	fn deserialize_map<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
		self.watch(|this| match this.next() {
			Next::Object => this.visit_object(visitor, false),
			Next::BareObject => this.visit_object(visitor, true),
			_ => this.node()?.deserialize_map(visitor),
		})
	}

	fn deserialize_struct<V: Visitor<'de>>(
		self,
		name: &'static str,
		fields: &'static [&'static str],
		visitor: V,
	) -> Result<V::Value, Error> {
		self.watch(|this| {
			let is_bare = match this.next() {
				Next::Object => false,
				Next::BareObject => true,
				_ => return this.node()?.deserialize_struct(name, fields, visitor),
			};

			// An adjacently tagged enum whose tag is not first may buffer its content, losing native types and spans. The tree orders the tag before the content, including when unknown fields precede them.
			// A plain struct with two fields, whose first field is not first, also takes this path. That is slower, but it gives the same value.
			if let [tag, _] = fields
				&& !this.first_key_is(tag, is_bare)
			{
				return this.node()?.deserialize_struct(name, fields, visitor);
			}

			this.visit_object(visitor, is_bare)
		})
	}

	fn deserialize_enum<V: Visitor<'de>>(
		self,
		name: &'static str,
		variants: &'static [&'static str],
		visitor: V,
	) -> Result<V::Value, Error> {
		self.watch(|this| this.node()?.deserialize_enum(name, variants, visitor))
	}

	fn deserialize_ignored_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value, Error> {
		// Validate ignored collections through the stream instead of allocating a discarded tree.
		self.watch(|this| {
			match this.next() {
				Next::Array => {
					this.visit_array(de::IgnoredAny)?;
				}
				Next::Object => {
					this.visit_object(de::IgnoredAny, false)?;
				}
				Next::BareObject => {
					this.visit_object(de::IgnoredAny, true)?;
				}
				Next::Scalar => {
					this.node()?.deserialize_ignored_any(de::IgnoredAny)?;
				}
			}
			visitor.visit_unit()
		})
	}

	deserialize_from_tree! {
		deserialize_bool deserialize_i8 deserialize_i16 deserialize_i32 deserialize_i64 deserialize_i128
		deserialize_u8 deserialize_u16 deserialize_u32 deserialize_u64 deserialize_u128
		deserialize_f32 deserialize_f64 deserialize_char deserialize_str deserialize_string
		deserialize_unit deserialize_identifier
	}
}

struct Items<'a, 'de> {
	stream: &'a mut Stream<'de>,
	/**
	The offset of the opening bracket.
	*/
	start: usize,
	/**
	Whether the closing bracket has been read.
	*/
	is_done: bool,
}

impl<'de> Items<'_, 'de> {
	#[inline]
	fn has_item(&mut self) -> Result<bool, Error> {
		self.stream
			.parser
			.next_braced_item(Collection::Array, self.start)
			.map_err(failure)
	}

	fn next_item<T: DeserializeSeed<'de>>(&mut self, seed: T) -> Result<Option<T::Value>, Error> {
		// A visitor may ask again after the end, which must not read past the closing bracket.
		if self.is_done || !self.has_item()? {
			self.is_done = true;
			return Ok(None);
		}

		let value = self.stream.read_value(seed)?;

		self.stream
			.parser
			.after_braced_item(Collection::Array, self.start)
			.map_err(failure)?;

		Ok(Some(value))
	}
}

impl<'de> SeqAccess<'de> for Items<'_, 'de> {
	type Error = Error;

	fn next_element_seed<T: DeserializeSeed<'de>>(
		&mut self,
		seed: T,
	) -> Result<Option<T::Value>, Error> {
		// After an error that a type caught, the cursor stays where it failed, so a type that keeps asking would never get to the end. The document is read again through the tree anyway.
		if self.stream.failed {
			return Ok(None);
		}

		let result = self.next_item(seed);
		self.stream.failed |= result.is_err();
		result
	}
}

struct Members<'a, 'de> {
	stream: &'a mut Stream<'de>,
	/**
	The offset of the opening brace, or `None` for the document's own object, which has no braces.
	*/
	start: Option<usize>,
	keys: ObjectKeys<'de>,
	/**
	Whether the first entry of an object without braces is next, which has no separator before it.
	*/
	is_first: bool,
	/**
	Whether the end of the object has been read.
	*/
	is_done: bool,
	/**
	Whether a key has been read, and its value is next.
	*/
	has_value: bool,
}

impl<'de> Members<'_, 'de> {
	#[inline]
	fn has_entry(&mut self) -> Result<bool, Error> {
		let parser = &mut self.stream.parser;

		match self.start {
			Some(start) => parser.next_braced_item(Collection::Object, start),
			None if std::mem::take(&mut self.is_first) => Ok(true),
			None => parser.next_bare_entry(),
		}
		.map_err(failure)
	}

	/**
	Remembers a key of this object, and fails when it has the key already.
	*/
	#[inline]
	fn insert(&mut self, key: Cow<'de, str>) -> Result<(), Error> {
		// The stack of keys ends with the keys of this object, because an inner object removes its keys when it ends, and no key is read after a failure.
		if self.keys.insert(&mut self.stream.keys, key) {
			Ok(())
		} else {
			Err(failure(()))
		}
	}

	fn next_key<K: DeserializeSeed<'de>>(&mut self, seed: K) -> Result<Option<K::Value>, Error> {
		// The cursor is at the value of the key before, which must not be read as a key.
		if self.has_value {
			return Err(failure(()));
		}

		// A visitor may ask again after the end, which must not read past the closing brace.
		if self.is_done || !self.has_entry()? {
			self.is_done = true;
			return Ok(None);
		}

		let (key, _) = self.stream.parser.parse_entry_key().map_err(failure)?;
		self.insert(key.clone())?;
		self.has_value = true;
		seed.deserialize(KeyDeserializer { key }).map(Some)
	}

	fn next_value<V: DeserializeSeed<'de>>(&mut self, seed: V) -> Result<V::Value, Error> {
		if !std::mem::take(&mut self.has_value) {
			return Err(failure(()));
		}

		let value = self.stream.read_value(seed)?;

		// In the document's own object, `has_entry` reads the separator before the next key instead.
		if let Some(start) = self.start {
			self.stream
				.parser
				.after_braced_item(Collection::Object, start)
				.map_err(failure)?;
		}

		Ok(value)
	}
}

impl<'de> MapAccess<'de> for Members<'_, 'de> {
	type Error = Error;

	fn next_key_seed<K: DeserializeSeed<'de>>(
		&mut self,
		seed: K,
	) -> Result<Option<K::Value>, Error> {
		// After an error that a type caught, the cursor stays where it failed, so a type that keeps asking would never get to the end. The document is read again through the tree anyway.
		if self.stream.failed {
			return Ok(None);
		}

		let result = self.next_key(seed);
		self.stream.failed |= result.is_err();
		result
	}

	fn next_value_seed<V: DeserializeSeed<'de>>(&mut self, seed: V) -> Result<V::Value, Error> {
		let result = self.next_value(seed);
		self.stream.failed |= result.is_err();
		result
	}
}