30 lines
846 B
Rust
30 lines
846 B
Rust
use super::characters::CharacterDataType;
|
|
use rocket::http::RawStr;
|
|
use rocket::request::FromFormValue;
|
|
use serde::Serialize;
|
|
use std::str::FromStr;
|
|
use thiserror::Error;
|
|
|
|
/// Validation errors from form submissions.
|
|
#[derive(Serialize, Error, Debug, Clone)]
|
|
pub enum ValidationError {
|
|
#[error("bad UTF-8 encoding")]
|
|
BadEncoding,
|
|
|
|
#[error("invalid game system: {0}")]
|
|
InvalidGameSystem(String),
|
|
}
|
|
|
|
impl<'v> FromFormValue<'v> for CharacterDataType {
|
|
type Error = ValidationError;
|
|
|
|
fn from_form_value(form_value: &'v RawStr) -> Result<CharacterDataType, ValidationError> {
|
|
let system = form_value
|
|
.url_decode()
|
|
.or(Err(ValidationError::BadEncoding))?;
|
|
|
|
CharacterDataType::from_str(&system)
|
|
.or_else(|_| Err(ValidationError::InvalidGameSystem(system.to_string())))
|
|
}
|
|
}
|