use fastly::{Error, Request, Response};
use lazy_static::lazy_static;
use regex::Regex;

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
    log_fastly::init_simple("mylogs", log::LevelFilter::Info);
    let url = req.get_url_str();

    {
        // Capture a group with brackets and regex::captures[n]
        lazy_static! {
            static ref RE: Regex = Regex::new(r"/status/(\d+)").unwrap();
        }
        if let Some(caps) = RE.captures(url) {
            assert_eq!(&caps[1], "200");
            log::info!("Captured status code using positional groups: {}", &caps[1]);
        }
    }

    {
        // Named captures with (?P<name>exp)
        lazy_static! {
            static ref RE: Regex = Regex::new(r"/status/(?P<code>\d+)").unwrap();
        }
        if let Some(caps) = RE.captures(url) {
            assert_eq!(&caps["code"], "200");
            log::info!("Captured status code using named groups: {}", &caps["code"]);
        }
    }

    {
        // Non-capturing groups can be created by prefixing the
        // bracketed expression with ?:
        lazy_static! {
            static ref RE: Regex = Regex::new(r"/(?:status|code)/(\d+)\?").unwrap();
        }
        if let Some(caps) = RE.captures(url) {
            assert_eq!(&caps[1], "200");
            log::info!("Captured status code using non-capturing groups: {}", &caps[1]);
        }
    }

    {
        // Bracketed character class with [...]
        lazy_static! {
            static ref RE: Regex = Regex::new(r"[?&]aaa=([^&]*)").unwrap();
        }
        if let Some(caps) = RE.captures(url) {
            assert_eq!(&caps[1], "foo");
            log::info!("Captured aaa parameter using bracketed character class: {}", &caps[1]);
        }
    }

    {
        // Specific repeat count with {min,max}
        // Alternate options using (a|b)
        lazy_static! {
            static ref RE: Regex = Regex::new(r"[\?&]a{2,6}=(foo|bar)(?:$|&)").unwrap();
        }
        if let Some(caps) = RE.captures(url) {
            assert_eq!(&caps[1], "foo");
            log::info!("Captured aaa parameter using specific repeat count and alternate options: {}", &caps[1]);
        }
    }

    {
        // Make the match case insensitive using an (?i) prefix
        lazy_static! {
            static ref RE: Regex = Regex::new(r"(?i)AaA=Foo").unwrap();
        }
        if let Some(caps) = RE.captures(url) {
            assert_eq!(&caps[0], "aaa=foo");
            log::info!("Case insensitive match using (?i)AaA=Foo: {}", &caps[0]);
        }
    }

    Ok(Response::new())
}