1mod diagnostics;
2mod line_helpers;
3mod parse;
4mod relation_helpers;
5mod validate;
6
7#[cfg(test)]
8mod tests;
9
10use std::path::Path;
11
12pub use diagnostics::SchemaError;
13
14#[cfg(test)]
15use relation_helpers::parse_relation_attribute;
16
17pub fn parse_schema(source: &str) -> Result<cratestack_core::Schema, SchemaError> {
18 parse_schema_named("<schema>", source)
19}
20
21pub fn parse_schema_named(
22 path: &str,
23 source: &str,
24) -> Result<cratestack_core::Schema, SchemaError> {
25 let schema = parse::parse_schema_only(source)?;
26 validate::validate_schema(path, source, &schema)?;
27 Ok(schema)
28}
29
30pub fn parse_schema_file(path: impl AsRef<Path>) -> Result<cratestack_core::Schema, SchemaError> {
31 let path = path.as_ref();
32 let source = std::fs::read_to_string(path).map_err(|error| {
33 SchemaError::new(
34 format!("failed to read schema file {}: {error}", path.display()),
35 0..0,
36 1,
37 )
38 })?;
39 parse_schema_named(&path.display().to_string(), &source)
40}