Skip to content

Commit 4e8afee

Browse files
committed
wip: replay the shared exception constructor and text reader (#1528) on the large-project tree
Applies the slice's shared new_error/exception_from constructor and the base optional_text/text_item readers, keeping the large-project tree's behaviour. - The tree's own optional_text calls (its local reader in cli dispatch and the planner's reader) are unchanged; only the calls the slice converted use the base reader. - The Python-tests input error keeps the tree's help text, which names tests/orchestration/. - main's within_identity_pass, which the tree does not have, is not carried over. Validation: cargo check --workspace --all-targets passes with no warnings, and every line the tree added to a touched file is still present. Nothing is built or tested yet. fensu reports 349 faults; the three new ones ask to lower Python-crossing budgets that keep the tree's values for the final pass.
1 parent dadc0f3 commit 4e8afee

70 files changed

Lines changed: 460 additions & 705 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/sqlbuild-python-base/src/boundary/main/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ pub mod imported;
88
pub mod json_value_to_python;
99
pub mod keywords;
1010
pub mod native_compiler_error;
11+
pub mod optional_text;
1112
pub mod register_expected_errors;
1213
pub mod register_native_compiler_error;
1314
pub mod register_oracles;
15+
pub mod text_item;
1416
pub mod utf16_units;
1517
pub mod value_error;
1618
pub mod within;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! `value` as text when it is a `str`, else `None`.
2+
3+
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult};
4+
use pyo3::types::PyString;
5+
6+
/// `value` as text when it is a `str`, else `None`.
7+
pub fn optional_text(value: &Bound<'_, PyAny>) -> PyResult<Option<String>> {
8+
if value.is_instance_of::<PyString>() {
9+
return Ok(Some(value.extract()?));
10+
}
11+
Ok(None)
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! `values[key]` as text when it is present and a `str`, else `None`.
2+
3+
use crate::boundary::main::optional_text::optional_text;
4+
use pyo3::prelude::{Bound, PyResult};
5+
use pyo3::types::{PyDict, PyDictMethods};
6+
7+
/// `values[key]` as text when it is present and a `str`, else `None`.
8+
pub fn text_item(values: &Bound<'_, PyDict>, key: &str) -> PyResult<Option<String>> {
9+
match values.get_item(key)? {
10+
Some(value) => optional_text(&value),
11+
None => Ok(None),
12+
}
13+
}

‎crates/sqlbuild-python-base/src/native_host/_helpers/host.rs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,3 @@ pub(crate) fn log_warning_message(py: Python<'_>, logger: &str, message: &str) -
127127
.call_method1("warning", (message,))
128128
.map(|_| ())
129129
}
130-
131-
/// The host exception class `id` raised with `message`, or the error looking it up or raising it.
132-
pub(crate) fn host_error(py: Python<'_>, id: impl Into<HostId>, message: &str) -> PyErr {
133-
entry(py, id)
134-
.and_then(|class| class.call1((message,)))
135-
.map_or_else(|error| error, PyErr::from_value)
136-
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! The host exception class `id` raised with `message`, or the error looking it up or raising it.
22
3-
use crate::native_host::_helpers::host;
3+
use crate::native_host::_helpers::host::entry;
4+
use crate::records::main::construct::exception_from::exception_from;
45
use pyo3::PyErr;
56
use pyo3::prelude::Python;
67
use sqlbuild_host::entries::models::HostId;
78

89
/// The host exception class `id` raised with `message`, or the error looking it up or raising it.
9-
#[inline]
1010
pub fn host_error(py: Python<'_>, id: impl Into<HostId>, message: &str) -> PyErr {
11-
host::host_error(py, id, message)
11+
exception_from(entry(py, id), message, (None, None))
1212
}

‎crates/sqlbuild-python-base/src/records/_helpers/native_classes/construct.rs‎

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,37 @@ pub(crate) fn error_class(py: Python<'_>, id: ErrorId) -> PyResult<Bound<'_, PyA
4343

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

51-
/// A raisable native `id` error carrying `message` and the diagnostic `code`.
52-
pub(crate) fn new_error(py: Python<'_>, id: ErrorId, message: String, code: &str) -> PyErr {
53-
let raised = (|| {
54-
let options: Bound<'_, PyDict> = PyDict::new(py);
55-
options.set_item("code", code)?;
56-
error_class(py, id)?.call((message,), Some(&options))
57-
})();
58-
match raised {
59-
Ok(error) => PyErr::from_value(error),
60-
Err(error) => error,
61-
}
49+
/// The native `id` exception with `message` and the given `code` and `help`, or the error raising it.
50+
pub(crate) fn new_error(
51+
py: Python<'_>,
52+
id: ErrorId,
53+
message: &str,
54+
keywords: (Option<&str>, Option<&str>),
55+
) -> PyErr {
56+
exception_from(error_class(py, id), message, keywords)
57+
}
58+
59+
/// The exception `class` with `message` and the given `code` and `help`, or the error raising it.
60+
pub(crate) fn exception_from(
61+
class: PyResult<Bound<'_, PyAny>>,
62+
message: &str,
63+
(code, help): (Option<&str>, Option<&str>),
64+
) -> PyErr {
65+
class
66+
.and_then(|class| {
67+
let keywords: Bound<'_, PyDict> = PyDict::new(class.py());
68+
if let Some(code) = code {
69+
keywords.set_item("code", code)?;
70+
}
71+
if let Some(help) = help {
72+
keywords.set_item("help", help)?;
73+
}
74+
class.call((message,), Some(&keywords))
75+
})
76+
.map_or_else(|error| error, PyErr::from_value)
6277
}
6378

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

84-
/// A raisable native `id` error carrying `message`, the diagnostic `code` and optional `help`.
85-
pub(crate) fn new_helped_error(
86-
py: Python<'_>,
87-
id: ErrorId,
88-
(message, code): (String, &str),
89-
help: Option<&str>,
90-
) -> PyErr {
91-
let raised = (|| {
92-
let options: Bound<'_, PyDict> = PyDict::new(py);
93-
options.set_item("code", code)?;
94-
if let Some(help) = help {
95-
options.set_item("help", help)?;
96-
}
97-
error_class(py, id)?.call((message,), Some(&options))
98-
})();
99-
match raised {
100-
Ok(error) => PyErr::from_value(error),
101-
Err(error) => error,
102-
}
103-
}
104-
10599
/// The native record class or string enum `id` as registered, ignoring later attribute replacement.
106100
pub(crate) fn record_class(py: Python<'_>, id: RecordId) -> PyResult<Bound<'_, PyAny>> {
107101
let classes: &Vec<Py<PyAny>> = CLASSES
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! The exception `class` with `message` and the given `code` and `help`.
2+
3+
use crate::records::_helpers::native_classes::construct;
4+
use pyo3::prelude::{Bound, PyAny, PyErr, PyResult};
5+
6+
/// The exception `class` with `message` and the given `code` and `help`, or the error raising it.
7+
#[inline]
8+
pub fn exception_from(
9+
class: PyResult<Bound<'_, PyAny>>,
10+
message: &str,
11+
keywords: (Option<&str>, Option<&str>),
12+
) -> PyErr {
13+
construct::exception_from(class, message, keywords)
14+
}

‎crates/sqlbuild-python-base/src/records/main/construct/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pub mod enum_from_value;
22
pub mod enum_member;
33
pub mod error_class;
4+
pub mod exception_from;
45
pub mod new_annotated_error;
56
pub mod new_error;
6-
pub mod new_helped_error;
77
pub mod new_plain_error;
88
pub mod new_record;
99
pub mod new_record_with;
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
//! A raisable native `id` error carrying `message` and the diagnostic `code`.
1+
//! The native `id` exception raised with `message` and the `code` and `help` keywords that are given.
22
33
use crate::records::_helpers::native_classes::construct;
44
use crate::records::types::ErrorId;
55
use pyo3::prelude::PyErr;
66
use pyo3::prelude::Python;
77

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

‎crates/sqlbuild-python-base/src/records/main/construct/new_helped_error.rs‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)