paperclip_actix/
lib.rs

1#![cfg(any(feature = "actix2", feature = "actix3", feature = "actix4"))]
2#![allow(clippy::return_self_not_must_use)]
3
4#[cfg(feature = "actix2")]
5extern crate actix_web2 as actix_web;
6#[cfg(feature = "actix3")]
7extern crate actix_web3 as actix_web;
8#[cfg(feature = "actix4")]
9extern crate actix_web4 as actix_web;
10
11#[cfg(any(feature = "swagger-ui", feature = "rapidoc"))]
12use include_dir::{include_dir, Dir};
13
14#[cfg(feature = "actix4")]
15pub mod web;
16
17#[cfg(not(feature = "actix4"))]
18pub mod web3;
19#[cfg(not(feature = "actix4"))]
20pub use web3 as web;
21
22#[cfg(feature = "actix4")]
23pub mod app;
24
25#[cfg(not(feature = "actix4"))]
26pub mod app3;
27#[cfg(not(feature = "actix4"))]
28pub use app3 as app;
29
30pub use self::{
31    app::{App, OpenApiExt},
32    web::{Resource, Route, Scope},
33};
34pub use paperclip_macros::{
35    api_v2_errors, api_v2_errors_overlay, api_v2_operation, delete, get, head, patch, post, put,
36    Apiv2Header, Apiv2Schema, Apiv2Security,
37};
38
39use paperclip_core::v2::models::{
40    DefaultOperationRaw, DefaultPathItemRaw, DefaultSchemaRaw, HttpMethod, SecurityScheme,
41};
42
43use std::collections::BTreeMap;
44
45#[cfg(feature = "swagger-ui")]
46static SWAGGER_DIST: Dir = include_dir!("$CARGO_MANIFEST_DIR/swagger-ui/dist");
47#[cfg(feature = "rapidoc")]
48static RAPIDOC: Dir = include_dir!("$CARGO_MANIFEST_DIR/rapidoc");
49
50/// Indicates that this thingmabob has a path and a bunch of definitions and operations.
51pub trait Mountable {
52    /// Where this thing gets mounted.
53    fn path(&self) -> &str;
54
55    /// Map of HTTP methods and the associated API operations.
56    fn operations(&mut self) -> BTreeMap<HttpMethod, DefaultOperationRaw>;
57
58    /// The definitions recorded by this object.
59    fn definitions(&mut self) -> BTreeMap<String, DefaultSchemaRaw>;
60
61    /// The security definitions recorded by this object.
62    fn security_definitions(&mut self) -> BTreeMap<String, SecurityScheme>;
63
64    /// Updates the given map of operations with operations tracked by this object.
65    ///
66    /// **NOTE:** Overriding implementations must ensure that the `PathItem`
67    /// is normalized before updating the input map.
68    fn update_operations(&mut self, map: &mut BTreeMap<String, DefaultPathItemRaw>) {
69        let operations = self.operations();
70        if !operations.is_empty() {
71            let op_map = map.entry(self.path().into()).or_default();
72            op_map.methods.extend(operations);
73        }
74    }
75}