Skip to main content

apimock_server/
middleware.rs

1//! Compiled Rhai middlewares.
2//!
3//! # Why this lives here and not in `apimock-config`
4//!
5//! A `MiddlewareHandler` owns a compiled Rhai `AST` and returns
6//! `hyper::Response` values when it handles a request. Both of those
7//! are runtime concerns that must not leak into the config crate (the
8//! config crate must remain serde/TOML-centric so stage-2 GUI editing
9//! can reason about it without linking against a scripting engine).
10
11use std::path::Path;
12
13use crate::error::{ServerError, ServerResult};
14
15pub mod middleware_handler;
16mod middleware_response;
17
18pub use middleware_handler::MiddlewareHandler;
19
20/// An ordered list of compiled middleware handlers.
21///
22/// Compilation happens once at server startup (see [`compile`]) so the
23/// per-request path is free of filesystem reads and Rhai parsing cost.
24#[derive(Clone, Default)]
25pub struct LoadedMiddlewares {
26    handlers: Vec<MiddlewareHandler>,
27}
28
29impl LoadedMiddlewares {
30    /// Compile every Rhai source file listed in `middleware_file_paths`.
31    ///
32    /// Paths are interpreted relative to `relative_dir_path` — the same
33    /// convention used by `Config::new` for rule-set paths.
34    pub fn compile(
35        middleware_file_paths: &[String],
36        relative_dir_path: &str,
37    ) -> ServerResult<Self> {
38        let mut handlers = Vec::with_capacity(middleware_file_paths.len());
39        for (idx, relative_path) in middleware_file_paths.iter().enumerate() {
40            let joined = Path::new(relative_dir_path).join(relative_path);
41            let path_str = joined.to_str().ok_or_else(|| ServerError::Io(
42                std::io::Error::new(
43                    std::io::ErrorKind::InvalidData,
44                    format!(
45                        "middleware #{} path contains non-UTF-8 bytes: {}",
46                        idx + 1,
47                        joined.to_string_lossy(),
48                    ),
49                ),
50            ))?;
51            handlers.push(MiddlewareHandler::new(path_str)?);
52        }
53        Ok(Self { handlers })
54    }
55
56    pub fn len(&self) -> usize {
57        self.handlers.len()
58    }
59
60    pub fn is_empty(&self) -> bool {
61        self.handlers.is_empty()
62    }
63
64    pub fn iter(&self) -> std::slice::Iter<'_, MiddlewareHandler> {
65        self.handlers.iter()
66    }
67}