#[non_exhaustive]pub enum RequireIndent {
Unchecked,
Divisible(usize),
Even,
Uniform(Option<usize>),
}Expand description
Requirements for indentation validation during YAML deserialization.
When set via options!, the deserializer validates every
parser-reported indentation level against the chosen policy.
§Variants
| Variant | Accepts |
|---|---|
Unchecked | Any indentation (default) |
Divisible(n) | Indentation divisible by n |
Even | Even indentation (0, 2, 4, …) |
Uniform | Consistent indentation throughout the document |
Important: if there are multiple documents in the YAML input, or added with !include, indentation requirements apply to all of them. For Uniform, indentation is required to be consistent across all included documents as well.
§Examples
§Enforcing even indentation
use serde_json::Value;
// This YAML uses 2-space indentation — accepted by `Even`.
let yaml = r#"
server:
host: localhost
port: 8080
"#;
let options = serde_saphyr::options! {
require_indent: serde_saphyr::RequireIndent::Even,
};
let result = serde_saphyr::from_str_with_options::<Value>(yaml, options);
assert!(result.is_ok());use serde_json::Value;
// This YAML uses 3-space indentation — rejected by `Even`.
let yaml = r#"
server:
host: localhost
"#;
let options = serde_saphyr::options! {
require_indent: serde_saphyr::RequireIndent::Even,
};
let result = serde_saphyr::from_str_with_options::<Value>(yaml, options);
assert!(result.is_err());§Enforcing indentation divisible by 4
use serde_json::Value;
// 4-space indentation — accepted by `Divisible(4)`.
let yaml = r#"
database:
host: db.local
port: 5432
"#;
let options = serde_saphyr::options! {
require_indent: serde_saphyr::RequireIndent::Divisible(4),
};
assert!(serde_saphyr::from_str_with_options::<Value>(yaml, options).is_ok());§Enforcing uniform indentation
use serde_json::Value;
// Consistent 2-space indentation throughout.
let yaml = r#"
app:
name: demo
version: 1
"#;
let options = serde_saphyr::options! {
require_indent: serde_saphyr::RequireIndent::Uniform(None),
};
assert!(serde_saphyr::from_str_with_options::<Value>(yaml, options).is_ok());§YAML sample that triggers an indentation error
# With RequireIndent::Even, this document would fail:
config:
debug: true # 3-space indent — odd, rejectedVariants (Non-exhaustive)§
This enum is marked as non-exhaustive
Unchecked
No indentation checking is performed.
Divisible(usize)
Indentation must be divisible by n.
Even
Indentation must be even.
Uniform(Option<usize>)
Indentation must be uniform throughout the document.
Implementations§
Source§impl RequireIndent
impl RequireIndent
Sourcepub fn is_valid(&mut self, n: usize) -> Result<(), Error>
pub fn is_valid(&mut self, n: usize) -> Result<(), Error>
Checks whether the given indentation n is valid with respect to this requirement.
For Uniform, the first non-zero indentation encountered is
remembered, and subsequent calls compare against that stored value.
Returns Ok(()) if valid, or an Error::IndentationError
with Location::UNKNOWN. Deserialization entry points attach the
source location before returning this error.
Trait Implementations§
Source§impl Clone for RequireIndent
impl Clone for RequireIndent
Source§fn clone(&self) -> RequireIndent
fn clone(&self) -> RequireIndent
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more