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