Skip to main content

hyperlane_plugin_rewrite/
enum.rs

1use super::*;
2
3/// Supported HTTP redirect status codes for short-circuit rewrite rules.
4#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
5pub enum RedirectStatus {
6    /// `301 Moved Permanently`
7    MovedPermanently301,
8    /// `302 Found`
9    Found302,
10    /// `307 Temporary Redirect` (preserves HTTP method and body).
11    Temporary307,
12    /// `308 Permanent Redirect` (preserves HTTP method and body).
13    #[default]
14    Permanent308,
15}
16
17/// Action taken when a rewrite rule matches an incoming request path.
18#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
19pub enum RewriteAction {
20    /// Mutates `Request::set_path` internally and continues routing.
21    #[default]
22    InternalRewrite,
23    /// Short-circuits the pipeline with an HTTP redirect and `Location` header.
24    Redirect(RedirectStatus),
25}
26
27/// Trailing-slash normalization policy applied to non-root paths (`path != "/"`).
28#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
29pub enum TrailingSlashMode {
30    /// Leave trailing slashes untouched.
31    #[default]
32    Keep,
33    /// Remove trailing slashes (`/docs/` -> `/docs`).
34    Strip,
35    /// Ensure a trailing slash (`/docs` -> `/docs/`).
36    Enforce,
37}
38
39/// Outcome returned by [`CompiledRewrite::apply`] when evaluating a request context.
40#[derive(Clone, Debug, Eq, PartialEq)]
41pub enum RewriteOutcome {
42    /// No canonical redirect or rewrite rule matched (zero-allocation fast path).
43    Unchanged,
44    /// Request path was internally rewritten via `Request::set_path`.
45    Rewritten(String),
46    /// Response was populated with a redirect status and `Location` header ready to send.
47    Redirected {
48        /// HTTP redirect status code (`301`/`302`/`307`/`308`).
49        status_code: usize,
50        /// Target `Location` header value.
51        location: String,
52    },
53}
54
55/// Errors produced when compiling [`RewriteConfig`] into [`CompiledRewrite`].
56#[derive(Clone, Debug, Eq, PartialEq)]
57pub enum RewriteError {
58    /// A configured regex pattern failed to compile.
59    InvalidRegex {
60        /// Source pattern string.
61        pattern: String,
62        /// Underlying regex parser diagnostic.
63        message: String,
64    },
65    /// A configured canonical host or target contained forbidden CR/LF control characters.
66    UnsafeLocation(String),
67}
68
69/// Character class validated by a regex-free single-capture matcher.
70#[derive(Clone, Copy, Debug, Eq, PartialEq)]
71pub(crate) enum CaptureKind {
72    /// Matches zero or more characters without newlines (`.*`).
73    Any,
74    /// Matches one or more characters without newlines (`.+`).
75    NonEmpty,
76    /// Matches one or more non-slash characters (`[^/]+`).
77    NonSlash,
78    /// Matches one or more ASCII decimal digits (`[0-9]+`).
79    Digits,
80}
81
82/// Compiled matcher for a regex rule, selecting regex-free fast paths when safe.
83#[derive(Clone, Debug)]
84pub(crate) enum CompiledRegexMatcher {
85    /// Exact anchored literal (`^literal$`) with static replacement containing no `$`.
86    ExactLiteral {
87        /// Exact path string to match.
88        needle: String,
89        /// Static replacement string.
90        replacement: String,
91    },
92    /// Anchored `^prefix(<cap>)suffix$` with a single `$1` or `${1}` capture substitution.
93    PrefixSuffixCapture {
94        /// Literal prefix before the capture group.
95        prefix: String,
96        /// Literal suffix after the capture group.
97        suffix: String,
98        /// Capture validation rule.
99        kind: CaptureKind,
100        /// Literal replacement text before `$1`.
101        repl_prefix: String,
102        /// Literal replacement text after `$1`.
103        repl_suffix: String,
104    },
105    /// General precompiled regular expression fallback.
106    General {
107        /// Precompiled regex engine.
108        regex: Regex,
109        /// Replacement template.
110        replacement: String,
111        /// Whether `replacement` contains `$` capture expansion syntax.
112        has_expand: bool,
113    },
114}