ruprizzle_parser/lib.rs
1//! `schema.ruprizzle` parser.
2//!
3//! The public surface is deliberately one function, [`parse`]. Everything else —
4//! the Pest grammar, the loose AST, the five lowering passes, the validation rule
5//! table — is an implementation detail behind it. That narrowness is what makes
6//! replacing the parser a contained change rather than a schedule risk (see the
7//! fallback note in `ImplPlan02SchemaDslParser.md`).
8//!
9//! ```
10//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
11//! let schema = ruprizzle_parser::parse(
12//! "schema.ruprizzle",
13//! r#"
14//! datasource db {
15//! provider = "postgres"
16//! url = env("DATABASE_URL")
17//! }
18//!
19//! model User {
20//! id Uuid @id @default(uuid7())
21//! email String @unique
22//! }
23//! "#,
24//! )?;
25//!
26//! let user = schema.model("User").ok_or("User not found")?;
27//! assert_eq!(user.table, "users");
28//! # Ok(())
29//! # }
30//! ```
31//!
32//! Errors accumulate: one call reports every problem it can find, each with a
33//! span and a suggested fix.
34
35#![forbid(unsafe_code)]
36#![warn(missing_docs, clippy::pedantic)]
37
38pub mod ast;
39mod errors;
40mod grammar;
41mod lower;
42pub mod naming;
43mod validate;
44
45use ruprizzle_core::diagnostic::{Diagnostics, SchemaErrors};
46use ruprizzle_core::ir::Schema;
47
48pub use ast::Ast;
49
50/// Parses and validates a schema.
51///
52/// `file_name` is used only to label diagnostics; nothing is read from disk.
53///
54/// # Errors
55///
56/// Returns every problem found in one bundle — syntax errors, unresolved types,
57/// and broken validation rules alike — with the source attached so each can
58/// render its own span. Warnings (V17, and the dialect notes added in P2) never
59/// fail: they are returned to the caller through [`parse_with_warnings`].
60pub fn parse(file_name: &str, source: &str) -> Result<Schema, Box<SchemaErrors>> {
61 parse_with_warnings(file_name, source).map(|(schema, _)| schema)
62}
63
64/// Like [`parse`], but also returns the advisory diagnostics.
65///
66/// The CLI prints these on the success path; tests assert on them.
67///
68/// # Errors
69///
70/// As [`parse`].
71pub fn parse_with_warnings(
72 file_name: &str,
73 source: &str,
74) -> Result<(Schema, Vec<ruprizzle_core::SchemaError>), Box<SchemaErrors>> {
75 let mut diags = Diagnostics::new();
76
77 let ast = match grammar::parse_ast(source) {
78 Ok(ast) => ast,
79 Err(err) => {
80 // A syntax error means there is no tree to lower, so this is the one
81 // place the parser cannot keep going.
82 diags.push(errors::from_pest(&err, source));
83 diags.into_result(file_name, source)?;
84 unreachable!("a syntax error is always fatal");
85 }
86 };
87
88 let schema = lower::lower(&ast, &mut diags);
89 let warnings = diags.take_warnings();
90 diags.into_result(file_name, source)?;
91 Ok((schema, warnings))
92}
93
94/// Parses a schema without validating it, for tests and tooling that need the
95/// syntax tree rather than the IR.
96///
97/// # Errors
98///
99/// Returns the syntax error, phrased for humans.
100pub fn parse_ast(file_name: &str, source: &str) -> Result<Ast, Box<SchemaErrors>> {
101 grammar::parse_ast(source).map_err(|err| {
102 let mut diags = Diagnostics::new();
103 diags.push(errors::from_pest(&err, source));
104 // We just pushed a fatal diagnostic, so `into_result` must return `Err`.
105 match diags.into_result(file_name, source) {
106 Err(errors) => errors,
107 Ok(()) => unreachable!("a syntax error is always fatal"),
108 }
109 })
110}