Skip to main content

rustapi_rs/
lib.rs

1//! # RustAPI
2//!
3//! Public facade crate for RustAPI.
4
5extern crate self as rustapi_rs;
6
7// Re-export all procedural macros from rustapi-macros.
8pub use rustapi_macros::*;
9
10/// Macro/runtime internals. Not part of the public compatibility contract.
11#[doc(hidden)]
12pub mod __private {
13    pub use async_trait;
14    pub use rustapi_core as core;
15    pub use rustapi_core::__private::{linkme, AUTO_ROUTES, AUTO_SCHEMAS};
16    pub use rustapi_openapi as openapi;
17    pub use rustapi_validate as validate;
18    pub use serde_json;
19}
20
21/// Stable core surface exposed by the facade.
22pub mod core {
23    pub use rustapi_core::collect_auto_routes;
24    pub use rustapi_core::validation::Validatable;
25    pub use rustapi_core::{
26        delete, delete_route, get, get_route, patch, patch_route, post, post_route, put, put_route,
27        route, serve_dir, sse_response, ApiError, AsyncValidatedJson, Body, BodyLimitLayer,
28        BodyStream, BodyVariant, ClientIp, Created, Environment, Extension, FieldError,
29        FromRequest, FromRequestParts, Handler, HandlerService, HeaderValue, Headers, Html,
30        IntoResponse, Json, KeepAlive, MethodRouter, Multipart, MultipartConfig, MultipartField,
31        NoContent, Path, Query, Redirect, Request, RequestId, RequestIdLayer, Response,
32        ResponseBody, Result, Route, RouteHandler, RouteMatch, Router, RustApi, RustApiConfig, Sse,
33        SseEvent, State, StaticFile, StaticFileConfig, StatusCode, StreamBody, TracingLayer, Typed,
34        TypedPath, UploadedFile, ValidatedJson, WithStatus,
35    };
36
37    pub use rustapi_core::get_environment;
38
39    #[cfg(any(feature = "core-cookies", feature = "cookies"))]
40    pub use rustapi_core::Cookies;
41
42    #[cfg(any(feature = "core-compression", feature = "compression"))]
43    pub use rustapi_core::CompressionLayer;
44
45    #[cfg(any(feature = "core-compression", feature = "compression"))]
46    pub use rustapi_core::middleware::{CompressionAlgorithm, CompressionConfig};
47
48    #[cfg(any(feature = "core-http3", feature = "protocol-http3", feature = "http3"))]
49    pub use rustapi_core::{Http3Config, Http3Server};
50}
51
52// Backward-compatible root re-exports.
53pub use core::*;
54
55/// Optional protocol integrations grouped under a stable namespace.
56pub mod protocol {
57    #[cfg(any(feature = "protocol-toon", feature = "toon"))]
58    pub mod toon {
59        pub use rustapi_toon::*;
60    }
61
62    #[cfg(any(feature = "protocol-ws", feature = "ws"))]
63    pub mod ws {
64        pub use rustapi_ws::*;
65    }
66
67    #[cfg(any(feature = "protocol-view", feature = "view"))]
68    pub mod view {
69        pub use rustapi_view::*;
70    }
71
72    #[cfg(any(feature = "protocol-grpc", feature = "grpc"))]
73    pub mod grpc {
74        pub use rustapi_grpc::*;
75    }
76
77    #[cfg(any(feature = "core-http3", feature = "protocol-http3", feature = "http3"))]
78    pub mod http3 {
79        pub use rustapi_core::{Http3Config, Http3Server};
80    }
81}
82
83/// Optional extras grouped under a stable namespace.
84pub mod extras {
85    #[cfg(any(feature = "extras-jwt", feature = "jwt"))]
86    pub mod jwt {
87        pub use rustapi_extras::jwt;
88        pub use rustapi_extras::{
89            create_token, AuthUser, JwtError, JwtLayer, JwtValidation, ValidatedClaims,
90        };
91    }
92
93    #[cfg(any(feature = "extras-cors", feature = "cors"))]
94    pub mod cors {
95        pub use rustapi_extras::cors;
96        pub use rustapi_extras::{AllowedOrigins, CorsLayer};
97    }
98
99    #[cfg(any(feature = "extras-rate-limit", feature = "rate-limit"))]
100    pub mod rate_limit {
101        pub use rustapi_extras::rate_limit;
102        pub use rustapi_extras::RateLimitLayer;
103    }
104
105    #[cfg(any(feature = "extras-config", feature = "config"))]
106    pub mod config {
107        pub use rustapi_extras::config;
108        pub use rustapi_extras::{
109            env_or, env_parse, load_dotenv, load_dotenv_from, require_env, Config, ConfigError,
110            Environment,
111        };
112    }
113
114    #[cfg(any(feature = "extras-sqlx", feature = "sqlx"))]
115    pub mod sqlx {
116        pub use rustapi_extras::{convert_sqlx_error, SqlxErrorExt};
117    }
118
119    #[cfg(any(feature = "extras-insight", feature = "insight"))]
120    pub mod insight {
121        pub use rustapi_extras::insight;
122    }
123
124    #[cfg(any(feature = "extras-timeout", feature = "timeout"))]
125    pub mod timeout {
126        pub use rustapi_extras::timeout;
127    }
128
129    #[cfg(any(feature = "extras-guard", feature = "guard"))]
130    pub mod guard {
131        pub use rustapi_extras::guard;
132    }
133
134    #[cfg(any(feature = "extras-logging", feature = "logging"))]
135    pub mod logging {
136        pub use rustapi_extras::logging;
137    }
138
139    #[cfg(any(feature = "extras-circuit-breaker", feature = "circuit-breaker"))]
140    pub mod circuit_breaker {
141        pub use rustapi_extras::circuit_breaker;
142    }
143
144    #[cfg(any(feature = "extras-retry", feature = "retry"))]
145    pub mod retry {
146        pub use rustapi_extras::retry;
147    }
148
149    #[cfg(any(feature = "extras-security-headers", feature = "security-headers"))]
150    pub mod security_headers {
151        pub use rustapi_extras::security_headers;
152    }
153
154    #[cfg(any(feature = "extras-api-key", feature = "api-key"))]
155    pub mod api_key {
156        pub use rustapi_extras::api_key;
157    }
158
159    #[cfg(any(feature = "extras-cache", feature = "cache"))]
160    pub mod cache {
161        pub use rustapi_extras::cache;
162    }
163
164    #[cfg(any(feature = "extras-dedup", feature = "dedup"))]
165    pub mod dedup {
166        pub use rustapi_extras::dedup;
167    }
168
169    #[cfg(any(feature = "extras-sanitization", feature = "sanitization"))]
170    pub mod sanitization {
171        pub use rustapi_extras::sanitization;
172    }
173
174    #[cfg(any(feature = "extras-otel", feature = "otel"))]
175    pub mod otel {
176        pub use rustapi_extras::otel;
177    }
178
179    #[cfg(any(feature = "extras-structured-logging", feature = "structured-logging"))]
180    pub mod structured_logging {
181        pub use rustapi_extras::structured_logging;
182    }
183
184    #[cfg(any(feature = "extras-replay", feature = "replay"))]
185    pub mod replay {
186        pub use rustapi_extras::replay;
187    }
188}
189
190// Legacy root module aliases.
191#[cfg(any(feature = "protocol-toon", feature = "toon"))]
192#[deprecated(note = "Use rustapi_rs::protocol::toon instead")]
193pub mod toon {
194    pub use crate::protocol::toon::*;
195}
196
197#[cfg(any(feature = "protocol-ws", feature = "ws"))]
198#[deprecated(note = "Use rustapi_rs::protocol::ws instead")]
199pub mod ws {
200    pub use crate::protocol::ws::*;
201}
202
203#[cfg(any(feature = "protocol-view", feature = "view"))]
204#[deprecated(note = "Use rustapi_rs::protocol::view instead")]
205pub mod view {
206    pub use crate::protocol::view::*;
207}
208
209#[cfg(any(feature = "protocol-grpc", feature = "grpc"))]
210#[deprecated(note = "Use rustapi_rs::protocol::grpc instead")]
211pub mod grpc {
212    pub use crate::protocol::grpc::*;
213}
214
215// Legacy root extras re-exports for compatibility.
216#[cfg(any(feature = "extras-jwt", feature = "jwt"))]
217pub use rustapi_extras::jwt;
218#[cfg(any(feature = "extras-jwt", feature = "jwt"))]
219pub use rustapi_extras::{
220    create_token, AuthUser, JwtError, JwtLayer, JwtValidation, ValidatedClaims,
221};
222
223#[cfg(any(feature = "extras-cors", feature = "cors"))]
224pub use rustapi_extras::cors;
225#[cfg(any(feature = "extras-cors", feature = "cors"))]
226pub use rustapi_extras::{AllowedOrigins, CorsLayer};
227
228#[cfg(any(feature = "extras-rate-limit", feature = "rate-limit"))]
229pub use rustapi_extras::rate_limit;
230#[cfg(any(feature = "extras-rate-limit", feature = "rate-limit"))]
231pub use rustapi_extras::RateLimitLayer;
232
233#[cfg(any(feature = "extras-config", feature = "config"))]
234pub use rustapi_extras::config;
235#[cfg(any(feature = "extras-config", feature = "config"))]
236pub use rustapi_extras::{
237    env_or, env_parse, load_dotenv, load_dotenv_from, require_env, Config, ConfigError,
238    Environment as ExtrasEnvironment,
239};
240
241#[cfg(any(feature = "extras-sqlx", feature = "sqlx"))]
242pub use rustapi_extras::{convert_sqlx_error, SqlxErrorExt};
243
244#[cfg(any(feature = "extras-api-key", feature = "api-key"))]
245pub use rustapi_extras::api_key;
246#[cfg(any(feature = "extras-cache", feature = "cache"))]
247pub use rustapi_extras::cache;
248#[cfg(any(feature = "extras-circuit-breaker", feature = "circuit-breaker"))]
249pub use rustapi_extras::circuit_breaker;
250#[cfg(any(feature = "extras-dedup", feature = "dedup"))]
251pub use rustapi_extras::dedup;
252#[cfg(any(feature = "extras-guard", feature = "guard"))]
253pub use rustapi_extras::guard;
254#[cfg(any(feature = "extras-logging", feature = "logging"))]
255pub use rustapi_extras::logging;
256#[cfg(any(feature = "extras-otel", feature = "otel"))]
257pub use rustapi_extras::otel;
258#[cfg(any(feature = "extras-replay", feature = "replay"))]
259pub use rustapi_extras::replay;
260#[cfg(any(feature = "extras-retry", feature = "retry"))]
261pub use rustapi_extras::retry;
262#[cfg(any(feature = "extras-sanitization", feature = "sanitization"))]
263pub use rustapi_extras::sanitization;
264#[cfg(any(feature = "extras-security-headers", feature = "security-headers"))]
265pub use rustapi_extras::security_headers;
266#[cfg(any(feature = "extras-structured-logging", feature = "structured-logging"))]
267pub use rustapi_extras::structured_logging;
268#[cfg(any(feature = "extras-timeout", feature = "timeout"))]
269pub use rustapi_extras::timeout;
270
271/// Prelude module: `use rustapi_rs::prelude::*`.
272pub mod prelude {
273    pub use crate::core::Validatable;
274    pub use crate::core::{
275        delete, delete_route, get, get_route, patch, patch_route, post, post_route, put, put_route,
276        route, serve_dir, sse_response, ApiError, AsyncValidatedJson, Body, BodyLimitLayer,
277        ClientIp, Created, Extension, HeaderValue, Headers, Html, IntoResponse, Json, KeepAlive,
278        Multipart, MultipartConfig, MultipartField, NoContent, Path, Query, Redirect, Request,
279        RequestId, RequestIdLayer, Response, Result, Route, Router, RustApi, RustApiConfig, Sse,
280        SseEvent, State, StaticFile, StaticFileConfig, StatusCode, StreamBody, TracingLayer, Typed,
281        TypedPath, UploadedFile, ValidatedJson, WithStatus,
282    };
283
284    #[cfg(any(feature = "core-compression", feature = "compression"))]
285    pub use crate::core::{CompressionAlgorithm, CompressionConfig, CompressionLayer};
286
287    #[cfg(any(feature = "core-cookies", feature = "cookies"))]
288    pub use crate::core::Cookies;
289
290    pub use rustapi_macros::ApiError;
291    pub use rustapi_macros::Schema;
292    pub use rustapi_macros::TypedPath;
293
294    pub use rustapi_validate::v2::AsyncValidate;
295    pub use rustapi_validate::v2::Validate as V2Validate;
296
297    #[cfg(any(feature = "core-legacy-validator", feature = "legacy-validator"))]
298    pub use validator::Validate;
299
300    pub use serde::{Deserialize, Serialize};
301    pub use tracing::{debug, error, info, trace, warn};
302
303    #[cfg(any(feature = "extras-jwt", feature = "jwt"))]
304    pub use crate::{create_token, AuthUser, JwtError, JwtLayer, JwtValidation, ValidatedClaims};
305
306    #[cfg(any(feature = "extras-cors", feature = "cors"))]
307    pub use crate::{AllowedOrigins, CorsLayer};
308
309    #[cfg(any(feature = "extras-rate-limit", feature = "rate-limit"))]
310    pub use crate::RateLimitLayer;
311
312    #[cfg(any(feature = "extras-config", feature = "config"))]
313    pub use crate::{
314        env_or, env_parse, load_dotenv, load_dotenv_from, require_env, Config, ConfigError,
315        ExtrasEnvironment,
316    };
317
318    #[cfg(any(feature = "extras-sqlx", feature = "sqlx"))]
319    pub use crate::{convert_sqlx_error, SqlxErrorExt};
320
321    #[cfg(any(feature = "protocol-toon", feature = "toon"))]
322    pub use crate::protocol::toon::{AcceptHeader, LlmResponse, Negotiate, OutputFormat, Toon};
323
324    #[cfg(any(feature = "protocol-ws", feature = "ws"))]
325    pub use crate::protocol::ws::{Broadcast, Message, WebSocket, WebSocketStream};
326
327    #[cfg(any(feature = "protocol-view", feature = "view"))]
328    pub use crate::protocol::view::{ContextBuilder, Templates, TemplatesConfig, View};
329
330    #[cfg(any(feature = "protocol-grpc", feature = "grpc"))]
331    pub use crate::protocol::grpc::{
332        run_concurrently, run_rustapi_and_grpc, run_rustapi_and_grpc_with_shutdown,
333    };
334}
335
336#[cfg(test)]
337mod tests {
338    use super::prelude::*;
339
340    #[test]
341    fn prelude_imports_work() {
342        let _: fn() -> Result<()> = || Ok(());
343    }
344}