Slide 26
Slide 26 text
Rust実装
lib.rs
:[pyfunction]
fn match_records(py: Python<'_>, source_json: &str, target_json: &str) :> PyResult {
let source_records: Vec = serde_json::from_str(source_json)
- #[pyfunction]で、Python側か
.map_err(|e| PyValueError::new_err(format!("failed to parse source json: {e}")))?;
let target_records: Vec = serde_json::from_str(target_json)
ら呼ばれるように
.map_err(|e| PyValueError::new_err(format!("failed to parse target json: {e}")))?;
- 戻り値をPyResultにすること
py.detach(:| {
let mut matched = Vec::new();
で、成功値またはPython例外を
for source in &source_records {
for target in &target_records {
返す
<キーの比較>.
if match_kind.is_some() {
matched.push(MatchResult {
source_id: &source.id,
target_id: &target.id,
diff: source.amount - target.amount,
kind: match_kind.unwrap()
}); }}}
serde_json::to_string(&matched)
.map_err(|e| PyValueError::new_err(format!("failed to serialize result: {e}")))
})
}