use crate::error::ParseResult;
use crate::value::Value;
use saphyr::LoadableYamlNode;
#[derive(Debug)]
pub struct Parser;
impl Parser {
pub fn parse_str(input: &str) -> ParseResult<Option<Value>> {
let docs = Value::load_from_str(input)?;
Ok(docs.into_iter().next())
}
pub fn parse_all(input: &str) -> ParseResult<Vec<Value>> {
Ok(Value::load_from_str(input)?)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_str_simple() {
let result = Parser::parse_str("name: test\nvalue: 123").unwrap();
assert!(result.is_some());
}
#[test]
fn test_parse_str_empty() {
let result = Parser::parse_str("").unwrap();
assert!(result.is_none());
}
#[test]
fn test_parse_all_multiple_docs() {
let docs = Parser::parse_all("---\nfoo: 1\n---\nbar: 2").unwrap();
assert_eq!(docs.len(), 2);
}
#[test]
fn test_parse_str_invalid() {
let result = Parser::parse_str("invalid: [\n missing: bracket");
assert!(result.is_err());
}
#[test]
fn test_parse_nested() {
let yaml = r"
person:
name: John
age: 30
hobbies:
- reading
- coding
";
let result = Parser::parse_str(yaml).unwrap();
assert!(result.is_some());
}
#[test]
fn test_parse_anchors() {
let yaml = r"
defaults: &defaults
adapter: postgres
host: localhost
development:
<<: *defaults
database: dev_db
";
let result = Parser::parse_str(yaml).unwrap();
assert!(result.is_some());
}
}