~jplatte/turbo.fish

ref: 69f909f0f8a30fb3c748fecac68ae9fa139e4bdd turbo.fish/src/turbofish.rs -rw-r--r-- 1.0 KiB View raw
69f909f0 — Jonas Platte Implement random_reverse 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::fmt;

use rocket::{
    http::{
        impl_from_uri_param_identity,
        uri::{Formatter, Path, UriDisplay},
        RawStr,
    },
    request::FromParam,
};

pub struct TurboFish(String);

impl TurboFish {
    pub fn new(guts: String) -> TurboFish {
        TurboFish(guts)
    }

    pub fn gut(self) -> String {
        self.0
    }
}

impl<'a> FromParam<'a> for TurboFish {
    type Error = &'a RawStr;

    fn from_param(param: &'a RawStr) -> Result<Self, Self::Error> {
        let param_cow = param.percent_decode().map_err(|_| param)?;
        let (back, rest) = param_cow.split_at(3);
        let (mid, front) = rest.split_at(rest.len() - 1);

        if back == "::<" && front == ">" {
            Ok(TurboFish(mid.replace("<", "<‚Äč")))
        } else {
            Err(param)
        }
    }
}

impl UriDisplay<Path> for TurboFish {
    fn fmt(&self, f: &mut Formatter<Path>) -> fmt::Result {
        f.write_value(&format!("::<{}>", self.0))
    }
}

impl_from_uri_param_identity!([Path] TurboFish);