Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: derive hash etc #49

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/server/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Env {
pub listen: String,
pub port: String,
Expand Down
16 changes: 8 additions & 8 deletions dev/server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub type AppError<K, T = ()> = AppErrorDetail<K, T>;

pub const APP_DEFAULT_ERROR_CODE: StatusCode = StatusCode::BAD_REQUEST;

#[derive(Error, Debug)]
#[derive(Error, Debug, Clone, PartialEq, Eq, Default, Hash)]
#[error("{0}")]
pub struct Logged<T>(pub T);
#[derive(Error, Debug)]
Expand Down Expand Up @@ -88,39 +88,39 @@ pub mod kind {
fn msg() -> &'static str;
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NotFound {}
impl Kind for NotFound {
fn msg() -> &'static str {
"not found"
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BadRequest {}
impl Kind for BadRequest {
fn msg() -> &'static str {
"bad request"
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Retriable {}
impl Kind for Retriable {
fn msg() -> &'static str {
"please try again later"
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Unreachable {}
impl Kind for Unreachable {
fn msg() -> &'static str {
"something went wrong"
}
}
}
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[derive(Error, Debug, Clone, PartialEq, Eq, Default, Hash)]
pub struct AppErrorInner<K, T> {
pub msg: PhantomData<K>,
pub detail: T,
Expand All @@ -130,7 +130,7 @@ impl<K: kind::Kind, T: Serialize> IntoResponse for AppErrorInner<K, T> {
Json(ErrorResponseInner::from(self)).into_response()
}
}
#[derive(Error, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Error, Debug, Clone, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
pub struct ErrorResponseInner<T> {
pub msg: String,
pub detail: T,
Expand All @@ -144,7 +144,7 @@ impl<K: kind::Kind, T> From<AppErrorInner<K, T>> for ErrorResponseInner<T> {
pub mod counter {
use super::*;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[derive(Error, Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CounterError<E> {
#[error("overflow counter")]
Overflow(E),
Expand Down
6 changes: 3 additions & 3 deletions dev/server/src/route/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn route_counter() -> Router<AppState> {
.route("/resets", get(reset::<BInt>))
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Serialize, Deserialize)]
pub struct CounterState {
#[serde(with = "bigint_string")]
pub count: BigInt,
Expand All @@ -56,11 +56,11 @@ mod bigint_string {
}

// TODO better implementation for increment/increments ?
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Serialize, Deserialize)]
pub struct CounterResponse<T> {
pub count: T,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Serialize, Deserialize)]
pub struct BInt(#[serde(with = "bigint_string")] pub BigInt);
impl From<BigInt> for BInt {
fn from(value: BigInt) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion dev/server/src/route/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub async fn json_body(body: Json<Value>) -> Json<Value> {
body
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
pub struct Jsonizer(pub Vec<(String, String)>);
impl Jsonizer {
pub fn entry<'a, I: Iterator<Item = &'a str>>(
Expand Down
4 changes: 2 additions & 2 deletions dev/server/src/route/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn route_health() -> Router<AppState> {
.route("/disabled", get(disabled))
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Serialize, Deserialize)]
pub struct Health {
#[serde(flatten, with = "health_response")]
pub status: StatusCode,
Expand All @@ -31,7 +31,7 @@ impl IntoResponse for Health {
}
mod health_response {
use super::*;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct HealthResponse {
status: String,
code: u16,
Expand Down
2 changes: 1 addition & 1 deletion dev/server/src/route/information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn route_information() -> Router<AppState> {
.route("/*path", any(information))
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct InformationResponse {
#[serde(default)]
pub datetime: Option<DateTime<Utc>>,
Expand Down
4 changes: 2 additions & 2 deletions dev/server/src/route/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ where
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RandomString {
#[serde(default)]
pub len: Option<usize>,
Expand Down Expand Up @@ -103,7 +103,7 @@ impl RandomResponse {
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DistRangeParam<T> {
#[serde(default)]
pub low: Option<T>,
Expand Down
17 changes: 9 additions & 8 deletions dev/server/src/route/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,34 @@ use super::PinResponseFuture;

pub fn route_wait() -> Router<AppState> {
Router::new()
.route("/:duration", get(DurationUnit::Seconds.handler()))
.route("/:duration", get(DurationUnit::default().handler()))
.route("/:duration/s", get(DurationUnit::Seconds.handler()))
.route("/:duration/ms", get(DurationUnit::Milliseconds.handler()))
.route("/:duration/ns", get(DurationUnit::Nanoseconds.handler()))
// .fallback() // TODO
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
pub struct WaitResponse {
pub duration: u64,
pub unit: DurationUnit,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Serialize, Deserialize)]
pub enum DurationUnit {
Seconds,
Milliseconds,
Nanoseconds,
Milliseconds,
#[default]
Seconds,
}
impl DurationUnit {
pub fn handler(self) -> impl FnOnce(Path<u64>) -> PinResponseFuture<Result<Json<WaitResponse>>> + Clone {
// return type ref: https://github.com/tokio-rs/axum/pull/1082/files#diff-93eb961c85da77636607a224513f085faf7876f5a9f7091c13e05939aa5de33cR61-R62
move |Path(duration): Path<u64>| {
move |Path(duration)| {
let d = match self {
DurationUnit::Seconds => Duration::from_secs(duration),
DurationUnit::Milliseconds => Duration::from_millis(duration),
DurationUnit::Nanoseconds => Duration::from_nanos(duration),
DurationUnit::Milliseconds => Duration::from_millis(duration),
DurationUnit::Seconds => Duration::from_secs(duration),
};
let unit = self.clone();
Box::pin(async move {
Expand Down
2 changes: 1 addition & 1 deletion src/assault/evaluate/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
interface::{config::Severity, helper::is_default::IsDefault},
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct JsonEvaluate {
#[serde(default, skip_serializing_if = "IsDefault::is_default")]
Expand Down
2 changes: 1 addition & 1 deletion src/assault/evaluate/plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
error::EvaluateError,
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct PlaintextEvaluate {
pub regex: Option<AllOr<String>>,
Expand Down
2 changes: 1 addition & 1 deletion src/assault/service/origin_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tower::Service;

use crate::error::{AssaultError, Wrap};

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct OriginRouter<S, B> {
map: HashMap<Authority, S>,
phantom: PhantomData<B>,
Expand Down
4 changes: 2 additions & 2 deletions src/assault/service/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
pub struct RecordLayer {
path: Option<PathBuf>,
}
Expand All @@ -117,7 +117,7 @@ impl<S> Layer<S> for RecordLayer {
}
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
pub struct RecordService<S> {
path: Option<PathBuf>,
inner: S,
Expand Down
6 changes: 3 additions & 3 deletions src/assault/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use super::{
};

/// TODO document
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Control<'a, Q, P, S, Req> {
client: &'a mut S,
phantom: PhantomData<(Q, P, S, Req)>,
Expand Down Expand Up @@ -61,7 +61,7 @@ where
}

/// TODO document
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Worker<'a, Q, P, S, Req> {
client: &'a mut S,
phantom: PhantomData<(Q, P, Req, S)>,
Expand Down Expand Up @@ -99,7 +99,7 @@ where
}

/// TODO document
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Case<'a, Q, P, S, Req> {
client: &'a mut S,
phantom: PhantomData<(Q, P, S, Req)>,
Expand Down
2 changes: 1 addition & 1 deletion src/implement/service_http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::error::WrappedResult;

pub const APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct DefaultHttpClient<ReqB, ResB> {
client: reqwest::Client,
phantom: PhantomData<(ReqB, ResB)>,
Expand Down
8 changes: 4 additions & 4 deletions src/implement/service_http/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
interface::helper::{coalesce::Coalesce, http_serde_priv, is_default::IsDefault},
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct HttpResponse {
#[serde(default, skip_serializing_if = "IsDefault::is_default")]
Expand All @@ -28,23 +28,23 @@ pub struct HttpResponse {
#[cfg_attr(feature = "yaml", serde(with = "serde_yaml::with::singleton_map_recursive"))]
pub body: BodyEvaluate,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub enum StatusEvaluate {
#[default]
OkOrEqual,
Expect(AllOr<http_serde_priv::StatusCode>),
Ignore,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub enum HeaderEvaluate {
#[default]
AnyOrEqual,
Expect(AllOr<http_serde_priv::HeaderMap>),
Ignore,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub enum BodyEvaluate {
#[default]
Expand Down
4 changes: 2 additions & 2 deletions src/implement/service_http/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
},
};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
pub struct HttpRequest {
#[serde(default, skip_serializing_if = "IsDefault::is_default")]
Expand All @@ -30,7 +30,7 @@ pub struct HttpRequest {
#[serde(default, skip_serializing_if = "IsDefault::is_default")]
pub body: HttpBody,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "kebab-case", untagged)]
pub enum HttpBody {
#[default]
Expand Down
4 changes: 2 additions & 2 deletions src/interface/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn execute() -> Result<ExitCode, Box<dyn std::error::Error + Send + Sy
Ok(cmd.exit_code(&rep))
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "cli", derive(Parser))]
#[cfg_attr(feature = "cli", clap(version, about, arg_required_else_help = true))]
pub struct Relentless {
Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct Relentless {
pub rps: Option<usize>,
}
#[cfg_attr(feature = "cli", derive(ValueEnum))]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
pub enum ReportFormat {
/// without report
#[cfg_attr(not(feature = "console-report"), default)]
Expand Down
Loading
Loading