Skip to main content

cratestack_parser/
lib.rs

1mod diagnostics;
2mod line_helpers;
3mod parse;
4mod relation_actions;
5mod relation_helpers;
6mod validate;
7
8#[cfg(test)]
9mod tests_basic;
10#[cfg(test)]
11mod tests_docs;
12#[cfg(test)]
13mod tests_enums;
14#[cfg(test)]
15mod tests_extensions;
16#[cfg(test)]
17mod tests_field_attrs;
18#[cfg(test)]
19mod tests_list_arity;
20#[cfg(test)]
21mod tests_mixins;
22#[cfg(test)]
23mod tests_model_attrs;
24#[cfg(test)]
25mod tests_model_index;
26#[cfg(test)]
27mod tests_model_unique;
28#[cfg(test)]
29mod tests_procedures;
30#[cfg(test)]
31mod tests_relation_actions;
32#[cfg(test)]
33mod tests_relations;
34#[cfg(test)]
35mod tests_relations_policy;
36#[cfg(test)]
37mod tests_reserved_keywords;
38#[cfg(test)]
39mod tests_snake_case_collisions;
40#[cfg(test)]
41mod tests_stream_attribute;
42#[cfg(test)]
43mod tests_transport;
44#[cfg(test)]
45mod tests_type_declaration_collisions;
46#[cfg(test)]
47mod tests_types;
48#[cfg(test)]
49mod tests_validators;
50#[cfg(test)]
51mod tests_vector;
52#[cfg(test)]
53mod tests_version;
54#[cfg(test)]
55mod tests_views;
56
57use std::path::Path;
58
59pub use diagnostics::SchemaError;
60
61/// Canonical scalar type names built into the `.cstack` language (e.g.
62/// `String`, `Int`, `Decimal`, ...), including `Page` (which is valid only
63/// as a procedure return type — see `validate::type_names::validate_type_ref`
64/// — not as a plain field type).
65///
66/// This is the same list `cratestack-lsp`'s autocompletion and the
67/// `cratestack-pg`/`cratestack-sqlite` emitter/decoder round-trip tests
68/// assert against, so a new builtin scalar only has to be added here once —
69/// see cratestack#232 for why that matters (this list had already silently
70/// drifted from the LSP's hand-copied one before this accessor existed).
71pub fn builtin_type_names() -> &'static [&'static str] {
72    validate::builtin_type_names()
73}
74
75#[cfg(test)]
76use relation_helpers::parse_relation_attribute;
77
78pub fn parse_schema(source: &str) -> Result<cratestack_core::Schema, SchemaError> {
79    parse_schema_named("<schema>", source)
80}
81
82pub fn parse_schema_named(
83    path: &str,
84    source: &str,
85) -> Result<cratestack_core::Schema, SchemaError> {
86    let schema = parse::parse_schema_only(source)?;
87    validate::validate_schema(path, source, &schema)?;
88    Ok(schema)
89}
90
91/// Parse a `.cstack` source into a [`cratestack_core::Schema`] WITHOUT
92/// running [`validate::validate_schema`].
93///
94/// Prefer [`parse_schema`] for any new source — this exists for two
95/// legitimate cases where the validated pipeline understates what a
96/// `Schema` value can actually be:
97///
98/// 1. A committed `migrations/*/schema.snapshot.json` can predate a
99///    validation rule added later. `cratestack-cli`'s `migrate diff`
100///    deserializes that "previous" snapshot directly and never re-runs
101///    `validate_schema` on it (only the *new* side, parsed fresh from the
102///    `.cstack` source, goes through [`parse_schema_file`]) — so an emitter
103///    can still legitimately be handed a shape the current validator would
104///    reject at the source level.
105/// 2. Tests that deliberately exercise an emitter's rendering logic for
106///    such an already-invalid shape, to prove the emitter itself still
107///    behaves sanely if that shape arrives via (1) — see
108///    `cratestack-migrate`'s `emit::postgres::tests::enums` for an example
109///    (a list-valued enum column, rejected by cratestack#229/#236 at parse
110///    time, but still real input to the Postgres emitter via a pre-#236
111///    snapshot).
112pub fn parse_schema_unvalidated(source: &str) -> Result<cratestack_core::Schema, SchemaError> {
113    parse::parse_schema_only(source)
114}
115
116pub fn parse_schema_file(path: impl AsRef<Path>) -> Result<cratestack_core::Schema, SchemaError> {
117    let path = path.as_ref();
118    let source = std::fs::read_to_string(path).map_err(|error| {
119        SchemaError::new(
120            format!("failed to read schema file {}: {error}", path.display()),
121            0..0,
122            1,
123        )
124    })?;
125    parse_schema_named(&path.display().to_string(), &source)
126}