Skip to content

Commit

Permalink
fix: Fixed clippy warnings.
Browse files Browse the repository at this point in the history
ci: Added lint in ci.
  • Loading branch information
Shrey Bana committed Jan 22, 2025
1 parent b36eb2e commit 1aef89d
Show file tree
Hide file tree
Showing 57 changed files with 209 additions and 237 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci_check_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ jobs:
targets: wasm32-unknown-unknown
components: rustfmt, clippy

- name: Check formatting
- name: Check formatting & linting
shell: bash
run: cargo fmt --all --check
run: make check

- name: install cocogitto
uses: baptiste0928/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions crates/cac_client/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ pub extern "C" fn cac_get_default_config(
unwrap_safe!(
serde_json::to_string::<Map<String, Value>>(&ov)
.map(|overrides| rstring_to_cstring(overrides).into_raw()),
return std::ptr::null()
std::ptr::null()
)
})
},
return std::ptr::null()
std::ptr::null()
)
})
}
2 changes: 1 addition & 1 deletion crates/cac_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Client {
}

pub async fn get_last_modified(&self) -> DateTime<Utc> {
self.last_modified.read().await.clone()
*self.last_modified.read().await
}

pub async fn get_resolved_config(
Expand Down
14 changes: 7 additions & 7 deletions crates/context_aware_config/src/api/config/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ use crate::{
use super::helpers::apply_prefix_filter_to_config;

pub fn endpoints() -> Scope {
let scope = Scope::new("")
#[cfg(feature = "high-performance-mode")]
let scope = scope.service(get_config_fast);
Scope::new("")
.service(get_config)
.service(get_resolved_config)
.service(reduce_config)
.service(get_config_versions);
#[cfg(feature = "high-performance-mode")]
let scope = scope.service(get_config_fast);
scope
.service(get_config_versions)
}

fn validate_version_in_params(
Expand Down Expand Up @@ -159,7 +158,7 @@ pub fn generate_config_from_version(
tenant: &Tenant,
) -> superposition::Result<Config> {
if let Some(val) = version {
let val = val.clone();
let val = *val;
let config = config_versions::config_versions
.select(config_versions::config)
.filter(config_versions::id.eq(val))
Expand Down Expand Up @@ -316,6 +315,7 @@ fn reduce(
Ok(dimensions)
}

#[allow(clippy::type_complexity)]
fn get_contextids_from_overrideid(
contexts: Vec<Context>,
overrides: Map<String, Value>,
Expand Down Expand Up @@ -503,7 +503,7 @@ async fn reduce_config_key(
false,
&user,
&tenant,
&tenant_config,
tenant_config,
false,
);
}
Expand Down
3 changes: 2 additions & 1 deletion crates/context_aware_config/src/api/context/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ async fn update_override_handler(
Ok(http_resp.json(override_resp))
}

#[allow(clippy::too_many_arguments)]
#[put("/move/{ctx_id}")]
async fn move_handler(
state: Data<AppState>,
Expand Down Expand Up @@ -619,7 +620,7 @@ async fn weight_recompute(
diesel::update(contexts.filter(id.eq(context_id)))
.set((
weight.eq(context_weight),
last_modified_at.eq(last_modified_time.clone()),
last_modified_at.eq(last_modified_time),
last_modified_by.eq(user.get_email())
))
.schema_name(&tenant)
Expand Down
4 changes: 2 additions & 2 deletions crates/context_aware_config/src/api/context/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub fn create_ctx_from_put_req(
let ctx_override = Value::Object(r_override.clone().into());
let description = if req.description.is_none() {
let ctx_condition_value = json!(ctx_condition);
ensure_description(ctx_condition_value, conn, &tenant)?
ensure_description(ctx_condition_value, conn, tenant)?
} else {
req.description
.clone()
Expand Down Expand Up @@ -310,7 +310,7 @@ pub fn update_override_of_existing_ctx(
let mut new_override: Value = dsl::contexts
.filter(dsl::id.eq(ctx.id.clone()))
.select(dsl::override_)
.schema_name(&tenant)
.schema_name(tenant)
.first(conn)?;
cac_client::merge(
&mut new_override,
Expand Down
16 changes: 8 additions & 8 deletions crates/context_aware_config/src/api/context/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ pub fn put(
replace: bool,
) -> result::Result<PutResp> {
use contexts::dsl::contexts;
let new_ctx = create_ctx_from_put_req(req, conn, user, tenant_config, &tenant)?;
let new_ctx = create_ctx_from_put_req(req, conn, user, tenant_config, tenant)?;

if already_under_txn {
diesel::sql_query("SAVEPOINT put_ctx_savepoint").execute(conn)?;
}
let insert = diesel::insert_into(contexts)
.values(&new_ctx)
.returning(Context::as_returning())
.schema_name(&tenant)
.schema_name(tenant)
.execute(conn);

match insert {
Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn r#move(

let new_ctx_id = hash(&ctx_condition_value);

let dimension_data = get_dimension_data(conn, &tenant)?;
let dimension_data = get_dimension_data(conn, tenant)?;
let dimension_data_map = get_dimension_data_map(&dimension_data)?;
validate_dimensions("context", &ctx_condition_value, &dimension_data_map)?;
let weight = calculate_context_weight(&ctx_condition_value, &dimension_data_map)
Expand All @@ -121,7 +121,7 @@ pub fn r#move(
dsl::last_modified_by.eq(user.get_email()),
))
.returning(Context::as_returning())
.schema_name(&tenant)
.schema_name(tenant)
.get_result::<Context>(conn);

let contruct_new_ctx_with_old_overrides = |ctx: Context| Context {
Expand All @@ -143,7 +143,7 @@ pub fn r#move(
if already_under_txn {
let deleted_ctxt = diesel::delete(dsl::contexts)
.filter(dsl::id.eq(&old_ctx_id))
.schema_name(&tenant)
.schema_name(tenant)
.get_result(db_conn)?;

let ctx = contruct_new_ctx_with_old_overrides(deleted_ctxt);
Expand All @@ -152,7 +152,7 @@ pub fn r#move(
db_conn.transaction(|conn| {
let deleted_ctxt = diesel::delete(dsl::contexts)
.filter(dsl::id.eq(&old_ctx_id))
.schema_name(&tenant)
.schema_name(tenant)
.get_result(conn)?;
let ctx = contruct_new_ctx_with_old_overrides(deleted_ctxt);
update_override_of_existing_ctx(conn, ctx, user, tenant)
Expand Down Expand Up @@ -189,10 +189,10 @@ pub fn delete(
dsl::last_modified_by.eq(user.get_email()),
))
.returning(Context::as_returning())
.schema_name(&tenant)
.schema_name(tenant)
.execute(conn)?;
let deleted_row = diesel::delete(dsl::contexts.filter(dsl::id.eq(&ctx_id)))
.schema_name(&tenant)
.schema_name(tenant)
.execute(conn);
match deleted_row {
Ok(0) => Err(not_found!("Context Id `{}` doesn't exists", ctx_id)),
Expand Down
2 changes: 1 addition & 1 deletion crates/context_aware_config/src/api/context/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
.expect("Invalid context override");

let expected_action = ContextAction::Put(PutReq {
context: context,
context,
r#override: override_,
description: Some("".to_string()),
change_reason: "".to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ async fn update_default_config(
return Err(validation_error!(
"Schema validation failed: {}",
validation_err_to_str(verrors)
.get(0)
.first()
.unwrap_or(&String::new())
));
}
Expand Down Expand Up @@ -287,7 +287,7 @@ fn validate_and_get_function_code(
tenant: &Tenant,
) -> superposition::Result<()> {
if let Some(f_name) = function_name {
let function_code = get_published_function_code(conn, f_name.clone(), &tenant)
let function_code = get_published_function_code(conn, f_name.clone(), tenant)
.map_err(|_| bad_argument!("Function {} doesn't exist.", f_name))?;
if let Some(f_code) = function_code {
validate_value_with_function(
Expand Down
7 changes: 4 additions & 3 deletions crates/context_aware_config/src/api/default_config/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ impl<'de> Deserialize<'de> for FunctionNameEnum {
Value::Null => Ok(Self::Remove),
_ => {
log::error!("Expected a string or null literal as the function name.");
Err("Expected a string or null literal as the function name.")
.map_err(serde::de::Error::custom)
Err(serde::de::Error::custom(
"Expected a string or null literal as the function name.",
))
}
}
}
Expand All @@ -62,7 +63,7 @@ impl DefaultConfigKey {
impl TryFrom<String> for DefaultConfigKey {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(Self::validate_data(value)?)
Self::validate_data(value)
}
}

Expand Down
7 changes: 3 additions & 4 deletions crates/context_aware_config/src/api/dimension/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async fn create(
log::error!("{fun_name:?} function not found with error: {e:?}");
Err(bad_argument!(
"Function {} doesn't exists",
fun_name.unwrap_or(String::new())
fun_name.unwrap_or_default()
))
}
Err(e) => {
Expand Down Expand Up @@ -164,9 +164,8 @@ async fn update(
}

dimension_row.change_reason = update_req.change_reason;
dimension_row.description = update_req
.description
.unwrap_or_else(|| dimension_row.description);
dimension_row.description =
update_req.description.unwrap_or(dimension_row.description);

dimension_row.function_name = match update_req.function_name {
Some(FunctionNameEnum::Name(func_name)) => Some(func_name),
Expand Down
18 changes: 7 additions & 11 deletions crates/context_aware_config/src/api/dimension/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ pub struct CreateReq {

#[derive(Debug, Deserialize, AsRef, Deref, DerefMut, Into, Clone)]
#[serde(try_from = "i32")]
#[derive(Default)]
pub struct Position(i32);
impl Position {
fn validate_data(position_val: i32) -> Result<Self, String> {
if position_val < 0 {
return Err("Position should be greater than equal to 0".to_string());
Err("Position should be greater than equal to 0".to_string())
} else {
Ok(Self(position_val))
}
Expand All @@ -29,13 +30,7 @@ impl Position {
impl TryFrom<i32> for Position {
type Error = String;
fn try_from(value: i32) -> Result<Self, Self::Error> {
Ok(Self::validate_data(value)?)
}
}

impl Default for Position {
fn default() -> Self {
Position(0)
Self::validate_data(value)
}
}

Expand Down Expand Up @@ -65,8 +60,9 @@ impl<'de> Deserialize<'de> for FunctionNameEnum {
Value::Null => Ok(Self::Remove),
_ => {
log::error!("Expected a string or null literal as the function name.");
Err("Expected a string or null literal as the function name.")
.map_err(serde::de::Error::custom)
Err(serde::de::Error::custom(
"Expected a string or null literal as the function name.",
))
}
}
}
Expand All @@ -87,7 +83,7 @@ impl DimensionName {
impl TryFrom<String> for DimensionName {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(Self::validate_data(value)?)
Self::validate_data(value)
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/context_aware_config/src/api/dimension/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ pub fn get_dimension_data(
}

pub fn get_dimension_data_map(
dimensions_vec: &Vec<Dimension>,
dimensions_vec: &[Dimension],
) -> superposition::Result<HashMap<String, DimensionData>> {
let dimension_schema_map = dimensions_vec
.into_iter()
.iter()
.filter_map(|item| {
let compiled_schema = JSONSchema::options()
.with_draft(Draft::Draft7)
Expand Down Expand Up @@ -65,9 +65,9 @@ pub fn get_dimension_usage_context_ids(
})?
.into_inner();

extract_dimensions(&condition)?
.get(key)
.map(|_| context_ids.push(context.id.to_owned()));
if extract_dimensions(&condition)?.get(key).is_some() {
context_ids.push(context.id.to_owned())
}
}
Ok(context_ids)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/context_aware_config/src/api/functions/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl FunctionName {
impl TryFrom<String> for FunctionName {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(Self::validate_data(value)?)
Self::validate_data(value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ async fn update_type(

let description = request.description;
let type_name: String = path.into_inner().into();
let final_description = if description.is_none() {
let final_description = if let Some(description) = description {
description
} else {
let existing_template = type_templates::table
.filter(type_templates::type_name.eq(&type_name))
.schema_name(&tenant)
Expand All @@ -110,8 +112,6 @@ async fn update_type(
));
}
}
} else {
description.unwrap().to_string()
};
let change_reason = request.change_reason;
let timestamp = Utc::now().naive_utc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ impl TypeTemplateName {
impl TryFrom<String> for TypeTemplateName {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(Self::validate_data(value)?)
Self::validate_data(value)
}
}
4 changes: 2 additions & 2 deletions crates/context_aware_config/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub fn calculate_context_weight(
log::error!("{}", msg);
msg
})?;
weight = weight + calculate_weight_from_index(position as u32)?;
weight += calculate_weight_from_index(position as u32)?;
}
Ok(weight)
}
Expand Down Expand Up @@ -268,7 +268,7 @@ pub fn generate_cac(

let default_config_vec = def_conf::default_configs
.select((def_conf::key, def_conf::value))
.schema_name(&tenant)
.schema_name(tenant)
.load::<(String, Value)>(conn)
.map_err(|err| {
log::error!("failed to fetch default_configs with error: {}", err);
Expand Down
Loading

0 comments on commit 1aef89d

Please sign in to comment.