tidying fns

This commit is contained in:
Mashy 2019-07-19 21:01:12 +10:00
parent 9c45a3fb7a
commit b289ed92de

View File

@ -41,21 +41,31 @@ pub enum MnmlHttpError {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct JsonResponse { struct JsonResponse {
response: String, response: Option<String>,
success: bool, success: bool,
error_message: String error_message: Option<String>
} }
impl JsonResponse { impl JsonResponse {
fn success(response: String) -> Self { fn success(response: String) -> Self {
JsonResponse { response: response, success: true, error_message: "".to_string() } JsonResponse { response: Some(response), success: true, error_message: None }
} }
fn error(msg: String) -> Self { fn error(msg: String) -> Self {
JsonResponse { response: "".to_string(), success: false, error_message: msg } JsonResponse { response: None, success: false, error_message: Some(msg) }
} }
} }
fn iron_response (status: status::Status, message: String) -> Response {
let content_type = "application/json".parse::<Mime>().unwrap();
let msg = match status {
status::Ok => JsonResponse::success(message),
_ => JsonResponse::error(message)
};
let msg_out = serde_json::to_string(&msg).unwrap();
return Response::with((content_type, status, msg_out));
}
impl From<MnmlHttpError> for IronError { impl From<MnmlHttpError> for IronError {
fn from(m_err: MnmlHttpError) -> Self { fn from(m_err: MnmlHttpError) -> Self {
let (err, res) = match m_err { let (err, res) = match m_err {
@ -67,11 +77,7 @@ impl From<MnmlHttpError> for IronError {
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest), MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized), MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized),
}; };
let err_msg = JsonResponse::error(m_err.to_string()); IronError { error: Box::new(err), response: iron_response(res, m_err.to_string()) }
let err_out = serde_json::to_string(&err_msg).expect("Failed to encode response");
let content_type = "application/json".parse::<Mime>().expect("Failed to parse content type");
IronError::new(err, (content_type, res, err_out))
} }
} }
@ -132,15 +138,7 @@ fn token_res(token: String) -> Response {
.max_age(Duration::weeks(1)) // 1 week aligns with db set .max_age(Duration::weeks(1)) // 1 week aligns with db set
.finish(); .finish();
let content_type = "application/json".parse::<Mime>().expect("Failed to parse content type"); let mut res = iron_response(status::Ok, "token_res".to_string());
let msg = JsonResponse {
response: "success666".to_string(),
success: true,
error_message: "".to_string()
};
let msg_out = serde_json::to_string(&msg).expect("Failed to encode response");
let mut res = Response::with((content_type, status::Ok, msg_out));
res.headers.set(SetCookie(vec![v.to_string()])); res.headers.set(SetCookie(vec![v.to_string()]));
return res; return res;
@ -216,7 +214,7 @@ fn logout(req: &mut Request) -> IronResult<Response> {
tx.commit().or(Err(MnmlHttpError::ServerError))?; tx.commit().or(Err(MnmlHttpError::ServerError))?;
let mut res = Response::with(status::Ok); let mut res = iron_response(status::Ok, "token_res".to_string());
res.headers.set(SetCookie(vec![AUTH_CLEAR.to_string()])); res.headers.set(SetCookie(vec![AUTH_CLEAR.to_string()]));
Ok(res) Ok(res)