diff --git a/src/request.rs b/src/request.rs index ea145b1..cbb2918 100644 --- a/src/request.rs +++ b/src/request.rs @@ -21,7 +21,7 @@ pub enum RequestHeader { Host(String), UserAgent(String), ContentType(ContentType), - Accept((String, String)), + Accept(ContentType), Other(String, String), } @@ -29,7 +29,25 @@ impl FromStr for RequestHeader { type Err = io::Error; fn from_str(s: &str) -> Result { - let + let header_split: Vec<&str> = s.split(": ").collect(); + + match header_split.as_slice() { + [header_type, value] => match *header_type { + "Host" => Ok(RequestHeader::Host(value.to_string())), + "UserAgent" => Ok(RequestHeader::UserAgent(value.to_string())), + "ContentType" => Ok(RequestHeader::ContentType(ContentType::from_str(value)?)), + "Accept" => Ok(RequestHeader::Accept(ContentType::from_str(value)?)), + + _ => Ok(RequestHeader::Other( + header_type.to_string(), + value.to_string(), + )), + }, + _ => Err(io::Error::new( + io::ErrorKind::InvalidData, + "Invalid header-type", + )), + } } } diff --git a/src/shared_enums.rs b/src/shared_enums.rs index 99e924a..c5add9e 100644 --- a/src/shared_enums.rs +++ b/src/shared_enums.rs @@ -1,14 +1,30 @@ +use std::{io, str::FromStr}; + pub enum ContentType { Text(TextType), Aplication(ApplicationType), + Any, +} + +impl FromStr for ContentType { + type Err = io::Error; + + fn from_str(s: &str) -> Result { + Err(io::Error::new( + io::ErrorKind::InvalidData, + "Invalid content-type", + )) + } } pub enum TextType { Html, Css, Javascript, + Any, } pub enum ApplicationType { Json, + Any, }