chore: refactor
This commit is contained in:
parent
ca569d2bde
commit
6fe5cefc4a
6 changed files with 90 additions and 79 deletions
39
src/errors.rs
Normal file
39
src/errors.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PostWomanError {
|
||||
#[error("network error: {0:?}")]
|
||||
Request(#[from] reqwest::Error),
|
||||
|
||||
#[error("invalid method: {0:?}")]
|
||||
InvalidMethod(#[from] http::method::InvalidMethod),
|
||||
|
||||
#[error("invalid header name: {0:?}")]
|
||||
InvalidHeaderName(#[from] reqwest::header::InvalidHeaderName),
|
||||
|
||||
#[error("invalid header value: {0:?}")]
|
||||
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
|
||||
|
||||
#[error("contains Unprintable characters: {0:?}")]
|
||||
Unprintable(#[from] reqwest::header::ToStrError),
|
||||
|
||||
#[error("header '{0}' not found in response")]
|
||||
HeaderNotFound(String),
|
||||
|
||||
#[error("invalid header: '{0}'")]
|
||||
InvalidHeader(String),
|
||||
|
||||
#[error("error opening collection: {0:?}")]
|
||||
ErrorOpeningCollection(#[from] std::io::Error),
|
||||
|
||||
#[error("collection is not valid toml: {0:?}")]
|
||||
InvalidCollection(#[from] toml::de::Error),
|
||||
|
||||
#[error("could not represent collection: {0:?}")] // should never happen
|
||||
ErrorSerializingInternallyCollection(#[from] toml_edit::ser::Error),
|
||||
|
||||
#[error("invalid json payload: {0:?}")]
|
||||
InvalidJson(#[from] serde_json::Error),
|
||||
|
||||
#[error("invalid regex: {0:?}")]
|
||||
InvalidRegex(#[from] regex::Error),
|
||||
}
|
|
@ -1,11 +1,13 @@
|
|||
mod model;
|
||||
mod errors;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use model::{PostWomanConfig, PostWomanError};
|
||||
|
||||
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
use model::PostWomanConfig;
|
||||
|
||||
pub use errors::PostWomanError;
|
||||
pub static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
/// API tester and debugger from your CLI
|
||||
#[derive(Parser, Debug)]
|
||||
|
|
4
src/model/client.rs
Normal file
4
src/model/client.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PostWomanClient {
|
||||
pub user_agent: Option<String>,
|
||||
}
|
|
@ -1,48 +1,12 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
|
||||
use http::{HeaderMap, HeaderName, HeaderValue};
|
||||
|
||||
use crate::APP_USER_AGENT;
|
||||
use crate::{PostWomanError, APP_USER_AGENT};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PostWomanError {
|
||||
#[error("network error: {0:?}")]
|
||||
Request(#[from] reqwest::Error),
|
||||
use super::{Extractor, PostWomanClient, StringOr};
|
||||
|
||||
#[error("invalid method: {0:?}")]
|
||||
InvalidMethod(#[from] http::method::InvalidMethod),
|
||||
|
||||
#[error("invalid header name: {0:?}")]
|
||||
InvalidHeaderName(#[from] reqwest::header::InvalidHeaderName),
|
||||
|
||||
#[error("invalid header value: {0:?}")]
|
||||
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
|
||||
|
||||
#[error("contains Unprintable characters: {0:?}")]
|
||||
Unprintable(#[from] reqwest::header::ToStrError),
|
||||
|
||||
#[error("header '{0}' not found in response")]
|
||||
HeaderNotFound(String),
|
||||
|
||||
#[error("invalid header: '{0}'")]
|
||||
InvalidHeader(String),
|
||||
|
||||
#[error("error opening collection: {0:?}")]
|
||||
ErrorOpeningCollection(#[from] std::io::Error),
|
||||
|
||||
#[error("collection is not valid toml: {0:?}")]
|
||||
InvalidCollection(#[from] toml::de::Error),
|
||||
|
||||
#[error("could not represent collection: {0:?}")] // should never happen
|
||||
ErrorSerializingInternallyCollection(#[from] toml_edit::ser::Error),
|
||||
|
||||
#[error("invalid json payload: {0:?}")]
|
||||
InvalidJson(#[from] serde_json::Error),
|
||||
|
||||
#[error("invalid regex: {0:?}")]
|
||||
InvalidRegex(#[from] regex::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Endpoint {
|
||||
|
@ -175,40 +139,3 @@ async fn format_body(res: reqwest::Response) -> Result<String, PostWomanError> {
|
|||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum StringOr<T> {
|
||||
Str(String),
|
||||
T(T),
|
||||
}
|
||||
|
||||
impl<T: Default> Default for StringOr<T> {
|
||||
fn default() -> Self {
|
||||
Self::T(T::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "lowercase")]
|
||||
pub enum Extractor {
|
||||
#[default]
|
||||
Debug,
|
||||
Body,
|
||||
Discard,
|
||||
// JQL { query: String },
|
||||
// Regex { pattern: String },
|
||||
Header { key: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PostWomanClient {
|
||||
pub user_agent: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PostWomanConfig {
|
||||
pub client: PostWomanClient,
|
||||
// it's weird to name it singular but makes more sense in config
|
||||
pub route: indexmap::IndexMap<String, Endpoint>,
|
||||
}
|
12
src/model/extractor.rs
Normal file
12
src/model/extractor.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "lowercase")]
|
||||
pub enum Extractor {
|
||||
#[default]
|
||||
Debug,
|
||||
Body,
|
||||
Discard,
|
||||
// JQL { query: String },
|
||||
// Regex { pattern: String },
|
||||
Header { key: String },
|
||||
}
|
27
src/model/mod.rs
Normal file
27
src/model/mod.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
mod client;
|
||||
mod endpoint;
|
||||
mod extractor;
|
||||
|
||||
pub use client::PostWomanClient;
|
||||
pub use endpoint::Endpoint;
|
||||
pub use extractor::Extractor;
|
||||
|
||||
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PostWomanConfig {
|
||||
pub client: PostWomanClient,
|
||||
// it's weird to name it singular but makes more sense in config
|
||||
pub route: indexmap::IndexMap<String, Endpoint>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum StringOr<T> {
|
||||
Str(String),
|
||||
T(T),
|
||||
}
|
||||
|
||||
impl<T: Default> Default for StringOr<T> {
|
||||
fn default() -> Self {
|
||||
Self::T(T::default())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue