}
}
+macro_rules! extract_data {
+ ($req:expr,$n:expr) => {
+ $req.uri().path().split('/').nth($n).unwrap()
+ }
+}
+
+macro_rules! extract_header {
+ ($req:expr,$header:expr) => {
+ match $req.headers().get($header) {
+ Some(header) => header.to_str().unwrap_or(""),
+ _ => ""
+ }
+ }
+}
+
#[derive(Debug)]
struct ImageError(image::ImageError);
#[get("/gen/0/{data}")]
async fn img_gen0(req: HttpRequest) -> Result<impl Responder> {
- let data = req.uri().path().split('/').nth(3).unwrap();
+ let data = extract_data!(req, 3);
let cursor = make_png(32, 32, 16, percent_decode_str(data).into_iter().borrow_mut())?;
Ok(response_image!(cursor))
}
#[get("/gen/1/{dim_x}/{dim_y}/{scale}/{data}")]
async fn img_gen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>) -> Result<impl Responder> {
let (dim_x, dim_y, scale) = path.into_inner();
- let data = req.uri().path().split('/').nth(6).unwrap();
+ let data = extract_data!(req, 6);
let cursor = make_png(dim_x, dim_y, scale, percent_decode_str(data).into_iter().borrow_mut())?;
Ok(response_image!(cursor))
}
#[get("/gen/2/{dim_x}/{dim_y}/{scale}")]
async fn img_gen2(req: HttpRequest, path: web::Path<(u32, u32, u32)>) -> Result<impl Responder> {
let (dim_x, dim_y, scale) = path.into_inner();
- let data = match req.headers().get("user-agent") {
- Some(header) => header.to_str().unwrap_or(""),
- _ => ""
- };
+ let data = extract_header!(req, "user-agent");
let cursor = make_png(dim_x, dim_y, scale, &mut data.bytes())?;
Ok(response_image!(cursor))
}
#[get("/gen/3/{scale}")]
async fn img_gen3(req: HttpRequest, path: web::Path<u32>) -> Result<impl Responder> {
let scale = path.into_inner();
- let data = match req.headers().get("user-agent") {
- Some(header) => header.to_str().unwrap_or(""),
- _ => ""
- };
+ let data = extract_header!(req, "user-agent");
let rgbimg: RgbImage = rgb_encode(ImageBuffer::new(TIMEAVATAR_SIZE_U32, TIMEAVATAR_SIZE_U32), &mut data.bytes())?;
let mut resimg: RgbImage = ImageBuffer::new(60 * TIMEAVATAR_SIZE_U32, 24 * TIMEAVATAR_SIZE_U32);
let time = OffsetDateTime::now_utc().time();
#[get("/pgen/0/{data}")]
async fn img_pgen0(req: HttpRequest, canvas0: web::Data<Arc<Mutex<Canvas0>>>) -> Result<impl Responder> {
- let data = req.uri().path().split('/').nth(3).unwrap();
+ let data = extract_data!(req, 3);
let scale = 16;
let img: RgbImage = ({
let canvas_option = &mut *canvas0.lock().unwrap();
#[get("/pgen/1/{dim_x}/{dim_y}/{scale}/{data}")]
async fn img_pgen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>, canvas1: web::Data<Arc<Mutex<Canvas1>>>) -> Result<impl Responder> {
let (dim_x, dim_y, scale) = path.into_inner();
- let data = req.uri().path().split('/').nth(6).unwrap();
+ let data = extract_data!(req, 6);
let img: RgbImage = ({
let canvas_option = &mut *canvas1.lock().unwrap();
Ok(apply_to_canvas( |c, d| rgb_encode_to_canvas(c, d), &mut canvas_option.0, Some((dim_x, dim_y)), data)?.img.clone())
#[get("/pgen/2/{dim_x}/{dim_y}/{scale}")]
async fn img_pgen2(req: HttpRequest, path: web::Path<(u32, u32, u32)>, canvas1: web::Data<Arc<Mutex<Canvas1>>>) -> Result<impl Responder> {
let (dim_x, dim_y, scale) = path.into_inner();
- let data = match req.headers().get("user-agent") {
- Some(header) => header.to_str().unwrap_or(""),
- _ => ""
- };
+ let data = extract_header!(req, "user-agent");
let img: RgbImage = ({
let canvas_option = &mut *canvas1.lock().unwrap();
Ok(apply_to_canvas( |c, d| rgb_encode_to_canvas(c, d), &mut canvas_option.0, Some((dim_x, dim_y)), data)?.img.clone())
#[get("/pgen/3/{scale}")]
async fn img_pgen3(req: HttpRequest, path: web::Path<u32>, canvas2: web::Data<Arc<Mutex<Canvas2>>>) -> Result<impl Responder> {
let scale = path.into_inner();
- let data = match req.headers().get("user-agent") {
- Some(header) => header.to_str().unwrap_or(""),
- _ => ""
- };
+ let data = extract_header!(req, "user-agent");
let time = OffsetDateTime::now_utc().time();
let (hour, minute): (i64, i64) = (time.hour().into(), time.minute().into());
let rgbimg: RgbImage = rgb_encode(ImageBuffer::new(TIMEAVATAR_SIZE_U32, TIMEAVATAR_SIZE_U32), &mut data.bytes())?;