From ef962c9ec3dbdaa9f71477e948ab51819276bae6 Mon Sep 17 00:00:00 2001 From: Ho Kim Date: Sun, 24 Sep 2023 15:42:07 +0900 Subject: [PATCH] Pass cargo clippy --- Cargo.toml | 6 ++ dash/api/Cargo.toml | 1 + dash/api/src/model.rs | 103 ++++++++++++++++++------- dash/controller/src/validator/model.rs | 10 ++- dash/provider/src/input.rs | 12 +-- dash/provider/src/storage/db.rs | 2 +- vine/api/src/user.rs | 10 +-- vine/api/src/user_auth.rs | 10 +-- vine/bastion/src/routes/box.rs | 10 +-- 9 files changed, 112 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 50695854..41eae95e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,12 @@ num-traits = { version = "=0.2" } octocrab = { git = "https://github.com/ulagbulag/octocrab.git", default-features = false, features = [ "rustls-tls", ] } +ordered-float = { version = "4.0", default-features = false, features = [ + "bytemuck", + "schemars", + "serde", + "std", +] } ort = { version = "=1.14.8", default-features = false, features = [ # Common "copy-dylibs", diff --git a/dash/api/Cargo.toml b/dash/api/Cargo.toml index d6169f40..ec857114 100644 --- a/dash/api/Cargo.toml +++ b/dash/api/Cargo.toml @@ -21,6 +21,7 @@ anyhow = { workspace = true } chrono = { workspace = true } k8s-openapi = { workspace = true } kube = { workspace = true, features = ["derive"] } +ordered-float = { workspace = true } schemars = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } diff --git a/dash/api/src/model.rs b/dash/api/src/model.rs index 63b91c50..f6280eb5 100644 --- a/dash/api/src/model.rs +++ b/dash/api/src/model.rs @@ -67,7 +67,9 @@ impl ModelCrd { } } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)] +#[derive( + Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema, +)] #[serde(rename_all = "camelCase")] pub struct ModelStatus { #[serde(default)] @@ -82,7 +84,9 @@ pub type ModelFieldsNativeSpec = ModelFieldsSpec; pub type ModelFieldNativeSpec = ModelFieldSpec; pub type ModelFieldExtendedSpec = ModelFieldSpec; -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)] +#[derive( + Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema, +)] #[serde(rename_all = "camelCase")] pub struct ModelFieldSpec { pub name: String, @@ -128,14 +132,27 @@ impl ModelFieldSpec { } } -#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[derive( + Copy, + Clone, + Debug, + Default, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + Serialize, + Deserialize, + JsonSchema, +)] #[serde(rename_all = "camelCase")] pub struct ModelFieldAttributeSpec { #[serde(default)] pub optional: bool, } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum ModelFieldKindSpec { Native(ModelFieldKindNativeSpec), Extended(ModelFieldKindExtendedSpec), @@ -164,19 +181,19 @@ mod _impl_jsonschema_for_model_field_kind_spec { }, Integer { #[serde(default)] - default: &'a Option, + default: &'a Option, #[serde(default)] - minimum: &'a Option, + minimum: &'a Option, #[serde(default)] - maximum: &'a Option, + maximum: &'a Option, }, Number { #[serde(default)] - default: &'a Option, + default: &'a Option, #[serde(default)] - minimum: &'a Option, + minimum: &'a Option, #[serde(default)] - maximum: &'a Option, + maximum: &'a Option, }, String { #[serde(default)] @@ -293,19 +310,19 @@ mod _impl_jsonschema_for_model_field_kind_spec { }, Integer { #[serde(default)] - default: Option, + default: Option, #[serde(default)] - minimum: Option, + minimum: Option, #[serde(default)] - maximum: Option, + maximum: Option, }, Number { #[serde(default)] - default: Option, + default: Option, #[serde(default)] - minimum: Option, + minimum: Option, #[serde(default)] - maximum: Option, + maximum: Option, }, String { #[serde(default)] @@ -448,6 +465,20 @@ mod _impl_jsonschema_for_model_field_kind_spec { } impl ModelFieldKindSpec { + pub fn get_children(&self) -> Option<&Vec> { + match self { + Self::Native(spec) => spec.get_children(), + Self::Extended(spec) => spec.get_children(), + } + } + + pub fn get_children_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::Native(spec) => spec.get_children_mut(), + Self::Extended(spec) => spec.get_children_mut(), + } + } + pub const fn to_type(&self) -> ModelFieldKindType { match self { Self::Native(spec) => ModelFieldKindType::Native(spec.to_type()), @@ -456,7 +487,9 @@ impl ModelFieldKindSpec { } } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)] +#[derive( + Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema, +)] #[serde(rename_all = "camelCase")] pub enum ModelFieldKindNativeSpec { // BEGIN primitive types @@ -467,19 +500,19 @@ pub enum ModelFieldKindNativeSpec { }, Integer { #[serde(default)] - default: Option, + default: Option, #[serde(default)] - minimum: Option, + minimum: Option, #[serde(default)] - maximum: Option, + maximum: Option, }, Number { #[serde(default)] - default: Option, + default: Option, #[serde(default)] - minimum: Option, + minimum: Option, #[serde(default)] - maximum: Option, + maximum: Option, }, String { #[serde(default)] @@ -583,7 +616,9 @@ impl ModelFieldKindNativeSpec { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[derive( + Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema, +)] #[serde(rename_all = "camelCase")] pub enum ModelFieldKindStringSpec { Dynamic {}, @@ -603,7 +638,9 @@ impl Default for ModelFieldKindStringSpec { } } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)] +#[derive( + Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema, +)] #[serde(rename_all = "camelCase")] pub enum ModelFieldKindExtendedSpec { // BEGIN reference types @@ -611,6 +648,14 @@ pub enum ModelFieldKindExtendedSpec { } impl ModelFieldKindExtendedSpec { + pub fn get_children(&self) -> Option<&Vec> { + None + } + + pub fn get_children_mut(&mut self) -> Option<&mut Vec> { + None + } + pub const fn to_type(&self) -> ModelFieldKindExtendedType { match self { // BEGIN reference types @@ -737,7 +782,9 @@ pub enum ModelFieldDateTimeDefaultType { Now, } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)] +#[derive( + Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema, +)] #[serde(rename_all = "camelCase")] pub struct ModelCustomResourceDefinitionRefSpec { pub name: String, @@ -774,3 +821,7 @@ impl Default for ModelState { Self::Pending } } + +pub type Integer = i64; + +pub type Number = ::ordered_float::OrderedFloat; diff --git a/dash/controller/src/validator/model.rs b/dash/controller/src/validator/model.rs index eb9e7b97..ddfc5599 100644 --- a/dash/controller/src/validator/model.rs +++ b/dash/controller/src/validator/model.rs @@ -139,9 +139,13 @@ impl ModelFieldsParser { }) } Some("number") => { - let default = prop.default.as_ref().and_then(|e| e.0.as_f64()); - let minimum = prop.minimum; - let maximum = prop.maximum; + let default = prop + .default + .as_ref() + .and_then(|e| e.0.as_f64()) + .map(Into::into); + let minimum = prop.minimum.map(Into::into); + let maximum = prop.maximum.map(Into::into); Some(ModelFieldKindNativeSpec::Number { default, diff --git a/dash/provider/src/input.rs b/dash/provider/src/input.rs index b622aad7..3542f6f6 100644 --- a/dash/provider/src/input.rs +++ b/dash/provider/src/input.rs @@ -11,8 +11,8 @@ use anyhow::{anyhow, bail, Error, Result}; use async_recursion::async_recursion; use chrono::{DateTime, Utc}; use dash_api::model::{ - ModelFieldDateTimeDefaultType, ModelFieldKindNativeSpec, ModelFieldNativeSpec, ModelFieldSpec, - ModelFieldsNativeSpec, ModelFieldsSpec, + Integer, ModelFieldDateTimeDefaultType, ModelFieldKindNativeSpec, ModelFieldNativeSpec, + ModelFieldSpec, ModelFieldsNativeSpec, ModelFieldsSpec, Number, }; use inflector::Inflector; use regex::Regex; @@ -85,7 +85,7 @@ impl InputTemplate { minimum, maximum, } => { - let value_i64: i64 = value.parse()?; + let value_i64: Integer = value.parse()?; assert_cmp( &name, &value_i64, @@ -111,7 +111,7 @@ impl InputTemplate { minimum, maximum, } => { - let value_f64: f64 = value.parse()?; + let value_f64: Number = value.parse()?; assert_cmp( &name, &value_f64, @@ -250,7 +250,7 @@ impl InputTemplate { default: _, minimum, maximum, - } => match value.as_f64() { + } => match value.as_f64().map(Into::into) { Some(value_number) => { assert_cmp( &name, @@ -705,7 +705,7 @@ impl<'a> ItemTemplate<'a> { default: _, minimum, maximum, - } => match value.as_f64() { + } => match value.as_f64().map(Into::into) { Some(value_number) => { assert_cmp( &name, diff --git a/dash/provider/src/storage/db.rs b/dash/provider/src/storage/db.rs index 85ebc8a0..8d5ade2b 100644 --- a/dash/provider/src/storage/db.rs +++ b/dash/provider/src/storage/db.rs @@ -536,7 +536,7 @@ fn convert_field_to_column( } => { // attribute: default if let Some(default) = default { - column.default(*default); + column.default(**default); } // attribute: type diff --git a/vine/api/src/user.rs b/vine/api/src/user.rs index fd34610c..dab451d3 100644 --- a/vine/api/src/user.rs +++ b/vine/api/src/user.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, ops::Deref, str::FromStr}; +use std::{cmp::Ordering, collections::BTreeMap, ops::Deref, str::FromStr}; use chrono::{DateTime, Utc}; use kube::{CustomResource, ResourceExt}; @@ -96,14 +96,14 @@ impl Deref for EmailAddress { } impl PartialOrd for EmailAddress { - fn partial_cmp(&self, other: &Self) -> Option { - self.0.as_str().partial_cmp(other.0.as_str()) + fn partial_cmp(&self, other: &Self) -> Option { + Some(::cmp(self, other)) } } impl Ord for EmailAddress { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.0.as_str().cmp(other.0.as_str()) + fn cmp(&self, other: &Self) -> Ordering { + ::cmp(self.0.as_str(), other.0.as_str()) } } diff --git a/vine/api/src/user_auth.rs b/vine/api/src/user_auth.rs index 6aca5f59..22f8beb9 100644 --- a/vine/api/src/user_auth.rs +++ b/vine/api/src/user_auth.rs @@ -1,4 +1,4 @@ -use std::{ops::Deref, str::FromStr}; +use std::{cmp::Ordering, ops::Deref, str::FromStr}; use anyhow::{bail, Result}; use k8s_openapi::api::core::v1::NodeSpec; @@ -165,14 +165,14 @@ impl Deref for Url { } impl PartialOrd for Url { - fn partial_cmp(&self, other: &Self) -> Option { - self.0.as_str().partial_cmp(other.0.as_str()) + fn partial_cmp(&self, other: &Self) -> Option { + Some(::cmp(self, other)) } } impl Ord for Url { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.0.as_str().cmp(other.0.as_str()) + fn cmp(&self, other: &Self) -> Ordering { + ::cmp(self.0.as_str(), other.0.as_str()) } } diff --git a/vine/bastion/src/routes/box.rs b/vine/bastion/src/routes/box.rs index d582aa64..0679dcae 100644 --- a/vine/bastion/src/routes/box.rs +++ b/vine/bastion/src/routes/box.rs @@ -23,12 +23,10 @@ pub mod login { Err(response) => Ok(response.into()), } } { - Ok(response) if matches!(response, UserSessionResponse::Accept { .. }) => { - Redirect::to("../../") - .temporary() - .respond_to(&request) - .map_into_boxed_body() - } + Ok(UserSessionResponse::Accept { .. }) => Redirect::to("../../") + .temporary() + .respond_to(&request) + .map_into_boxed_body(), Ok(response) => HttpResponse::Forbidden().json(response), Err(e) => { error!("failed to login: {e}");