ndjson_stream/
config.rs

1//! This module defines the configuration options which a NDJSON-parser can be provided. The entry
2//! point is the [NdjsonConfig] struct. Child data types are also defined in this module.
3
4/// Controls how the parser deals with lines that contain no JSON values.
5#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
6pub enum EmptyLineHandling {
7
8    /// Parse every line, i.e. every segment between `\n` characters, even if it is empty. This will
9    /// result in errors for empty lines.
10    #[default]
11    ParseAlways,
12
13    /// Ignore lines, i.e. segments between `\n` characters, which are empty, i.e. contain no
14    /// characters. For compatibility with `\r\n`-style linebreaks, this also ignores lines which
15    /// consist of only a single `\r` character.
16    IgnoreEmpty,
17
18    /// Ignore lines, i.e. segments between `\n` characters, which contain only whitespace
19    /// characters.
20    IgnoreBlank
21}
22
23/// Configuration for the NDJSON-parser which controls the behavior in various situations.
24///
25/// By default, the parser will attempt to parse every line, i.e. every segment between `\n`
26/// characters, even if it is empty. This will result in errors for empty lines.
27///
28/// You can construct a config by first calling [NdjsonConfig::default] and then using the
29/// builder-style associated functions to configure it. See the example below.
30///
31/// ```
32/// use ndjson_stream::config::{EmptyLineHandling, NdjsonConfig};
33///
34/// let config = NdjsonConfig::default()
35///     .with_empty_line_handling(EmptyLineHandling::IgnoreBlank)
36///     .with_parse_rest(true);
37/// ```
38#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
39pub struct NdjsonConfig {
40    pub(crate) empty_line_handling: EmptyLineHandling,
41    pub(crate) parse_rest: bool
42}
43
44impl NdjsonConfig {
45
46    /// Creates a new config from this config which has a different handling for lines that contain
47    /// no JSON values. See [EmptyLineHandling] for more details.
48    ///
49    /// # Returns
50    ///
51    /// A new config with all the same values as this one, except the empty-line-handling.
52    pub fn with_empty_line_handling(self, empty_line_handling: EmptyLineHandling) -> NdjsonConfig {
53        NdjsonConfig {
54            empty_line_handling,
55            ..self
56        }
57    }
58
59    /// Creates a new config from this config which has the given configuration on whether to parse
60    /// or ignore the rest, i.e. the part after the last newline character. If `parse_rest` is set
61    /// to `false`, the rest will always be ignored, while `true` causes it to only be ignored if it
62    /// is empty or considered empty by the handling configured in
63    /// [NdjsonConfig::with_empty_line_handling], which by default is only truly empty. Otherwise,
64    /// the rest is parsed like an ordinary JSON record. By default, this is set to `false`.
65    ///
66    /// # Returns
67    ///
68    /// A new config with all the same values as this one, except the parse-rest-flag.
69    pub fn with_parse_rest(self, parse_rest: bool) -> NdjsonConfig {
70        NdjsonConfig {
71            parse_rest,
72            ..self
73        }
74    }
75}