Macro portunusd::derive_server_procedure[][src]

macro_rules! derive_server_procedure {
    ($function_name:ident as $type_name:ident) => { ... };
}
Expand description

Define a function which can respond to DOOR_CALL(3C).

This macro turns a function into a type which implements the ServerProcedure trait. The function should accept a &[u8] and return a Vec<u8>, because the ServerProcedure trait will expect that signature.

Example

use portunusd::derive_server_procedure;
use std::fmt::format;
use std::str::from_utf8;

// Consider this function, which returns a polite greeting to a client:
fn hello(request: &[u8]) -> Vec<u8> {
    match from_utf8(request) {
        Err(_) => b"Your name is not valid utf8".to_vec(),
        Ok(name) => {
            let response = format!("Hello, {}!", name);
            response.into_bytes()
        }
    }
}

// We can use the `derive_server_procedure!` macro to create a
// `ServerProcedure` type called `Hello`:
derive_server_procedure!(hello as Hello);

// We can now create a filesystem object known as a "door" which
// will give PortunusD the ability to invoke the `hello` function
// (as long as "hello.door" is readable by the `portunus` user):
Hello::install("hello.door").unwrap();