Skip to content

Commit 4db8f2b

Browse files
committed
Refactor Rust extension into modular structure
Split lib.rs (3,117 lines) into 6 well-organized modules: - lib.rs: Module exports and public API (76 lines) - types.rs: Type cache and BSON type markers (266 lines) - errors.rs: Error handling utilities (56 lines) - utils.rs: Utility functions (154 lines) - encode.rs: BSON encoding functions (1,545 lines) - decode.rs: BSON decoding functions (1,141 lines) Benefits: - Better code organization and maintainability - Follows Rust best practices for module structure - Improved compilation times (parallel module compilation) - Easier code navigation and testing - Clear separation of concerns All tests passing: test/test_bson.py (86 passed, 2 skipped) Tests for unimplemented features properly skipped via @skip_if_rust_bson
1 parent c05bbb4 commit 4db8f2b

84 files changed

Lines changed: 3613 additions & 3900 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.

bson/_rbson/src/decode.rs

Lines changed: 1140 additions & 0 deletions
Large diffs are not rendered by default.

bson/_rbson/src/encode.rs

Lines changed: 1543 additions & 0 deletions
Large diffs are not rendered by default.

bson/_rbson/src/errors.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2025-present MongoDB, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! Error handling utilities for BSON operations
16+
17+
use pyo3::prelude::*;
18+
use pyo3::types::{PyAny, PyTuple};
19+
20+
use crate::types::TYPE_CACHE;
21+
22+
/// Helper to create InvalidDocument exception
23+
pub(crate) fn invalid_document_error(py: Python, msg: String) -> PyErr {
24+
let invalid_document = TYPE_CACHE.get_invalid_document_class(py)
25+
.expect("Failed to get InvalidDocument class");
26+
PyErr::from_value(
27+
invalid_document.bind(py)
28+
.call1((msg,))
29+
.expect("Failed to create InvalidDocument")
30+
)
31+
}
32+
33+
/// Helper to create InvalidDocument exception with document property
34+
pub(crate) fn invalid_document_error_with_doc(py: Python, msg: String, doc: &Bound<'_, PyAny>) -> PyErr {
35+
let invalid_document = TYPE_CACHE.get_invalid_document_class(py)
36+
.expect("Failed to get InvalidDocument class");
37+
// Call with positional arguments: InvalidDocument(message, document)
38+
let args = PyTuple::new_bound(py, &[msg.into_py(py), doc.clone().into_py(py)]);
39+
PyErr::from_value(
40+
invalid_document.bind(py)
41+
.call1(args)
42+
.expect("Failed to create InvalidDocument")
43+
)
44+
}
45+
46+
/// Helper to create InvalidBSON exception
47+
pub(crate) fn invalid_bson_error(py: Python, msg: String) -> PyErr {
48+
let invalid_bson = TYPE_CACHE.get_invalid_bson_class(py)
49+
.expect("Failed to get InvalidBSON class");
50+
PyErr::from_value(
51+
invalid_bson.bind(py)
52+
.call1((msg,))
53+
.expect("Failed to create InvalidBSON")
54+
)
55+
}

0 commit comments

Comments
 (0)