Skip to content

Commit

Permalink
chore: add autoformatting config (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored Oct 29, 2024
1 parent 443f662 commit 10369a0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/sarif-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
uses: Swatinem/rust-cache@v2
- name: Install required cargo packages for reporting format
run: cargo install clippy-sarif sarif-fmt
- name: Check formatting
run: cargo fmt -- --check
- name: Run rust-clippy
run: |
cargo clippy --all-features --message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
Expand Down
Empty file added rustfmt.toml
Empty file.
5 changes: 3 additions & 2 deletions unleash-yggdrasil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ impl EngineState {
}
let total_weight: u32 = variants.iter().map(|var| var.weight as u32).sum();

let stickiness = variants.first()
let stickiness = variants
.first()
.and_then(|variant| variant.stickiness.clone());

let target = get_seed(stickiness, context)
Expand Down Expand Up @@ -563,7 +564,7 @@ impl EngineState {
self.get_toggle(name).map(|toggle| {
if self.enabled(toggle, context, external_values) {
self.check_variant_by_toggle(toggle, context)
.unwrap_or_default()
.unwrap_or_default()
} else {
VariantDef::default()
}
Expand Down
1 change: 0 additions & 1 deletion unleash-yggdrasil/src/strategy_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ fn rollout_constraint(node: Pairs<Rule>) -> CompileResult<RuleFragment> {

Ok(Box::new(move |context: &Context| {
if let Some(stickiness) = stickiness_resolver(context) {

let group_id = match &group_id {
Some(group_id) => group_id.clone(),
None => context.toggle_name.clone(),
Expand Down
5 changes: 4 additions & 1 deletion unleash-yggdrasil/src/strategy_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,9 @@ mod tests {

assert!(compile_rule(&rule).is_ok());

assert_eq!(rule.as_str(), "user_id in [\"[\\\"123\\\"\",\"\\\"456\\\"\",\"\\\"789\\\"]\"]");
assert_eq!(
rule.as_str(),
"user_id in [\"[\\\"123\\\"\",\"\\\"456\\\"\",\"\\\"789\\\"]\"]"
);
}
}
93 changes: 56 additions & 37 deletions yggdrasilwasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::from_value;
use wasm_bindgen::prelude::*;
use unleash_yggdrasil::{Context, EngineState, ExtendedVariantDef};
use unleash_types::client_features::ClientFeatures;
use std::collections::HashMap;
use unleash_types::client_features::ClientFeatures;
use unleash_yggdrasil::{Context, EngineState, ExtendedVariantDef};
use wasm_bindgen::prelude::*;

type CustomStrategyResults = HashMap<String, bool>;

Expand Down Expand Up @@ -70,7 +70,12 @@ impl Engine {
}

#[wasm_bindgen(js_name = checkEnabled)]
pub fn check_enabled(&self, toggle_name: &str, context: JsValue, custom_strategy_results: JsValue) -> JsValue {
pub fn check_enabled(
&self,
toggle_name: &str,
context: JsValue,
custom_strategy_results: JsValue,
) -> JsValue {
let context: Context = match from_value(context) {
Ok(ctx) => ctx,
Err(e) => {
Expand All @@ -83,23 +88,26 @@ impl Engine {
}
};

let custom_strategy_results: Option<CustomStrategyResults> = if custom_strategy_results.is_null() || custom_strategy_results.is_undefined() {
None
} else {
match from_value(custom_strategy_results) {
Ok(results) => Some(results),
Err(e) => {
let response = Response::<bool> {
status_code: ResponseCode::Error,
value: None,
error_message: Some(format!("Invalid strategy results: {}", e)),
};
return serde_wasm_bindgen::to_value(&response).unwrap();
let custom_strategy_results: Option<CustomStrategyResults> =
if custom_strategy_results.is_null() || custom_strategy_results.is_undefined() {
None
} else {
match from_value(custom_strategy_results) {
Ok(results) => Some(results),
Err(e) => {
let response = Response::<bool> {
status_code: ResponseCode::Error,
value: None,
error_message: Some(format!("Invalid strategy results: {}", e)),
};
return serde_wasm_bindgen::to_value(&response).unwrap();
}
}
}
};
};

let check_enabled = self.engine.check_enabled(toggle_name, &context, &custom_strategy_results);
let check_enabled =
self.engine
.check_enabled(toggle_name, &context, &custom_strategy_results);

let response = Response {
status_code: ResponseCode::Ok,
Expand All @@ -111,7 +119,12 @@ impl Engine {
}

#[wasm_bindgen(js_name = checkVariant)]
pub fn check_variant(&self, toggle_name: &str, context: JsValue, custom_strategy_results: JsValue) -> JsValue {
pub fn check_variant(
&self,
toggle_name: &str,
context: JsValue,
custom_strategy_results: JsValue,
) -> JsValue {
let context: Context = match from_value(context) {
Ok(ctx) => ctx,
Err(e) => {
Expand All @@ -124,26 +137,32 @@ impl Engine {
}
};

let custom_strategy_results: Option<CustomStrategyResults> = if custom_strategy_results.is_null() || custom_strategy_results.is_undefined() {
None
} else {
match from_value(custom_strategy_results) {
Ok(results) => Some(results),
Err(e) => {
let response = Response::<ExtendedVariantDef> {
status_code: ResponseCode::Error,
value: None,
error_message: Some(format!("Invalid strategy results: {}", e)),
};
return serde_wasm_bindgen::to_value(&response).unwrap();
let custom_strategy_results: Option<CustomStrategyResults> =
if custom_strategy_results.is_null() || custom_strategy_results.is_undefined() {
None
} else {
match from_value(custom_strategy_results) {
Ok(results) => Some(results),
Err(e) => {
let response = Response::<ExtendedVariantDef> {
status_code: ResponseCode::Error,
value: None,
error_message: Some(format!("Invalid strategy results: {}", e)),
};
return serde_wasm_bindgen::to_value(&response).unwrap();
}
}
}
};
};

let base_variant = self.engine.check_variant(toggle_name, &context, &custom_strategy_results);
let toggle_enabled = self.engine.is_enabled(toggle_name, &context, &custom_strategy_results);
let base_variant =
self.engine
.check_variant(toggle_name, &context, &custom_strategy_results);
let toggle_enabled =
self.engine
.is_enabled(toggle_name, &context, &custom_strategy_results);

let enriched_variant = base_variant.map(|variant| variant.to_enriched_response(toggle_enabled));
let enriched_variant =
base_variant.map(|variant| variant.to_enriched_response(toggle_enabled));

let response = Response {
status_code: ResponseCode::Ok,
Expand Down

0 comments on commit 10369a0

Please sign in to comment.