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