Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Serialize return value bound #11

Merged
merged 4 commits into from
Oct 27, 2017
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "MIT/Apache-2.0"
exclude = [".gitignore", "builder", "examples"]

[dependencies]
serde = "1.0"
serde_json = "1.0"
cpython = { version = "0.1", default-features = false }
cpython-json = { version = "0.2", default-features = false }
Expand Down
34 changes: 18 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,21 @@
extern crate cpython;
extern crate cpython_json;
extern crate serde_json;
extern crate serde;

#[doc(hidden)]
pub use cpython::{PyResult, PyObject};
pub use serde_json::value::Value;

/// Result object that accepts `Ok(Value)` or any `Err(Error)`.
/// Result object that accepts `Ok(T)` or any `Err(Error)`.
///
/// crowbar uses [the `Box<Error>` method of error handling]
/// (https://doc.rust-lang.org/stable/book/error-handling.html#error-handling-with-boxerror) so
/// that any `Error` can be thrown within your Lambda function.
///
/// If an error is thrown, it is converted to a Python `RuntimeError`, and the `Debug` string for
/// the `Error` returned is used as the value.
pub type LambdaResult = Result<Value, Box<std::error::Error>>;
pub type LambdaResult<T = Value> = Result<T, Box<std::error::Error>>;

use cpython::{Python, PyUnicode, PyTuple, PyErr, PythonObject, PythonObjectWithTypeObject,
ObjectProtocol};
Expand Down Expand Up @@ -217,21 +218,22 @@ impl std::error::Error for ContextError {
}

#[doc(hidden)]
pub fn handler<F>(py: Python, f: F, py_event: PyObject, py_context: PyObject) -> PyResult<PyObject>
where F: Fn(Value, LambdaContext) -> LambdaResult
pub fn handler<F, O>(py: Python, f: F, py_event: PyObject, py_context: PyObject) -> PyResult<PyObject>
where F: FnOnce(Value, LambdaContext) -> LambdaResult<O>,
O: serde::Serialize
{
let event = to_json(py, &py_event).or_else(|e| Err(e.to_pyerr(py)))?;
let result = match f(event, LambdaContext::new(&py, &py_context)?) {
Ok(r) => r,
Err(e) => {
return Err(PyErr {
ptype: cpython::exc::RuntimeError::type_object(py).into_object(),
pvalue: Some(PyUnicode::new(py, &format!("{:?}", e)).into_object()),
ptraceback: None,
})
}
};
from_json(py, result).or_else(|e| Err(e.to_pyerr(py)))
let event = to_json(py, &py_event).map_err(|e| e.to_pyerr(py))?;
f(event, LambdaContext::new(&py, &py_context)?)
.map_err(|e| PyErr {
ptype: cpython::exc::RuntimeError::type_object(py).into_object(),
pvalue: Some(PyUnicode::new(py, &format!("{:?}", e)).into_object()),
ptraceback: None,
}).and_then(|v| serde_json::value::to_value(v)
.map_err(cpython_json::JsonError::SerdeJsonError)
.map_err(|e| e.to_pyerr(py))
).and_then(|v| from_json(py, v)
.map_err(|e| e.to_pyerr(py))
)
}

#[macro_export]
Expand Down