Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/sqlbuild-python-base/src/boundary/main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ pub mod imported;
pub mod json_value_to_python;
pub mod keywords;
pub mod native_compiler_error;
pub mod optional_text;
pub mod register_expected_errors;
pub mod register_native_compiler_error;
pub mod register_oracles;
pub mod text_item;
pub mod utf16_units;
pub mod value_error;
pub mod within;
12 changes: 12 additions & 0 deletions crates/sqlbuild-python-base/src/boundary/main/optional_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! `value` as text when it is a `str`, else `None`.

use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult};
use pyo3::types::PyString;

/// `value` as text when it is a `str`, else `None`.
pub fn optional_text(value: &Bound<'_, PyAny>) -> PyResult<Option<String>> {
if value.is_instance_of::<PyString>() {
return Ok(Some(value.extract()?));
}
Ok(None)
}
13 changes: 13 additions & 0 deletions crates/sqlbuild-python-base/src/boundary/main/text_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! `values[key]` as text when it is present and a `str`, else `None`.

use crate::boundary::main::optional_text::optional_text;
use pyo3::prelude::{Bound, PyResult};
use pyo3::types::{PyDict, PyDictMethods};

/// `values[key]` as text when it is present and a `str`, else `None`.
pub fn text_item(values: &Bound<'_, PyDict>, key: &str) -> PyResult<Option<String>> {
match values.get_item(key)? {
Some(value) => optional_text(&value),
None => Ok(None),
}
}
7 changes: 0 additions & 7 deletions crates/sqlbuild-python-base/src/native_host/_helpers/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,3 @@ pub(crate) fn within_identity_pass<'py, T>(
let identity_pass: Bound<'py, PyAny> = entry(py, UserCodeHost::PythonIdentityPass)?.call0()?;
within(&identity_pass, |_| body())
}

/// The host exception class `id` raised with `message`, or the error looking it up or raising it.
pub(crate) fn host_error(py: Python<'_>, id: impl Into<HostId>, message: &str) -> PyErr {
entry(py, id)
.and_then(|class| class.call1((message,)))
.map_or_else(|error| error, PyErr::from_value)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! The host exception class `id` raised with `message`, or the error looking it up or raising it.

use crate::native_host::_helpers::host;
use crate::native_host::_helpers::host::entry;
use crate::records::main::construct::exception_from::exception_from;
use pyo3::PyErr;
use pyo3::prelude::Python;
use sqlbuild_host::entries::models::HostId;

/// The host exception class `id` raised with `message`, or the error looking it up or raising it.
#[inline]
pub fn host_error(py: Python<'_>, id: impl Into<HostId>, message: &str) -> PyErr {
host::host_error(py, id, message)
exception_from(entry(py, id), message, (None, None))
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,37 @@ pub(crate) fn error_class(py: Python<'_>, id: ErrorId) -> PyResult<Bound<'_, PyA

/// The native `id` exception raised with `message`, or the error looking it up or raising it.
pub(crate) fn new_plain_error(py: Python<'_>, id: ErrorId, message: &str) -> PyErr {
error_class(py, id)
.and_then(|class| class.call1((message,)))
.map_or_else(|error| error, PyErr::from_value)
new_error(py, id, message, (None, None))
}

/// A raisable native `id` error carrying `message` and the diagnostic `code`.
pub(crate) fn new_error(py: Python<'_>, id: ErrorId, message: String, code: &str) -> PyErr {
let raised = (|| {
let options: Bound<'_, PyDict> = PyDict::new(py);
options.set_item("code", code)?;
error_class(py, id)?.call((message,), Some(&options))
})();
match raised {
Ok(error) => PyErr::from_value(error),
Err(error) => error,
}
/// The native `id` exception with `message` and the given `code` and `help`, or the error raising it.
pub(crate) fn new_error(
py: Python<'_>,
id: ErrorId,
message: &str,
keywords: (Option<&str>, Option<&str>),
) -> PyErr {
exception_from(error_class(py, id), message, keywords)
}

/// The exception `class` with `message` and the given `code` and `help`, or the error raising it.
pub(crate) fn exception_from(
class: PyResult<Bound<'_, PyAny>>,
message: &str,
(code, help): (Option<&str>, Option<&str>),
) -> PyErr {
class
.and_then(|class| {
let keywords: Bound<'_, PyDict> = PyDict::new(class.py());
if let Some(code) = code {
keywords.set_item("code", code)?;
}
if let Some(help) = help {
keywords.set_item("help", help)?;
}
class.call((message,), Some(&keywords))
})
.map_or_else(|error| error, PyErr::from_value)
}

/// A raisable native `id` error carrying `message`, with each `(name, value)` attribute set on it.
Expand All @@ -81,27 +96,6 @@ pub(crate) fn new_annotated_error(
}
}

/// A raisable native `id` error carrying `message`, the diagnostic `code` and optional `help`.
pub(crate) fn new_helped_error(
py: Python<'_>,
id: ErrorId,
(message, code): (String, &str),
help: Option<&str>,
) -> PyErr {
let raised = (|| {
let options: Bound<'_, PyDict> = PyDict::new(py);
options.set_item("code", code)?;
if let Some(help) = help {
options.set_item("help", help)?;
}
error_class(py, id)?.call((message,), Some(&options))
})();
match raised {
Ok(error) => PyErr::from_value(error),
Err(error) => error,
}
}

/// The native record class or string enum `id` as registered, ignoring later attribute replacement.
pub(crate) fn record_class(py: Python<'_>, id: RecordId) -> PyResult<Bound<'_, PyAny>> {
let classes: &Vec<Py<PyAny>> = CLASSES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! The exception `class` with `message` and the given `code` and `help`.

use crate::records::_helpers::native_classes::construct;
use pyo3::prelude::{Bound, PyAny, PyErr, PyResult};

/// The exception `class` with `message` and the given `code` and `help`, or the error raising it.
#[inline]
pub fn exception_from(
class: PyResult<Bound<'_, PyAny>>,
message: &str,
keywords: (Option<&str>, Option<&str>),
) -> PyErr {
construct::exception_from(class, message, keywords)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod enum_from_value;
pub mod enum_member;
pub mod error_class;
pub mod exception_from;
pub mod new_annotated_error;
pub mod new_error;
pub mod new_helped_error;
pub mod new_plain_error;
pub mod new_record;
pub mod new_record_with;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//! A raisable native `id` error carrying `message` and the diagnostic `code`.
//! The native `id` exception raised with `message` and the `code` and `help` keywords that are given.

use crate::records::_helpers::native_classes::construct;
use crate::records::types::ErrorId;
use pyo3::prelude::PyErr;
use pyo3::prelude::Python;

/// A raisable native `id` error carrying `message` and the diagnostic `code`.
/// The native `id` exception with `message` and the given `code` and `help`, or the error raising it.
#[inline]
pub fn new_error(py: Python<'_>, id: ErrorId, message: String, code: &str) -> PyErr {
construct::new_error(py, id, message, code)
pub fn new_error(
py: Python<'_>,
id: ErrorId,
message: &str,
keywords: (Option<&str>, Option<&str>),
) -> PyErr {
construct::new_error(py, id, message, keywords)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyErr, PyResult, Python};
use pyo3::types::{PySet, PySetMethods, PyString};
use sqlbuild_host::entries::models::{AdapterHost, UserCodeHost};
use sqlbuild_python_base::records::main::construct::exception_from::exception_from;

use sqlbuild_python_base::boundary::main::call_with::call_with;
use sqlbuild_python_base::boundary::main::imported::imported;
Expand Down Expand Up @@ -33,12 +34,11 @@ impl<'py> RefKinds<'py> {

/// A `SharedInputError` with `message`.
fn shared_error(py: Python<'_>, message: String) -> PyErr {
match imported(py, SHARED_EXCEPTIONS_MODULE, "SharedInputError")
.and_then(|class| class.call1((message,)))
{
Ok(error) => PyErr::from_value(error),
Err(error) => error,
}
exception_from(
imported(py, SHARED_EXCEPTIONS_MODULE, "SharedInputError"),
&message,
(None, None),
)
}

/// The model, seed and Python source refs of a plan, as the frozenset Python builds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::compile_driver::main::setup::expanded_loader_namespace::expanded_loader_namespace;
use crate::compile_driver::main::setup::managed_write_schema::managed_write_schema;
use crate::compile_driver::models::TargetSettingsValues;
use sqlbuild_python_base::boundary::main::optional_text::optional_text;
use std::collections::{HashMap, HashSet};

use pyo3::exceptions::PyException;
Expand Down Expand Up @@ -92,27 +93,19 @@ pub(crate) fn planner_error(
}

/// `value` as an optional string: `None`, or a `str`'s text.
fn optional_text(value: &Bound<'_, PyAny>) -> PyResult<Option<String>> {
fn nullable_text(value: &Bound<'_, PyAny>) -> PyResult<Option<String>> {
if value.is_none() {
return Ok(None);
}
Ok(Some(value.extract()?))
}

/// `value` when it is a `str`, else `None`.
pub(crate) fn text_or_none(value: &Bound<'_, PyAny>) -> PyResult<Option<String>> {
match value.downcast::<PyString>() {
Ok(text) => Ok(Some(text.to_cow()?.into_owned())),
Err(_) => Ok(None),
}
}

/// The connection's `part` when the compile leaves it unused because the project names connections or targets.
fn ignored_connection_value(project: &Bound<'_, PyAny>, part: &str) -> PyResult<Option<String>> {
if text_or_none(&native_field(project, &format!("effective_target_{part}"))?)?.is_some() {
if optional_text(&native_field(project, &format!("effective_target_{part}"))?)?.is_some() {
return Ok(None);
}
text_or_none(&mapping_get(
optional_text(&mapping_get(
&native_field(project, "effective_connection")?,
part,
)?)
Expand Down Expand Up @@ -186,7 +179,7 @@ pub(crate) fn named_target_schema_strategy(
if target_name.is_none() {
return Ok(());
}
let schema: Option<String> = text_or_none(&native_field(&target_config, "schema")?)?;
let schema: Option<String> = optional_text(&native_field(&target_config, "schema")?)?;
if schema.is_some_and(|schema| !python_strip(&schema).is_empty()) {
return Ok(());
}
Expand All @@ -198,7 +191,7 @@ pub(crate) fn named_target_schema_strategy(
Some(cli_vars.clone()).filter(|value| !value.is_none()),
false,
)?;
let help: Option<String> = match text_or_none(&mapping_get(&connection, "schema")?)? {
let help: Option<String> = match optional_text(&mapping_get(&connection, "schema")?)? {
Some(value) => {
message.push_str(&ignored_connection_note("schema", &value));
Some(project_setting_help(
Expand Down Expand Up @@ -336,7 +329,7 @@ fn target_namespaces(
for item in mapping_items(&connection)?.try_iter()? {
let item: Bound<'_, PyAny> = item?;
let key: Bound<'_, PyAny> = item.get_item(0)?;
let Some(key) = text_or_none(&key)? else {
let Some(key) = optional_text(&key)? else {
continue;
};
if CONNECTION_IDENTITY_KEYS.contains(&key.as_str()) {
Expand Down Expand Up @@ -476,7 +469,7 @@ pub(crate) fn validate_project_targets<'py>(

/// Whether the adapter needs every model and seed database and schema resolved.
pub(crate) fn requires_explicit_targets(adapter_name: &Bound<'_, PyAny>) -> PyResult<bool> {
Ok(text_or_none(adapter_name)?
Ok(optional_text(adapter_name)?
.as_deref()
.is_some_and(|name| EXPLICIT_TARGET_ADAPTERS.contains(&name)))
}
Expand Down Expand Up @@ -528,8 +521,8 @@ fn required_target_parts(
project: &Bound<'_, PyAny>,
) -> PyResult<()> {
let py: Python<'_> = target.py();
let database: Option<String> = optional_text(&native_field(target, "database")?)?;
let schema: Option<String> = optional_text(&native_field(target, "schema")?)?;
let database: Option<String> = nullable_text(&native_field(target, "database")?)?;
let schema: Option<String> = nullable_text(&native_field(target, "schema")?)?;
let missing: Vec<&str> = missing_target_parts(database.as_deref(), schema.as_deref());
let Some(first) = missing.first() else {
return Ok(());
Expand Down Expand Up @@ -591,7 +584,7 @@ pub(crate) fn managed_write_schemas(
return Ok(());
}
let project_schema: Option<String> = explicit_write_schema(
text_or_none(&native_field(project, "effective_target_schema")?)?.as_deref(),
optional_text(&native_field(project, "effective_target_schema")?)?.as_deref(),
);
for (label, attribute) in [
("Model", "models"),
Expand Down Expand Up @@ -631,7 +624,7 @@ pub(crate) fn managed_write_schemas(
let source_loaders: HashSet<String> = managed_source_loaders(project)?;
for loader in native_field(project, "loader_functions")?.try_iter()? {
let loader: Bound<'_, PyAny> = loader?;
let destination: Option<String> = text_or_none(&native_field(&loader, "destination")?)?;
let destination: Option<String> = optional_text(&native_field(&loader, "destination")?)?;
if destination.is_some_and(|destination| destination.contains('.'))
|| source_loaders.contains(&native_field(&loader, "name")?.extract::<String>()?)
{
Expand All @@ -653,7 +646,7 @@ pub(crate) fn managed_write_schemas(
pub(crate) fn managed_source_loaders(project: &Bound<'_, PyAny>) -> PyResult<HashSet<String>> {
let mut loaders: HashSet<String> = HashSet::new();
for source in native_field(project, "sources")?.try_iter()? {
if let Some(loader) = text_or_none(&native_field(
if let Some(loader) = optional_text(&native_field(
&native_field(&source?, "source_entry")?,
"loader",
)?)? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ use crate::compile_driver::_helpers::setup::targets::DEFAULTS_SECTION;
use crate::compile_driver::_helpers::setup::targets::ignored_connection_note;
use crate::compile_driver::_helpers::setup::targets::planner_error;
use crate::compile_driver::_helpers::setup::targets::project_setting_help;
use crate::compile_driver::_helpers::setup::targets::text_or_none;
use pyo3::prelude::{Bound, PyAny, PyResult, Python};
use sqlbuild_analysis::assembly::target_checks::constants::MANAGED_WRITE_SCHEMA_CODE;
use sqlbuild_analysis::assembly::target_checks::errors::TargetError;
use sqlbuild_analysis::assembly::target_checks::main::explicit_write_schema::explicit_write_schema;
use sqlbuild_analysis::assembly::target_checks::main::target_error_message::target_error_message;
use sqlbuild_python_base::boundary::main::optional_text::optional_text;

/// Raise when neither the resource nor the project names a physical write schema.
pub fn managed_write_schema(
resource_label: &str,
schema: &Bound<'_, PyAny>,
(project_schema, ignored): (Option<&str>, Option<&str>),
) -> PyResult<()> {
if explicit_write_schema(text_or_none(schema)?.as_deref()).is_some() || project_schema.is_some()
if explicit_write_schema(optional_text(schema)?.as_deref()).is_some()
|| project_schema.is_some()
{
return Ok(());
}
Expand Down
Loading
Loading