#![doc = include_str!("../README.md")]
#![deny(rust_2018_idioms, unsafe_code)]
#![allow(clippy::derive_partial_eq_without_eq)]
pub mod builtin_connectors;
pub mod datamodel_connector;
pub mod mcf;
mod common;
mod configuration;
mod reformat;
mod set_config_dir;
mod validate;
pub use crate::{
common::{PreviewFeature, PreviewFeatures, ALL_PREVIEW_FEATURES},
configuration::{
Configuration, Datasource, DatasourceConnectorData, Generator, GeneratorConfigValue, StringFromEnvVar,
},
reformat::reformat,
};
pub use diagnostics;
pub use parser_database::{self, is_reserved_type_name};
pub use schema_ast;
pub use set_config_dir::set_config_dir;
use self::validate::{datasource_loader, generator_loader};
use diagnostics::Diagnostics;
use parser_database::{ast, ParserDatabase, SourceFile};
pub type ConnectorRegistry<'a> = &'a [&'static dyn datamodel_connector::Connector];
pub struct ValidatedSchema {
pub configuration: Configuration,
pub db: parser_database::ParserDatabase,
pub connector: &'static dyn datamodel_connector::Connector,
pub diagnostics: Diagnostics,
relation_mode: datamodel_connector::RelationMode,
}
impl std::fmt::Debug for ValidatedSchema {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("<Prisma schema>")
}
}
impl ValidatedSchema {
pub fn relation_mode(&self) -> datamodel_connector::RelationMode {
self.relation_mode
}
}
pub fn validate(file: SourceFile, connectors: ConnectorRegistry<'_>) -> ValidatedSchema {
let mut diagnostics = Diagnostics::new();
let db = ParserDatabase::new(file, &mut diagnostics);
let configuration = validate_configuration(db.ast(), &mut diagnostics, connectors);
let datasources = &configuration.datasources;
let out = validate::validate(db, datasources, configuration.preview_features(), diagnostics);
ValidatedSchema {
diagnostics: out.diagnostics,
configuration,
connector: out.connector,
db: out.db,
relation_mode: out.relation_mode,
}
}
pub fn parse_without_validation(file: SourceFile, connectors: ConnectorRegistry<'_>) -> ValidatedSchema {
let mut diagnostics = Diagnostics::new();
let db = ParserDatabase::new(file, &mut diagnostics);
let configuration = validate_configuration(db.ast(), &mut diagnostics, connectors);
let datasources = &configuration.datasources;
let out = validate::parse_without_validation(db, datasources);
ValidatedSchema {
diagnostics,
configuration,
connector: out.connector,
db: out.db,
relation_mode: out.relation_mode,
}
}
pub fn parse_configuration(
schema: &str,
connectors: ConnectorRegistry<'_>,
) -> Result<Configuration, diagnostics::Diagnostics> {
let mut diagnostics = Diagnostics::default();
let ast = schema_ast::parse_schema(schema, &mut diagnostics);
let out = validate_configuration(&ast, &mut diagnostics, connectors);
diagnostics.to_result().map(|_| out)
}
fn validate_configuration(
schema_ast: &ast::SchemaAst,
diagnostics: &mut Diagnostics,
connectors: ConnectorRegistry<'_>,
) -> Configuration {
let generators = generator_loader::load_generators_from_ast(schema_ast, diagnostics);
let datasources = datasource_loader::load_datasources_from_ast(schema_ast, diagnostics, connectors);
Configuration {
generators,
datasources,
warnings: diagnostics.warnings().to_owned(),
}
}