haha it works

This commit is contained in:
Cadey Ratio 2020-07-23 22:32:30 -04:00
parent b7f2f8bf4a
commit 5cce5a618c
5 changed files with 33 additions and 17 deletions

View File

@ -1,16 +1,5 @@
#[macro_use]
extern crate num_derive;
mod status_code;
mod response;
pub use status_code::StatusCode;
pub use response::Response;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
pub use response::{Response, Error as ResponseError};

View File

@ -19,6 +19,7 @@ enum State {
ReadBody { data: Vec<u8> },
}
/// Response parsing error.
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("unexpected end of file found while parsing response")]
@ -60,7 +61,7 @@ impl Response {
Err(why) => return Err(Error::IO(why)),
}
log::trace!("state: {:?}", state);
// log::trace!("state: {:?}", state);
match &mut state {
State::ReadStatusCode { data } => match buf[0] as char {
@ -105,11 +106,35 @@ mod tests {
use std::io::prelude::*;
#[test]
fn parse() -> Result<(), Error> {
pretty_env_logger::init();
fn success() -> Result<(), Error> {
let _ = pretty_env_logger::try_init();
let mut fin = std::fs::File::open("./testdata/simple_response.txt")?;
Response::parse(&mut fin)?;
let resp = Response::parse(&mut fin)?;
assert_eq!(resp.meta, "text/gemini".to_string());
assert_eq!(resp.status, StatusCode::Success);
Ok(())
}
#[test]
fn error() -> Result<(), Error> {
let _ = pretty_env_logger::try_init();
let mut fin = std::fs::File::open("./testdata/error_response.txt")?;
let resp = Response::parse(&mut fin)?;
assert_eq!(resp.status, StatusCode::PermanentFailure);
Ok(())
}
#[test]
fn not_found() -> Result<(), Error> {
let _ = pretty_env_logger::try_init();
let mut fin = std::fs::File::open("./testdata/notfound_response.txt")?;
let resp = Response::parse(&mut fin)?;
assert_eq!(resp.status, StatusCode::NotFound);
Ok(())
}

View File

@ -1,5 +1,5 @@
/// Status codes as specified in [the spec](https://gemini.circumlunar.space/docs/specification.html).
#[derive(Copy, Clone, num_derive::FromPrimitive)]
#[derive(Copy, Clone, num_derive::FromPrimitive, PartialEq, Debug)]
pub enum StatusCode {
/// The requested resource accepts a line of textual user input.
/// The <META> line is a prompt which should be displayed to the

1
testdata/error_response.txt vendored Normal file
View File

@ -0,0 +1 @@
50 oh god everything is on fire

1
testdata/notfound_response.txt vendored Normal file
View File

@ -0,0 +1 @@
51 Not found!