Skip to content

Commit 18b47ce

Browse files
committed
Refactor Python FFI error handling for clarity and propagation
- Use combinators to propagate errors in all #[pyfunction] wrappers - Remove unnecessary `.into()` in serialize - Suppress clippy::useless_conversion for macro-generated code - Simplify Apple callback state check with let-else pattern
1 parent 46c533e commit 18b47ce

3 files changed

Lines changed: 61 additions & 57 deletions

File tree

crates/cli/src/deploy/known_hosts.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
/// ```sh
3030
/// ssh-keyscan -t ed25519 api.smbcloud.xyz api-1.smbcloud.xyz 2>/dev/null
3131
/// ```
32-
3332
/// Pinned ed25519 host key for `api.smbcloud.xyz` (NodeJs / Static tier).
3433
pub const API_SMBCLOUD_XYZ: &str =
3534
"api.smbcloud.xyz ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINGy4LHGyOilS7SXo770V3tQnXDRVQr7X7JsPLCfy4XB";

crates/smbcloud-auth-sdk-py/src/lib.rs

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#![allow(unexpected_cfgs, unsafe_op_in_unsafe_fn)]
2+
// The #[pyfunction] macro generates FFI wrapper code that calls From<PyErr>::from(err)
3+
// on the error path of every wrapped function. Since PyErr: From<PyErr> is the identity
4+
// impl, clippy flags it as useless_conversion. We cannot change macro-generated code,
5+
// so suppress the lint at crate level.
6+
#![allow(clippy::useless_conversion)]
27

38
use pyo3::{
49
create_exception,
@@ -67,7 +72,7 @@ fn serialize<'py, T>(py: Python<'py>, value: &T) -> PyResult<PyObject>
6772
where
6873
T: Serialize,
6974
{
70-
Ok(pythonize(py, value)?.unbind().into())
75+
Ok(pythonize(py, value)?.unbind())
7176
}
7277

7378
fn login_payload(status: AccountStatus) -> LoginPayload {
@@ -120,18 +125,18 @@ fn signup_with_client(
120125
email: &str,
121126
password: &str,
122127
) -> PyResult<PyObject> {
123-
let env = parse_env(env)?;
124-
let client = client_credentials(app_id, app_secret);
125-
let result = runtime()
126-
.block_on(rust_signup_with_client(
127-
env,
128-
client,
129-
email.to_string(),
130-
password.to_string(),
131-
))
132-
.map_err(native_error)?;
133-
134-
serialize(py, &result)
128+
parse_env(env).and_then(|env| {
129+
let client = client_credentials(app_id, app_secret);
130+
runtime()
131+
.block_on(rust_signup_with_client(
132+
env,
133+
client,
134+
email.to_string(),
135+
password.to_string(),
136+
))
137+
.map_err(native_error)
138+
.and_then(|result| serialize(py, &result))
139+
})
135140
}
136141

137142
#[pyfunction]
@@ -143,19 +148,19 @@ fn login_with_client(
143148
email: &str,
144149
password: &str,
145150
) -> PyResult<PyObject> {
146-
let env = parse_env(env)?;
147-
let client = client_credentials(app_id, app_secret);
148-
let result = runtime()
149-
.block_on(rust_login_with_client(
150-
env,
151-
client,
152-
email.to_string(),
153-
password.to_string(),
154-
))
155-
.map(login_payload)
156-
.map_err(native_error)?;
157-
158-
serialize(py, &result)
151+
parse_env(env).and_then(|env| {
152+
let client = client_credentials(app_id, app_secret);
153+
runtime()
154+
.block_on(rust_login_with_client(
155+
env,
156+
client,
157+
email.to_string(),
158+
password.to_string(),
159+
))
160+
.map(login_payload)
161+
.map_err(native_error)
162+
.and_then(|result| serialize(py, &result))
163+
})
159164
}
160165

161166
#[pyfunction]
@@ -165,16 +170,16 @@ fn logout_with_client(
165170
app_secret: &str,
166171
access_token: &str,
167172
) -> PyResult<()> {
168-
let env = parse_env(env)?;
169-
let client = client_credentials(app_id, app_secret);
170-
171-
runtime()
172-
.block_on(rust_logout_with_client(
173-
env,
174-
client,
175-
access_token.to_string(),
176-
))
177-
.map_err(native_error)
173+
parse_env(env).and_then(|env| {
174+
let client = client_credentials(app_id, app_secret);
175+
runtime()
176+
.block_on(rust_logout_with_client(
177+
env,
178+
client,
179+
access_token.to_string(),
180+
))
181+
.map_err(native_error)
182+
})
178183
}
179184

180185
#[pyfunction]
@@ -185,13 +190,13 @@ fn me_with_client(
185190
app_secret: &str,
186191
access_token: &str,
187192
) -> PyResult<PyObject> {
188-
let env = parse_env(env)?;
189-
let client = client_credentials(app_id, app_secret);
190-
let result = runtime()
191-
.block_on(rust_me_with_client(env, client, access_token))
192-
.map_err(native_error)?;
193-
194-
serialize(py, &result)
193+
parse_env(env).and_then(|env| {
194+
let client = client_credentials(app_id, app_secret);
195+
runtime()
196+
.block_on(rust_me_with_client(env, client, access_token))
197+
.map_err(native_error)
198+
.and_then(|result| serialize(py, &result))
199+
})
195200
}
196201

197202
#[pyfunction]
@@ -201,12 +206,12 @@ fn remove_with_client(
201206
app_secret: &str,
202207
access_token: &str,
203208
) -> PyResult<()> {
204-
let env = parse_env(env)?;
205-
let client = client_credentials(app_id, app_secret);
206-
207-
runtime()
208-
.block_on(rust_remove_with_client(env, client, access_token))
209-
.map_err(native_error)
209+
parse_env(env).and_then(|env| {
210+
let client = client_credentials(app_id, app_secret);
211+
runtime()
212+
.block_on(rust_remove_with_client(env, client, access_token))
213+
.map_err(native_error)
214+
})
210215
}
211216

212217
#[pymodule(name = "_native")]

crates/smbcloud-auth-sdk/src/apple.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ pub fn parse_callback_url(
8787
});
8888
}
8989

90-
if let Some(expected_state) = expected_state {
91-
if state.as_deref() != Some(expected_state) {
92-
return Err(ErrorResponse::Error {
93-
error_code: ErrorCode::InvalidParams,
94-
message: "Apple callback state mismatch.".to_string(),
95-
});
96-
}
90+
if let Some(expected_state) = expected_state
91+
&& state.as_deref() != Some(expected_state)
92+
{
93+
return Err(ErrorResponse::Error {
94+
error_code: ErrorCode::InvalidParams,
95+
message: "Apple callback state mismatch.".to_string(),
96+
});
9797
}
9898

9999
Ok(AppleAuthSession {

0 commit comments

Comments
 (0)