Skip to content

Commit

Permalink
refactor: add inline to all functions with small callsite (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Dec 27, 2023
1 parent e10a179 commit 1c35a34
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions hitt-cli/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
},
};

#[inline]
fn set_method(term: &Term) -> Result<String, std::io::Error> {
let http_methods = [
"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "CONNECT", "TRACE",
Expand All @@ -21,6 +22,7 @@ fn set_method(term: &Term) -> Result<String, std::io::Error> {
select_input(term, "Which HTTP method?", &http_methods)
}

#[inline]
fn set_url(term: &Term) -> Result<String, std::io::Error> {
text_input_prompt(
term,
Expand All @@ -30,6 +32,7 @@ fn set_url(term: &Term) -> Result<String, std::io::Error> {
)
}

#[inline]
fn set_headers(term: &Term) -> Result<Vec<(String, String)>, std::io::Error> {
let mut headers = Vec::new();

Expand Down Expand Up @@ -71,6 +74,7 @@ fn set_headers(term: &Term) -> Result<Vec<(String, String)>, std::io::Error> {
Ok(headers)
}

#[inline]
fn try_find_content_type(headers: &[(String, String)]) -> Option<&str> {
for (key, value) in headers {
if key.eq_ignore_ascii_case("content-type") {
Expand All @@ -81,6 +85,7 @@ fn try_find_content_type(headers: &[(String, String)]) -> Option<&str> {
None
}

#[inline]
fn set_body(term: &Term, content_type: Option<&str>) -> Result<Option<String>, std::io::Error> {
if !confirm_input(term, "Do you want to add a body? (Y/n)", Key::Char('y'))? {
return Ok(None);
Expand Down Expand Up @@ -116,6 +121,7 @@ fn save_request(
std::fs::write(path, contents).map_err(|error| HittCliError::IoWrite(path.to_path_buf(), error))
}

#[inline]
async fn check_if_exist(term: &Term, path: &std::path::Path) -> Result<(), std::io::Error> {
if tokio::fs::try_exists(path).await? {
let should_continue = confirm_input(
Expand Down
3 changes: 3 additions & 0 deletions hitt-cli/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum HittCliError {
}

impl fmt::Display for HittCliError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let error_message = match self {
Self::Parse(path, error) => format!("error parsing file {path:?} - {error}"),
Expand All @@ -30,12 +31,14 @@ impl fmt::Display for HittCliError {
}

impl From<tokio::task::JoinError> for HittCliError {
#[inline]
fn from(value: tokio::task::JoinError) -> Self {
Self::Join(value)
}
}

impl From<std::io::Error> for HittCliError {
#[inline]
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
Expand Down
1 change: 1 addition & 0 deletions hitt-cli/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use hitt_parser::HittRequest;

use crate::error::HittCliError;

#[inline]
async fn get_file_content(path: &std::path::Path) -> Result<String, std::io::Error> {
tokio::fs::read(path)
.await
Expand Down
2 changes: 2 additions & 0 deletions hitt-cli/src/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ pub(crate) fn handle_response(
Ok(())
}

#[inline]
pub(crate) fn write_prompt(term: &Term, prompt: &str) -> Result<(), std::io::Error> {
term.write_line(prompt)
}

#[inline]
pub(crate) fn write_prompt_answer(
term: &Term,
prompt: &str,
Expand Down
1 change: 1 addition & 0 deletions hitt-formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum ContentType {
}

impl From<&str> for ContentType {
#[inline]
fn from(value: &str) -> Self {
if value.starts_with("application/json") {
return Self::Json;
Expand Down
1 change: 1 addition & 0 deletions hitt-parser/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum RequestParseError {
}

impl core::fmt::Display for RequestParseError {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidHttpMethod(method) => write!(f, "invalid HTTP method '{method}'"),
Expand Down
3 changes: 3 additions & 0 deletions hitt-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ mod test_parse_uri_input {
}
}

#[inline]
fn parse_http_version(
chars: &mut core::iter::Enumerate<core::str::Chars>,
) -> Option<http::version::Version> {
Expand Down Expand Up @@ -396,6 +397,7 @@ impl From<HeaderToken> for RequestToken {
}
}

#[inline]
fn tokenize(buffer: &str) -> Result<Vec<RequestToken>, RequestParseError> {
let mut tokens: Vec<RequestToken> = Vec::new();

Expand Down Expand Up @@ -608,6 +610,7 @@ struct PartialHittRequest {
}

impl PartialHittRequest {
#[inline]
fn build(self) -> Result<HittRequest, RequestParseError> {
match self.method {
Some(method) => match self.uri {
Expand Down
1 change: 1 addition & 0 deletions hitt-request/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct HittResponse {
pub duration: core::time::Duration,
}

#[inline]
pub async fn send_request(
http_client: &reqwest::Client,
input: &HittRequest,
Expand Down

0 comments on commit 1c35a34

Please sign in to comment.