use fastly::http::StatusCode;
use fastly::{Error, Request, Response};

/// The name of a backend server associated with this service.
const BACKEND_NAME: &str = "origin_0";

#[fastly::main]
fn main(req: Request) -> Result<Response, Error> {
    let resp = req.send(BACKEND_NAME)?;
    if resp.get_status() == StatusCode::OK {
        // s.chars().skip(a).take(b).collect()
        // will take a substring from `s` across the range [a..(b+a))
        // If [a..(b+a)) is out of range,
        // we will take as much as possible without panicking.
        let s = "afoobar";
        let substr: String = s.chars().skip(1).take(3).collect();

        // resp_body will be "left=foo"
        let resp_body = format!("left={}", substr);

        return Ok(Response::from_body(resp_body));
    }

    Ok(resp)
}