use thiserror::Error;

pub const TOKEN_PREFIX: &str = "sp_live_";
pub const TOKEN_SECRET_LEN: usize = 32;

#[derive(Debug, Error)]
pub enum TokenError {
    #[error("token must start with `{TOKEN_PREFIX}`")]
    BadPrefix,
    #[error("token secret must be {TOKEN_SECRET_LEN} characters")]
    BadLength,
}

pub fn validate(token: &str) -> Result<(), TokenError> {
    let secret = token
        .strip_prefix(TOKEN_PREFIX)
        .ok_or(TokenError::BadPrefix)?;
    if secret.len() != TOKEN_SECRET_LEN {
        return Err(TokenError::BadLength);
    }
    Ok(())
}
