Skip to content
Open
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
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions THIRD-PARTY-LICENSES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2739,13 +2739,13 @@ limitations under the License.
** arraydeque; version 0.5.1 -- https://crates.io/crates/arraydeque
** get-size-derive2; version 0.11.1 -- https://crates.io/crates/get-size-derive2
** get-size2; version 0.11.1 -- https://crates.io/crates/get-size2
** granit-parser; version 1.2.0 -- https://crates.io/crates/granit-parser
** granit-parser; version 2.1.0 -- https://crates.io/crates/granit-parser
** itoa; version 1.0.18 -- https://crates.io/crates/itoa
** libc; version 0.2.189 -- https://crates.io/crates/libc
** manyhow-macros; version 0.14.1 -- https://crates.io/crates/manyhow-macros
** openjd-expr; version 0.12.0 -- https://crates.io/crates/openjd-expr
** openjd-model; version 0.13.0 -- https://crates.io/crates/openjd-model
** openjd-sessions; version 0.8.1 -- https://crates.io/crates/openjd-sessions
** openjd-expr; version 0.14.0 -- https://crates.io/crates/openjd-expr
** openjd-model; version 0.15.0 -- https://crates.io/crates/openjd-model
** openjd-sessions; version 0.9.0 -- https://crates.io/crates/openjd-sessions
** pin-project-lite; version 0.2.17 -- https://crates.io/crates/pin-project-lite
** portable-atomic; version 1.15.0 -- https://crates.io/crates/portable-atomic
** proc-macro2; version 1.0.107 -- https://crates.io/crates/proc-macro2
Expand All @@ -2758,7 +2758,7 @@ limitations under the License.
** rustc-hash; version 2.1.3 -- https://crates.io/crates/rustc-hash
** rustversion; version 1.0.23 -- https://crates.io/crates/rustversion
** ryu; version 1.0.23 -- https://crates.io/crates/ryu
** serde-saphyr; version 1.2.0 -- https://crates.io/crates/serde-saphyr
** serde-saphyr; version 2.1.1 -- https://crates.io/crates/serde-saphyr
** serde; version 1.0.229 -- https://crates.io/crates/serde
** serde_core; version 1.0.229 -- https://crates.io/crates/serde_core
** serde_derive; version 1.0.229 -- https://crates.io/crates/serde_derive
Expand Down
6 changes: 3 additions & 3 deletions rust-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ name = "_openjd_rs"
crate-type = ["cdylib", "rlib"]

[dependencies]
openjd-expr = "0.12.0"
openjd-model = "0.13.0"
openjd-sessions = "0.8.1"
openjd-expr = "0.14.0"
openjd-model = "0.15.0"
openjd-sessions = "0.9.0"
tokio = { version = "1", features = ["rt-multi-thread"] }
uuid = { version = "1", features = ["v4"] }
serde_json = "1"
Expand Down
2 changes: 1 addition & 1 deletion rust-bindings/src/expr/expr_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl PyExprValue {

fn __len__(&self) -> PyResult<usize> {
match &self.inner {
ExprValue::RangeExpr(r) => Ok(r.len()),
ExprValue::RangeExpr(r) => crate::len_to_usize(r.len()),
_ => self.inner.list_len().ok_or_else(|| {
pyo3::exceptions::PyTypeError::new_err("ExprValue is not a list or range_expr")
}),
Expand Down
24 changes: 18 additions & 6 deletions rust-bindings/src/expr/path_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pyo3::types::PyDict;
#[cfg(feature = "stub-gen")]
use pyo3_stub_gen::derive::*;

use openjd_expr::path_mapping::{PathFormat, PathMappingRule};
use openjd_expr::path_mapping::{PathFormat, PathMapper, PathMappingRule};

use crate::expr::path_format::PyPathFormat;

Expand Down Expand Up @@ -167,16 +167,28 @@ impl PyPathMappingRule {
h.finish()
}

/// Apply this rule to `path`. Returns `(True, mapped)` when the rule
/// matches and `(False, path)` when it does not. Raises `ValueError`
/// when the rule itself is invalid, for example a POSIX or WINDOWS
/// `source_path` that is not absolute.
#[pyo3(signature = (*, path, output_format=None))]
fn apply(&self, path: &str, output_format: Option<PyPathFormat>) -> (bool, String) {
fn apply(&self, path: &str, output_format: Option<PyPathFormat>) -> PyResult<(bool, String)> {
// openjd-expr applies rules only through a `PathMapper`, so a
// one-rule mapper gives this rule's mapping. `validate` first so the
// error does not name an index in a list the caller never made.
let invalid = |e: openjd_expr::path_mapping::InvalidPathMappingRule| {
pyo3::exceptions::PyValueError::new_err(e.to_string())
};
self.inner.validate().map_err(invalid)?;
let mapper = PathMapper::new(vec![self.inner.clone()]).map_err(invalid)?;
let result = match output_format {
Some(fmt) => self.inner.apply_with_format(path, fmt.into()),
None => self.inner.apply(path),
Some(fmt) => mapper.apply_with_format(path, fmt.into()),
None => mapper.apply(path),
};
match result {
Ok(match result {
Some(mapped) => (true, mapped),
None => (false, path.to_string()),
}
})
}

fn to_dict(&self) -> std::collections::HashMap<String, String> {
Expand Down
24 changes: 17 additions & 7 deletions rust-bindings/src/expr/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use pyo3::types::{PyDict, PyType};
#[cfg(feature = "stub-gen")]
use pyo3_stub_gen::derive::*;

use openjd_expr::path_mapping::PathMapper;
use openjd_expr::profile::{ExprExtension, ExprProfile, ExprRevision, HostContext};

use crate::expr::path_mapping::PyPathMappingRule;
Expand Down Expand Up @@ -237,13 +238,19 @@ impl PyHostContext {
/// host context (`apply_path_mapping` is registered, returns
/// the path unchanged). Passing zero rules is *not* the same as
/// passing no host context at all.
///
/// Raises `ValueError` when a rule is invalid, for example a POSIX or
/// WINDOWS `source_path` that is not absolute. The message names the
/// rule's index in `rules`.
#[classmethod]
fn with_rules(_cls: &Bound<'_, PyType>, rules: Vec<PyPathMappingRule>) -> Self {
fn with_rules(_cls: &Bound<'_, PyType>, rules: Vec<PyPathMappingRule>) -> PyResult<Self> {
let rust_rules: Vec<openjd_expr::path_mapping::PathMappingRule> =
rules.into_iter().map(|r| r.inner).collect();
Self {
inner: HostContext::WithRules(Arc::new(rust_rules)),
}
let mapper = PathMapper::new(rust_rules)
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e.to_string()))?;
Ok(Self {
inner: HostContext::WithRules(Arc::new(mapper)),
})
}

/// Whether this host context registers any host-context
Expand All @@ -263,7 +270,7 @@ impl PyHostContext {
HostContext::None => "HostContext.none()".to_string(),
HostContext::Unresolved => "HostContext.unresolved()".to_string(),
HostContext::WithRules(rules) => {
format!("HostContext.with_rules(<{} rule(s)>)", rules.len())
format!("HostContext.with_rules(<{} rule(s)>)", rules.rules().len())
}
}
}
Expand Down Expand Up @@ -306,6 +313,7 @@ impl PyHostContext {
HostContext::WithRules(rules) => {
let f = cls.getattr("with_rules")?;
let py_rules: Vec<PyPathMappingRule> = rules
.rules()
.iter()
.cloned()
.map(|inner| PyPathMappingRule { inner })
Expand All @@ -328,8 +336,9 @@ fn host_context_eq(a: &HostContext, b: &HostContext) -> bool {
match (a, b) {
(HostContext::None, HostContext::None) => true,
(HostContext::Unresolved, HostContext::Unresolved) => true,
(HostContext::WithRules(la), HostContext::WithRules(lb)) => {
(HostContext::WithRules(ma), HostContext::WithRules(mb)) => {
// Compare rule-by-rule; `Arc` identity is irrelevant.
let (la, lb) = (ma.rules(), mb.rules());
la.len() == lb.len()
&& la.iter().zip(lb.iter()).all(|(ra, rb)| {
ra.source_path_format == rb.source_path_format
Expand All @@ -347,7 +356,8 @@ fn host_context_hash(hc: &HostContext) -> u64 {
match hc {
HostContext::None => 0u8.hash(&mut h),
HostContext::Unresolved => 1u8.hash(&mut h),
HostContext::WithRules(rules) => {
HostContext::WithRules(mapper) => {
let rules = mapper.rules();
2u8.hash(&mut h);
(rules.len() as u64).hash(&mut h);
for r in rules.iter() {
Expand Down
16 changes: 8 additions & 8 deletions rust-bindings/src/expr/range_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl PyIntRange {
self.inner.step()
}

fn __len__(&self) -> usize {
self.inner.len()
fn __len__(&self) -> PyResult<usize> {
crate::len_to_usize(self.inner.len())
}

fn __contains__(&self, value: i64) -> bool {
Expand Down Expand Up @@ -205,8 +205,8 @@ impl PyRangeExpr {
.ok_or_else(|| pyo3::exceptions::PyValueError::new_err("Range expression is empty"))
}

fn __len__(&self) -> usize {
self.inner.len()
fn __len__(&self) -> PyResult<usize> {
crate::len_to_usize(self.inner.len())
}

fn __eq__(&self, other: &PyRangeExpr) -> bool {
Expand All @@ -218,13 +218,13 @@ impl PyRangeExpr {
}

fn __getitem__(&self, index: isize) -> PyResult<i64> {
let len = self.inner.len();
let len = i128::from(self.inner.len());
let idx = if index < 0 {
len as isize + index
len + index as i128
} else {
index
index as i128
};
if idx < 0 || idx as usize >= len {
if idx < 0 || idx >= len {
return Err(pyo3::exceptions::PyIndexError::new_err(
"index out of range",
));
Expand Down
12 changes: 12 additions & 0 deletions rust-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ fn register_renamed_exception(
Ok(())
}

/// Convert a `u64` count from the openjd crates to the `usize` that
/// `__len__` must return. The crates keep counts 64-bit on every target,
/// so on a 32-bit target a count above `usize::MAX` raises `OverflowError`,
/// which is what Python's `len()` raises for a length it cannot represent.
pub(crate) fn len_to_usize(len: u64) -> PyResult<usize> {
usize::try_from(len).map_err(|_| {
pyo3::exceptions::PyOverflowError::new_err(
"length does not fit in this platform's Py_ssize_t",
)
})
}

// ── Module ──

#[pymodule]
Expand Down
24 changes: 12 additions & 12 deletions rust-bindings/src/model/step_param_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ pub(crate) struct PyStepParameterSpaceIterator {
space: StepParameterSpace,
/// Cached length captured at construction time. Used by `__len__`
/// for non-adaptive spaces (adaptive spaces raise instead).
len: usize,
len: u64,
/// Cached parameter names captured at construction time.
names: HashSet<String>,
/// The persistent iterator that backs `__next__`, `reset_iter`,
/// `__contains__`, and `chunks_default_task_count` (getter and
/// setter). Holding it across calls is what lets the setter
/// actually mutate state — the underlying `Arc<AtomicUsize>` for
/// actually mutate state — the underlying `Arc<AtomicU64>` for
/// adaptive chunking lives inside this iterator.
///
/// `Mutex` is required because pyclass types must be `Sync`. The
Expand All @@ -127,7 +127,7 @@ pub(crate) struct PyStepParameterSpaceIterator {
/// so `__getitem__` can rebuild an equivalent iterator. Without it,
/// random access would silently fall back to the template's own
/// `defaultTaskCount` and disagree with iteration.
chunk_override: Option<usize>,
chunk_override: Option<u64>,
}

#[cfg_attr(feature = "stub-gen", gen_stub_pymethods)]
Expand All @@ -153,20 +153,20 @@ impl PyStepParameterSpaceIterator {
space: Option<&PyStepParameterSpace>,
chunks_task_count_override: Option<i64>,
) -> PyResult<Self> {
// Taken as i64, not usize, so a negative value reaches this check instead of
// Taken as i64, not u64, so a negative value reaches this check instead of
// failing pyo3's unsigned extraction with OverflowError. Every non-positive
// value should report the same ValueError the message and the spec promise.
//
// Rejected rather than clamped: openjd-model applies `.max(1)` to the override,
// so 0 would silently mean 1. The `chunks_default_task_count` setter already
// refuses 0, and the two should not disagree.
let chunks_task_count_override: Option<usize> = match chunks_task_count_override {
let chunks_task_count_override: Option<u64> = match chunks_task_count_override {
Some(n) if n <= 0 => {
return Err(pyo3::exceptions::PyValueError::new_err(
"chunks_task_count_override must be a positive integer.",
));
}
Some(n) => Some(n as usize),
Some(n) => Some(n as u64),
None => None,
};
let ps = if let Some(s) = space {
Expand Down Expand Up @@ -210,7 +210,7 @@ impl PyStepParameterSpaceIterator {
"Length is not available because the parameter space uses adaptive chunking.",
));
}
Ok(self.len)
crate::len_to_usize(self.len)
}

fn __getitem__(&self, py: Python<'_>, index: isize) -> PyResult<Py<PyDict>> {
Expand All @@ -235,15 +235,15 @@ impl PyStepParameterSpaceIterator {
));
}
let idx = if index < 0 {
let adjusted = self.len as isize + index;
let adjusted = i128::from(self.len) + index as i128;
if adjusted < 0 {
return Err(pyo3::exceptions::PyIndexError::new_err(
"index out of range",
));
}
adjusted as usize
adjusted as u64
} else {
index as usize
index as u64
};
match iter.get(idx) {
Some(params) => task_param_set_to_py(py, &params),
Expand Down Expand Up @@ -312,13 +312,13 @@ impl PyStepParameterSpaceIterator {
}

#[getter]
fn chunks_default_task_count(&self) -> Option<usize> {
fn chunks_default_task_count(&self) -> Option<u64> {
let iter = lock_recover(&self.iter);
iter.chunks_default_task_count()
}

#[setter]
fn set_chunks_default_task_count(&self, value: usize) -> PyResult<()> {
fn set_chunks_default_task_count(&self, value: u64) -> PyResult<()> {
if value == 0 {
return Err(pyo3::exceptions::PyValueError::new_err(
"chunks_default_task_count must be a positive integer.",
Expand Down
Loading
Loading