Skip to main content

server_task_enum

Attribute Macro server_task_enum 

Source
#[server_task_enum]
Expand description

§#[server_task_enum]

The #[server_task_enum] macro streamlines the creation of server-side task enums. When applied to an enum, it automatically implements necessary traits for processing RPC requests, reducing boilerplate and improving code maintainability.

§Macro Arguments:

The server_task_enum can accept either or both of “req” and “resp” flags, with the following rules:

  • If req is specified, the enum is request task, the macro will impl ServerTaskDecode, ServerTaskAction. each variant must have an #[action(...)] attribute.
  • If “resp” is specified, the enum is response task. The macro will impl ServerTaskEncode, ServerTaskDone
  • If both req and resp is specified, the response type for ServerTaskDecode<R> and ServerTaskDone<R> is implicitly Self (the enum itself). resp_type can be omitted.
  • If only “req” is specified (and “resp” is not), then resp_type must be provided. This resp_type specifies the type <R> for parameters of ServerTaskDecode<R> and ServerTaskDone<R>.

server_task_enum also requires error flag, specified the custom error type for RpcError<E: RpcErrCodec>

§Variant Attributes:

  • #[action(...)]: Associates one or more RPC action (which can be numeric, string, or enum value) with an enum variant. Multiple actions can be specified (e.g., #[action(1, 2 )]). If there’s more than one actions. The subtype should store its action and implement ServerTaskAction.

§Example:

use razor_stream::server::task::ServerTaskVariant;
use razor_stream_macros::server_task_enum;
use serde_derive::{Deserialize, Serialize};
use nix::errno::Errno;

#[derive(PartialEq)]
#[repr(u8)]
enum FileAction {
    Open=1,
    Read=2,
    Write=3,
    Truncate=4,
}

#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
struct FileOpenReq { pub path: String, }

#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
struct FileIOReq { pub path: String, pub offset: u64, pub len: u64, }


#[server_task_enum(req, resp, error=Errno)]
#[derive(Debug)]
pub enum FileTask {
    #[action(FileAction::Open)]
    Open(ServerTaskVariant<FileTask, FileOpenReq, Errno>),
    #[action(FileAction::Read, FileAction::Write, FileAction::Truncate)]
    Read(ServerTaskVariant<FileTask, FileIOReq, Errno>),
}