use toml_edit::DocumentMut;
use crate::types::project::PROJECT_FILE;
use crate::types::report::{Diagnostic, FormatError, Location, ParserMessage};
pub(super) fn parse_document(text: &str) -> Result<DocumentMut, Diagnostic> {
text.parse().map_err(|error: toml_edit::TomlError| {
Diagnostic::new(
Location::file(PROJECT_FILE),
FormatError::TomlSyntax(ParserMessage::new(&error.to_string())),
)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_that_is_toml_keeps_its_comments() -> Result<(), Diagnostic> {
let text = "api = 1 # pinned\n";
assert_eq!(parse_document(text)?.to_string(), text);
Ok(())
}
#[test]
fn text_that_is_not_toml_is_refused_at_the_project_file() {
let refusal = parse_document("api = \n")
.err()
.map(|found| found.to_string());
assert!(
matches!(&refusal, Some(shown) if shown.starts_with("agentstacks.toml:")),
"{refusal:?}"
);
}
}