Skip to content

Commit cdbb053

Browse files
committed
rust examples
1 parent b72d45b commit cdbb053

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

rust_stuff/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "rust_stuff"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
clap = { version = "4.2.4", features = ["derive"] }
8+
openapi_type = "0.4.2"
9+
serde = "1.0.160"

rust_stuff/org/org_ai_rust.org

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
:PROPERTIES:
2+
:ID: 459e155c-6486-4aee-ab4a-e99faccacb1c
3+
:END:
4+
#+title: org-ai/rust
5+
6+
* Backend/axum
7+
8+
#+BEGIN_AI
9+
How can I add Swagger to axum in Rust?
10+
11+
[AI]: Currently, there is no dedicated library or plug-and-play solution for integrating Swagger (OpenAPI) with Axum in Rust. However, you can manually implement the OpenAPI specification (former Swagger) for your Axum application by following these steps:
12+
13+
1. Create an OpenAPI Specification file:
14+
Create a YAML or JSON file describing your Axum REST API according to the OpenAPI specification. You can use [Swagger editor](https://editor.swagger.io/) to help create and validate your specification. Save the resulting file (e.g., `openapi.yaml` or `openapi.json`).
15+
16+
2. Serve the OpenAPI Specification file:
17+
Include the OpenAPI specification file in#
18+
19+
[Me]: Ok, how can I write types that will be serialized to JSON that will be returned from axum endpoints and export them to OpenAPI?
20+
21+
[AI]: There's no direct solution to generate the OpenAPI specification from your types in Axum automatically, but you can manually create the OpenAPI document to describe your types and define the responses for your Axum endpoints. To serialize the types as JSON and serve through Axum, follow these steps:
22+
23+
1. Add the required dependencies to your `Cargo.toml`#+END_AI
24+
25+
#+BEGIN_AI
26+
How can I define types in Rust axum that would describe endpoint returns and can be exported to OpenAPI specification?
27+
28+
[AI]: To define types in Rust that describe the endpoint returns in Axum and can be manually added to an OpenAPI specification, follow these steps:
29+
30+
1. Add the dependencies to your `Cargo.toml`:
31+
32+
```toml
33+
[dependencies]
34+
axum = "0.3"
35+
serde = { version = "1.0", features = ["derive"] }
36+
serde_json = "1.0"
37+
tokio = { version = "1", features = ["full"] }
38+
```
39+
40+
2. Define your types using structs and derive `serde::Serialize` for serialization:
41+
42+
```rust
43+
use serde::Serialize;
44+
45+
#[derive(Serialize)]
46+
pub struct User {
47+
pub id: u32,
48+
pub name: String,
49+
pub email: String,
50+
}
51+
52+
#[derive(Serialize)]
53+
pub struct ErrorResponse {
54+
pub error: String,
55+
}
56+
```
57+
58+
3. Create endpoints that return your types:
59+
60+
```rust
61+
use axum::{handler::get, Router};
62+
use http::StatusCode;
63+
us serd_json::js;
64+
65+
async fn get_use() -> Resu<imp IntoResponse, Infallible> {
66+
letlltr user = User {
67+
id,
68+
nam "Alice".e:t_strin() emai [email protected]".to_string":l,
69+
go :1 onee#+END_AI

rust_stuff/src/main.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use clap;
2+
use clap::Parser;
3+
use std::str::FromStr;
4+
mod openapi_example;
5+
6+
#[derive(Debug, PartialEq, Clone)]
7+
enum Example {
8+
OAPI,
9+
}
10+
11+
impl FromStr for Example {
12+
type Err = String;
13+
14+
fn from_str(s: &str) -> Result<Self, Self::Err> {
15+
match s.to_lowercase().as_str() {
16+
"oapi" => Ok(Example::OAPI),
17+
_ => Err(format!("unknown example: {}", s)),
18+
}
19+
}
20+
}
21+
22+
#[derive(Parser, Debug)]
23+
struct MainOpt {
24+
/// set the listen addr
25+
#[clap(short = 'n', long = "name", default_value = "oapi")]
26+
example_name: Example,
27+
}
28+
29+
fn main() {
30+
let opt = MainOpt::parse();
31+
match opt.example_name {
32+
Example::OAPI => openapi_example::main(),
33+
}
34+
}

rust_stuff/src/openapi_example.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
extern crate openapi_type;
2+
3+
use openapi_type::OpenapiType;
4+
5+
#[derive(OpenapiType)]
6+
struct FooBar {
7+
foo: String,
8+
bar: u64,
9+
}
10+
11+
pub fn main() {
12+
println!("{:#?}", FooBar::schema());
13+
}

0 commit comments

Comments
 (0)