Skip to content

Commit

Permalink
enable anyhow feature for pyo3 for automatic anyhow error conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Esgrove committed Oct 26, 2024
1 parent 20654d4 commit f056bce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
1 change: 1 addition & 0 deletions python-pyo3/Cargo.lock

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

2 changes: 1 addition & 1 deletion python-pyo3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib"]
[dependencies]
anyhow = "1.0.91"
nitor-vault = { path = "../rust" }
pyo3 = "0.22.0"
pyo3 = { version = "0.22.0", features = ["anyhow"] }
tokio = "1.41.0"

[profile.release]
Expand Down
49 changes: 13 additions & 36 deletions python-pyo3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use tokio::runtime::Runtime;

Expand All @@ -13,16 +12,9 @@ const fn version() -> &'static str {
VERSION
}

#[allow(clippy::needless_pass_by_value)]
/// Convert `anyhow::Error` to `PyErr`
fn anyhow_to_py_err(err: anyhow::Error) -> PyErr {
PyRuntimeError::new_err(format!("{err:?}"))
}

/// Convert `VaultError` to `PyErr`
fn vault_error_to_py_err(err: VaultError) -> PyErr {
let anyhow_error: anyhow::Error = err.into();
PyRuntimeError::new_err(format!("{anyhow_error:?}"))
/// Convert `VaultError` to `anyhow::Error`
fn vault_error_to_py_err(err: VaultError) -> anyhow::Error {
err.into()
}

#[pyfunction(signature = (vault_stack=None, region=None, bucket=None, key=None, prefix=None))]
Expand All @@ -38,7 +30,7 @@ fn all(
.await
.map_err(vault_error_to_py_err)?;

cli::list_all_keys(&vault).await.map_err(anyhow_to_py_err)?;
cli::list_all_keys(&vault).await?;

Ok(())
})
Expand All @@ -58,7 +50,7 @@ fn delete(
.await
.map_err(vault_error_to_py_err)?;

cli::delete(&vault, key).await.map_err(anyhow_to_py_err)?;
cli::delete(&vault, key).await?;

Ok(())
})
Expand Down Expand Up @@ -99,9 +91,7 @@ fn decrypt(
.await
.map_err(vault_error_to_py_err)?;

cli::decrypt(&vault, value_positional, value_argument, file, outfile)
.await
.map_err(anyhow_to_py_err)?;
cli::decrypt(&vault, value_positional, value_argument, file, outfile).await?;
Ok(())
})
}
Expand All @@ -123,9 +113,7 @@ fn encrypt(
.await
.map_err(vault_error_to_py_err)?;

cli::encrypt(&vault, value_positional, value_argument, file, outfile)
.await
.map_err(anyhow_to_py_err)?;
cli::encrypt(&vault, value_positional, value_argument, file, outfile).await?;
Ok(())
})
}
Expand All @@ -145,9 +133,7 @@ fn exists(
.await
.map_err(vault_error_to_py_err)?;

let exists = cli::exists(&vault, key, quiet)
.await
.map_err(anyhow_to_py_err)?;
let exists = cli::exists(&vault, key, quiet).await?;
Ok(exists)
})
}
Expand All @@ -173,9 +159,7 @@ fn info(
#[pyfunction(signature = (region=None, quiet=false))]
fn id(region: Option<String>, quiet: bool) -> PyResult<()> {
Runtime::new()?.block_on(async {
cli::get_aws_account_id(region, quiet)
.await
.map_err(anyhow_to_py_err)?;
cli::get_aws_account_id(region, quiet).await?;

Ok(())
})
Expand All @@ -190,9 +174,7 @@ fn init(
quiet: bool,
) -> PyResult<()> {
Runtime::new()?.block_on(async {
cli::init_vault_stack(vault_stack.or(name), region, bucket, quiet)
.await
.map_err(anyhow_to_py_err)?;
cli::init_vault_stack(vault_stack.or(name), region, bucket, quiet).await?;

Ok(())
})
Expand All @@ -213,9 +195,7 @@ fn lookup(
.await
.map_err(vault_error_to_py_err)?;

cli::lookup(&vault, key, outfile)
.await
.map_err(anyhow_to_py_err)?;
cli::lookup(&vault, key, outfile).await?;

Ok(())
})
Expand Down Expand Up @@ -272,8 +252,7 @@ fn store(
overwrite,
quiet,
)
.await
.map_err(anyhow_to_py_err)?;
.await?;
Ok(())
})
}
Expand All @@ -293,9 +272,7 @@ fn update(
.await
.map_err(vault_error_to_py_err)?;

cli::update_vault_stack(&vault, quiet)
.await
.map_err(anyhow_to_py_err)?;
cli::update_vault_stack(&vault, quiet).await?;
Ok(())
})
}
Expand Down

0 comments on commit f056bce

Please sign in to comment.