Skip to content

Commit

Permalink
📝 修订正则解析并新增一些文档注释
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Jan 3, 2024
1 parent 8fe0e81 commit 1d90d82
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Request {
let method = method.to_uppercase();
let path = OblivionPath::new(&olps)?;
let olps = path.get_olps();
let oblivion = Oblivion::new(&method, &olps)?;
let oblivion = Oblivion::new(&method, &olps);
let plain_text = oblivion.plain_text();
Ok(Self {
method,
Expand Down Expand Up @@ -188,13 +188,13 @@ impl Request {
let mut oed = OED::new(self.aes_key.clone());
oed.from_stream(tcp, 5).await?;

let mut osc = OSC::from_stream(tcp).await?;
let osc = OSC::from_stream(tcp).await?;

let response = Response::new(
self.plain_text.clone(),
oed.get_data(),
self.olps.clone(),
osc.get_status_code(),
osc.status_code,
);
Ok(response)
}
Expand Down
6 changes: 1 addition & 5 deletions src/models/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ACK {
}

pub struct OSC {
status_code: i32,
pub status_code: i32,
}

impl OSC {
Expand All @@ -72,10 +72,6 @@ impl OSC {
let status_code = format!("{}", self.status_code);
status_code.into_bytes()
}

pub fn get_status_code(&mut self) -> i32 {
self.status_code
}
}

pub struct OKE<'a> {
Expand Down
72 changes: 44 additions & 28 deletions src/utils/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ pub fn length(bytes: &Vec<u8>) -> Result<Vec<u8>, OblivionException> {
Ok(list_num.into_iter().collect::<String>().into_bytes())
}

/// Oblivion Location Path String Parser
///
/// ```rust
/// use oblivion::utils::parser::OblivionPath;
///
/// let olps = OblivionPath::new("oblivion://127.0.0.1:813/test").unwrap();
///
/// assert_eq!("oblivion".to_string(), olps.get_protocol());
/// assert_eq!("127.0.0.1".to_string(), olps.get_host());
/// assert_eq!("813".to_string(), olps.get_port());
/// assert_eq!("/test".to_string(), olps.get_olps());
/// ```
pub struct OblivionPath {
protocol: String,
host: String,
Expand All @@ -50,7 +62,7 @@ pub struct OblivionPath {
impl OblivionPath {
pub fn new(obl_str: &str) -> Result<Self, OblivionException> {
let re = Regex::new(
r"^(?P<protocol>oblivion)?(?:://)?(?P<host>[^:/]+)(:(?P<port>\d+))?(?P<url>.+)?$",
r"^(?P<protocol>oblivion)?(?:://)?(?P<host>[^:/]+)(:(?P<port>\d+))?(?P<olps>.+)?$",
)
.unwrap();

Expand All @@ -64,31 +76,27 @@ impl OblivionPath {
}
}

let protocol = extracted_values
.get("protocol")
.unwrap_or(&None)
.unwrap_or_default()
.to_string();
let host = extracted_values
.get("host")
.unwrap_or(&None)
.unwrap_or_default()
.to_string();
let port = extracted_values
.get("port")
.unwrap_or(&Some("80"))
.unwrap_or_default()
.to_string();
let url = extracted_values
.get("url")
.unwrap_or(&Some("/"))
.unwrap_or_default()
.to_string();
let protocol = match extracted_values.get("protocol").unwrap() {
Some(result) => result.to_string(),
None => "oblivion".to_string(),
};
let host = match extracted_values.get("host").unwrap() {
Some(result) => result.to_string(),
None => "oblivion".to_string(),
};
let port = match extracted_values.get("port").unwrap() {
Some(result) => result.to_string(),
None => "80".to_string(),
};
let olps = match extracted_values.get("olps").unwrap() {
Some(result) => result.to_string(),
None => "/".to_string(),
};
Ok(Self {
protocol: protocol,
host: host,
port: port,
olps: url,
protocol,
host,
port,
olps,
})
} else {
Err(OblivionException::InvalidOblivion {
Expand All @@ -114,19 +122,26 @@ impl OblivionPath {
}
}

/// Oblivion Request Header Generator
///
/// ```rust
/// use oblivion::utils::parser::Oblivion;
///
/// assert_eq!(Oblivion::new("GET", "/test").plain_text().as_str(), "GET /test Oblivion/1.1");
/// ```
pub struct Oblivion {
method: String,
olps: String,
version: String,
}

impl Oblivion {
pub fn new(method: &str, olps: &str) -> Result<Self, OblivionException> {
Ok(Self {
pub fn new(method: &str, olps: &str) -> Self {
Self {
method: method.to_string(),
olps: olps.to_string(),
version: "1.1".to_string(),
})
}
}

pub fn plain_text(&self) -> String {
Expand All @@ -139,6 +154,7 @@ impl Oblivion {
}
}

/// Oblivion Request Header Parser
#[derive(Clone, Debug, PartialEq)]
pub struct OblivionRequest {
pub(crate) method: String,
Expand Down

0 comments on commit 1d90d82

Please sign in to comment.