Skip to content

Commit

Permalink
Raise error if --- is not at the start of document
Browse files Browse the repository at this point in the history
Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Dec 30, 2024
1 parent 88bda85 commit 7ded04e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) use self::token_iter::TokensIter;
pub(crate) use self::variant::{get_tag, YamlValueEnumAccess};

pub use self::deserializer::{from_str, to_value, RmsdDeserializer};
pub use self::error::RmsdError;
pub use self::error::{ErrorKind, RmsdError};
pub use self::map::YamlValueMap;
pub use self::position::RmsdPosition;
pub use self::value::{YamlTag, YamlValue, YamlValueData};
34 changes: 34 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ impl YamlToken {
// We might be got `---` as document begin which we should
// ignore
if iter.as_str().starts_with("---") {
if !ret.is_empty() {
return Err(RmsdError::unexpected_yaml_node_type(
format!(
"The `---` should be placed at the \
beginning of document, but we have {} \
before it",
ret.pop().unwrap()
),
iter.pos(),
));
}
iter.next();
iter.next();
iter.next();
Expand Down Expand Up @@ -364,6 +375,7 @@ fn read_unquoted_str_token(
#[cfg(test)]
mod tests {
use super::*;
use crate::ErrorKind;
use pretty_assertions::assert_eq;

#[test]
Expand Down Expand Up @@ -559,4 +571,26 @@ mod tests {
]
)
}

#[test]
fn test_doc_beging() {
assert_eq!(
YamlToken::parse("\n ---\n128").unwrap(),
vec![YamlToken {
indent: 0,
start: RmsdPosition::new(3, 1),
end: RmsdPosition::new(3, 3),
data: YamlTokenData::Scalar("128".into()),
},]
)
}

#[test]
fn test_doc_invalid_beging() {
let result = YamlToken::parse("128\n ---\n128");
assert!(result.is_err());
if let Err(e) = result {
assert_eq!(e.kind(), ErrorKind::UnexpectedYamlNodeType);
}
}
}

0 comments on commit 7ded04e

Please sign in to comment.