Skip to main content

apimock_routing/rule_set/rule/when/request/
url_path.rs

1use serde::Deserialize;
2
3use super::rule_op::RuleOp;
4
5#[derive(Clone, Debug, Deserialize)]
6#[serde(untagged)]
7pub enum UrlPathConfig {
8    Simple(String),
9    Detailed(UrlPath),
10}
11
12impl std::fmt::Display for UrlPathConfig {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        let _ = match self {
15            UrlPathConfig::Simple(s) => {
16                write!(f, "url_path{}`{}`", RuleOp::default(), s)
17            }
18            UrlPathConfig::Detailed(url_path) => {
19                write!(
20                    f,
21                    "url_path{}`{}`",
22                    url_path.op.clone().unwrap_or_default(),
23                    url_path.value,
24                )
25            }
26        };
27        Ok(())
28    }
29}
30
31#[derive(Clone, Debug, Deserialize)]
32pub struct UrlPath {
33    pub value: String,
34    #[serde(skip)]
35    pub value_with_prefix: String,
36    pub op: Option<RuleOp>,
37}
38
39impl UrlPath {
40    /// check if `url_path` in `when` matches
41    pub fn is_match(&self, parsed_request_url_path: &str) -> bool {
42        let op = self.op.clone().unwrap_or_default();
43        match op {
44            // contains op works with raw value (aka without url_path prefix)
45            RuleOp::Contains => op.is_match(parsed_request_url_path, self.value.as_str()),
46            _ => op.is_match(parsed_request_url_path, self.value_with_prefix.as_str()),
47        }
48    }
49
50    /// validate (ok when deserialization is successful)
51    pub fn validate(&self) -> bool {
52        true
53    }
54}