axum_codec/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![forbid(unsafe_code)]
#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
#![cfg_attr(
	not(any(
		feature = "json",
		feature = "msgpack",
		feature = "bincode",
		feature = "bitcode",
		feature = "cbor",
		feature = "yaml",
		feature = "toml"
	)),
	allow(unreachable_code, unused_variables)
)]
#![doc = include_str!("../README.md")]

mod content;
mod decode;
mod encode;
pub mod extract;
pub mod handler;
pub mod rejection;
pub mod response;
pub mod routing;

pub use content::{Accept, ContentType};
pub use decode::CodecDecode;
pub use encode::CodecEncode;
pub use extract::Codec;
pub use handler::CodecHandler;
pub use rejection::CodecRejection;
pub use response::IntoCodecResponse;

#[doc(hidden)]
pub mod __private {
	#[cfg(feature = "bincode")]
	pub use bincode;
	#[cfg(feature = "bitcode")]
	pub use bitcode;
	#[cfg(feature = "aide")]
	pub use schemars;
	#[cfg(feature = "serde")]
	pub use serde;
	#[cfg(feature = "validator")]
	pub use validator;
}

pub use axum_codec_macros as macros;
#[cfg(feature = "macros")]
pub use macros::apply;
#[cfg(feature = "macros")]
pub use macros::debug_handler;
#[cfg(feature = "macros")]
pub use macros::debug_middleware;

#[cfg(test)]
mod test {
	use super::*;

	#[apply(decode, encode)]
	#[derive(Debug, PartialEq)]
	struct Data {
		string: String,
		integer: i32,
		array: Vec<i32>,
		boolean: bool,
	}

	fn data() -> Data {
		Data {
			string: "hello".into(),
			integer: 42,
			array: vec![1, 2, 3],
			boolean: true,
		}
	}

	#[test]
	fn test_msgpack_roundtrip() {
		let data = data();
		let encoded = Codec(&data).to_msgpack().unwrap();

		let Codec(decoded) = Codec::<Data>::from_msgpack(&encoded).unwrap();

		assert_eq!(decoded, data);
	}

	#[test]
	fn test_json_roundtrip() {
		let data = data();
		let encoded = Codec(&data).to_json().unwrap();

		let Codec(decoded) = Codec::<Data>::from_json(&encoded).unwrap();

		assert_eq!(decoded, data);
	}

	#[test]
	fn test_cbor_roundtrip() {
		let data = data();
		let encoded = Codec(&data).to_cbor().unwrap();

		let Codec(decoded) = Codec::<Data>::from_cbor(&encoded).unwrap();

		assert_eq!(decoded, data);
	}

	#[test]
	fn test_yaml_roundtrip() {
		let data = data();
		let encoded = Codec(&data).to_yaml().unwrap();

		let Codec(decoded) = Codec::<Data>::from_yaml(&encoded).unwrap();

		assert_eq!(decoded, data);
	}

	#[test]
	fn test_toml_roundtrip() {
		let data = data();
		let encoded = Codec(&data).to_toml().unwrap();

		let Codec(decoded) = Codec::<Data>::from_toml(&encoded).unwrap();

		assert_eq!(decoded, data);
	}

	#[test]
	fn test_bincode_roundtrip() {
		let data = data();
		let encoded = Codec(&data).to_bincode().unwrap();

		let Codec(decoded) = Codec::<Data>::from_bincode(&encoded).unwrap();

		assert_eq!(decoded, data);
	}

	#[test]
	fn test_bitcode_roundtrip() {
		let encoded = Codec(data()).to_bitcode();

		let Codec(decoded) = Codec::<Data>::from_bitcode(&encoded).unwrap();

		assert_eq!(decoded, data());
	}
}