Skip to main content

cratestack_parser/
lib.rs

1mod diagnostics;
2mod line_helpers;
3mod parse;
4mod relation_helpers;
5mod validate;
6
7#[cfg(test)]
8mod tests_basic;
9#[cfg(test)]
10mod tests_docs;
11#[cfg(test)]
12mod tests_enums;
13#[cfg(test)]
14mod tests_field_attrs;
15#[cfg(test)]
16mod tests_mixins;
17#[cfg(test)]
18mod tests_model_attrs;
19#[cfg(test)]
20mod tests_procedures;
21#[cfg(test)]
22mod tests_relations;
23#[cfg(test)]
24mod tests_relations_policy;
25#[cfg(test)]
26mod tests_transport;
27#[cfg(test)]
28mod tests_types;
29#[cfg(test)]
30mod tests_validators;
31#[cfg(test)]
32mod tests_version;
33#[cfg(test)]
34mod tests_views;
35
36use std::path::Path;
37
38pub use diagnostics::SchemaError;
39
40#[cfg(test)]
41use relation_helpers::parse_relation_attribute;
42
43pub fn parse_schema(source: &str) -> Result<cratestack_core::Schema, SchemaError> {
44    parse_schema_named("<schema>", source)
45}
46
47pub fn parse_schema_named(
48    path: &str,
49    source: &str,
50) -> Result<cratestack_core::Schema, SchemaError> {
51    let schema = parse::parse_schema_only(source)?;
52    validate::validate_schema(path, source, &schema)?;
53    Ok(schema)
54}
55
56pub fn parse_schema_file(path: impl AsRef<Path>) -> Result<cratestack_core::Schema, SchemaError> {
57    let path = path.as_ref();
58    let source = std::fs::read_to_string(path).map_err(|error| {
59        SchemaError::new(
60            format!("failed to read schema file {}: {error}", path.display()),
61            0..0,
62            1,
63        )
64    })?;
65    parse_schema_named(&path.display().to_string(), &source)
66}