Skip to content
Snippets Groups Projects
resp.rs 1.21 KiB
Newer Older
use chrono::{Days, Utc};

#[derive(Copy, Clone)]
pub enum Expiry {
    Instant,
    Year,
}

impl Expiry {
    pub fn header_value(self) -> Option<String> {
        let date = match self {
            Expiry::Instant => return None,
            Expiry::Year => Utc::now() + Days::new(365),
        };

        Some(date.format("%a, %d %b %Y %T GMT").to_string())
    }
}

Stephen D's avatar
Stephen D committed
#[derive(Clone)]
pub struct Response {
    pub content_type: &'static str,
    pub expiry: Expiry,
Stephen D's avatar
Stephen D committed
    pub data: Vec<u8>,
}

impl Response {
    pub fn html(html: String) -> Self {
        Self {
            content_type: "text/html; charset=utf-8",
            expiry: Expiry::Instant,
Stephen D's avatar
Stephen D committed
            data: html.into_bytes(),
        }
    }
Stephen D's avatar
Stephen D committed

    pub fn css(css: String) -> Self {
        Self {
            content_type: "text/css; charset=utf-8",
            expiry: Expiry::Year,
Stephen D's avatar
Stephen D committed
            data: css.into_bytes(),
        }
    }
Stephen D's avatar
Stephen D committed

    pub fn png(data: Vec<u8>) -> Self {
        Self {
            content_type: "image/png",
            expiry: Expiry::Year,
Stephen D's avatar
Stephen D committed
            data,
        }
    }

    pub fn ico(data: Vec<u8>) -> Self {
        Self {
            content_type: "image/x-icon",
            expiry: Expiry::Year,
Stephen D's avatar
Stephen D committed
            data,
        }
    }
Stephen D's avatar
Stephen D committed
}