Skip to content

Commit c4f2bce

Browse files
committed
Move PathRewriter
1 parent 417522e commit c4f2bce

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/handler/mod.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ mod proxy;
55

66
use anyhow::Result;
77
use hyper::Body;
8-
use regex::Regex;
98

109
use self::fs::{DirHandler, FileHandler};
1110
use self::json::JsonHandler;
1211
use self::mock::MockHandler;
1312
use self::proxy::ProxyHandler;
1413
use crate::config;
14+
use crate::path::PathRewriter;
1515

1616
#[derive(Debug)]
1717
pub struct Handler {
@@ -29,12 +29,6 @@ pub enum HandlerKind {
2929
Mock(MockHandler),
3030
}
3131

32-
#[derive(Debug)]
33-
pub struct PathRewriter {
34-
regex: Regex,
35-
replace: String,
36-
}
37-
3832
impl Handler {
3933
pub async fn new(route: config::Route) -> Result<Self> {
4034
let config::Route {
@@ -45,7 +39,7 @@ impl Handler {
4539
} = route;
4640
let path_rewriter = rewrite_path.map(|replace| {
4741
let regex = route.to_regex();
48-
PathRewriter { regex, replace }
42+
PathRewriter::new(regex, replace)
4943
});
5044

5145
let kind = match kind {
@@ -87,9 +81,3 @@ impl Handler {
8781
result
8882
}
8983
}
90-
91-
impl PathRewriter {
92-
fn rewrite<'a>(&self, path: &'a str) -> String {
93-
self.regex.replace(path, self.replace.as_str()).into_owned()
94-
}
95-
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use structopt::StructOpt;
22

33
mod config;
44
mod handler;
5+
mod path;
56
mod response;
67
mod route;
78
mod server;

src/path.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use regex::Regex;
2+
3+
#[derive(Debug)]
4+
pub struct PathRewriter {
5+
regex: Regex,
6+
replace: String,
7+
}
8+
9+
impl PathRewriter {
10+
pub fn new(regex: Regex, replace: String) -> Self {
11+
PathRewriter { regex, replace }
12+
}
13+
14+
pub fn rewrite<'a>(&self, path: &'a str) -> String {
15+
self.regex.replace(path, self.replace.as_str()).into_owned()
16+
}
17+
}

0 commit comments

Comments
 (0)