From 6729f6f5431d8b937897705205a2a6d314464eea Mon Sep 17 00:00:00 2001 From: Ivan Leo Date: Mon, 8 Jul 2024 20:00:38 +0800 Subject: [PATCH] Added example for Option --- docs/types.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/docs/types.md b/docs/types.md index 595bed8..d97277d 100644 --- a/docs/types.md +++ b/docs/types.md @@ -207,3 +207,55 @@ struct Numbers { } */ ``` + +### Options + +We also support Option types. This is most popular when using a `Maybe` pattern where we have some form of data that we might want to extract. + +```rust +#[derive(InstructMacro, Debug)] +#[allow(dead_code)] +#[description("This is a user struct")] +struct User { + #[description("This is the user's name")] + pub name: String, + #[description("This is the user's age")] + pub age: i32, +} + +#[derive(InstructMacro, Debug)] +#[allow(dead_code)] +#[description("This is a struct with Option type")] +struct MaybeUser { + #[description("This is an optional user field")] + pub user: Option, + error_message: Option +} + +/* +{ + "type": "object", + "properties": { + "user": { + "type": "object", + "description": "This is an optional user field. If the user is not present, the field will be null", + "properties": { + "age": { + "type": "number", + "description": "" + }, + "name": { + "type": "string", + "description": "" + } + } + }, + "error_message": { + "type": "string", + "description": "" + } + }, + "required": [] +} +*/ +```