refactor doc tests

This commit is contained in:
gengteng
2023-10-09 11:37:27 +08:00
parent 928578a840
commit 6cf325b19d
14 changed files with 1510 additions and 332 deletions

View File

@@ -12,42 +12,85 @@
//! ## Example
//!
//! ```no_run
//!#![cfg(feature = "validator")]
//! #[cfg(feature = "validator")]
//! mod validator_example {
//! use axum::routing::post;
//! use axum::Json;
//! use axum::Router;
//! use axum_msgpack::{MsgPack, MsgPackRaw};
//! use axum_valid::Valid;
//! use serde::Deserialize;
//! use validator::Validate;
//! #[tokio::main]
//! pub async fn launch() -> anyhow::Result<()> {
//! let router = Router::new()
//! .route("/msgpack", post(handler))
//! .route("/msgpackraw", post(raw_handler));
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
//! .serve(router.into_make_service())
//! .await?;
//! Ok(())
//! }
//! async fn handler(Valid(MsgPack(parameter)): Valid<MsgPack<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! }
//!
//! use axum::routing::post;
//! use axum::Router;
//! use axum_msgpack::{MsgPack, MsgPackRaw};
//! use axum_valid::Valid;
//! use serde::Deserialize;
//! use validator::Validate;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let router = Router::new()
//! .route("/msgpack", post(handler))
//! .route("/msgpackraw", post(raw_handler));
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
//! .serve(router.into_make_service())
//! .await?;
//! Ok(())
//! async fn raw_handler(Valid(MsgPackRaw(parameter)): Valid<MsgPackRaw<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! }
//! #[derive(Validate, Deserialize)]
//! pub struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! pub v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! pub v1: String,
//! }
//! }
//!
//! async fn handler(Valid(MsgPack(parameter)): Valid<MsgPack<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! #[cfg(feature = "garde")]
//! mod garde_example {
//! use axum::routing::post;
//! use axum::Router;
//! use axum_msgpack::{MsgPack, MsgPackRaw};
//! use axum_valid::Garde;
//! use serde::Deserialize;
//! use garde::Validate;
//! #[tokio::main]
//! pub async fn launch() -> anyhow::Result<()> {
//! let router = Router::new()
//! .route("/msgpack", post(handler))
//! .route("/msgpackraw", post(raw_handler));
//! axum::Server::bind(&([0u8, 0, 0, 0], 8080).into())
//! .serve(router.into_make_service())
//! .await?;
//! Ok(())
//! }
//!
//! async fn handler(Garde(MsgPack(parameter)): Garde<MsgPack<Parameter>>) {
//! assert!(parameter.validate(&()).is_ok());
//! }
//!
//! async fn raw_handler(Garde(MsgPackRaw(parameter)): Garde<MsgPackRaw<Parameter>>) {
//! assert!(parameter.validate(&()).is_ok());
//! }
//! #[derive(Validate, Deserialize)]
//! pub struct Parameter {
//! #[garde(range(min = 5, max = 10))]
//! pub v0: i32,
//! #[garde(length(min = 1, max = 10))]
//! pub v1: String,
//! }
//! }
//!
//! async fn raw_handler(Valid(MsgPackRaw(parameter)): Valid<MsgPackRaw<Parameter>>) {
//! assert!(parameter.validate().is_ok());
//! }
//!
//! #[derive(Validate, Deserialize)]
//! pub struct Parameter {
//! #[validate(range(min = 5, max = 10))]
//! pub v0: i32,
//! #[validate(length(min = 1, max = 10))]
//! pub v1: String,
//! }
//! # fn main() -> anyhow::Result<()> {
//! # #[cfg(feature = "validator")]
//! # validator_example::launch()?;
//! # #[cfg(feature = "garde")]
//! # garde_example::launch()?;
//! # Ok(())
//! # }
//! ```
//!
use crate::HasValidate;
#[cfg(feature = "validator")]