Struct fdk::Function [] [src]

pub struct Function<S: Sized> { /* fields omitted */ }

A function runtime which wraps a simpler function written by the user and deals with all the complexity of the Fn platform contract while providing an easy to use API.

Methods

impl<S: Sized> Function<S>
[src]

[src]

Create a new Function with an initializer that is basically a factory for the function's state. The initializer takes a RuntimeContext from which configuration data can be extracted.

struct MyState {
    greeting: String
}

let func = fdk::Function::new(|context| Ok(MyState {
        greeting: context.config().get("GREETING")
            .unwrap_or(&"Hello!\n".to_string()).clone()
    }));

[src]

Runs the function runtime and processes any request to pass it down to the code provided. When the execution finishes, returns the exit code the program should exit with.

The code provided must take the function state as the first parameter and the function input as the second. It must then return a result where Ok provides the function output and the error type is a FunctionError.

struct MyState {
    greeting: String
}

let function = fdk::Function::new(|_| Ok(MyState {
    greeting: "Hello".to_string()
}));
let exit_code = function.run(|state, i: String| {
    Ok(format!("{}, {}!", state.greeting, i).to_string())
});

If the function was initialized with the STATELESS constant, the state parameter can be ignored (it is the empty type anyway).

let exit_code = fdk::Function::new(fdk::STATELESS).run(|_, i: String| {
    Ok(format!("Hello, {}!", i).to_string())
});

Most types can be coerced from Requests and to Responses by the runtime without need for explicit conversions. If a type is not already convertible, implement the InputCoercible or OutputCoercible trait for the type.

Input and output coercions are performed so that the code does not need to handle Requests or Responses directly, but it is possible to do so in cases where more control is needed.

extern crate fdk;
extern crate hyper;
// ...
let exit_code = fdk::Function::new(fdk::STATELESS).run(|_, r: hyper::Request| {
    Ok(hyper::Response::new().with_body(r.body()))
});