From 070e6d91ff05a8b999ef34f0d28080f7991e21df Mon Sep 17 00:00:00 2001 From: Gil Ben-Shachar Date: Sun, 15 Dec 2024 17:07:09 +0200 Subject: [PATCH] Make the cormatter cli use the config defaults. commit-id:8d2ad88e --- corelib/src/circuit.cairo | 10 ++-- crates/bin/cairo-format/src/main.rs | 36 +++++--------- crates/cairo-lang-formatter/src/lib.rs | 48 +++++++++++++++---- crates/cairo-lang-formatter/src/test.rs | 20 +++----- .../abi_dispatchers_tests.cairo | 9 +--- 5 files changed, 62 insertions(+), 61 deletions(-) diff --git a/corelib/src/circuit.cairo b/corelib/src/circuit.cairo index 4fe74c366d1..efe4825d716 100644 --- a/corelib/src/circuit.cairo +++ b/corelib/src/circuit.cairo @@ -288,9 +288,7 @@ impl U96sIntoCircuitInputValue of IntoCircuitInputValue<[u96; 4]> { fn into_circuit_input_value(self: [u96; 4]) -> [U96Guarantee; 4] { let [val0, val1, val2, val3] = self; [ - into_u96_guarantee(val0), - into_u96_guarantee(val1), - into_u96_guarantee(val2), + into_u96_guarantee(val0), into_u96_guarantee(val1), into_u96_guarantee(val2), into_u96_guarantee(val3), ] } @@ -299,10 +297,8 @@ impl U96sIntoCircuitInputValue of IntoCircuitInputValue<[u96; 4]> { impl U384IntoCircuitInputValue of IntoCircuitInputValue { fn into_circuit_input_value(self: u384) -> [U96Guarantee; 4] { [ - into_u96_guarantee(self.limb0), - into_u96_guarantee(self.limb1), - into_u96_guarantee(self.limb2), - into_u96_guarantee(self.limb3), + into_u96_guarantee(self.limb0), into_u96_guarantee(self.limb1), + into_u96_guarantee(self.limb2), into_u96_guarantee(self.limb3), ] } } diff --git a/crates/bin/cairo-format/src/main.rs b/crates/bin/cairo-format/src/main.rs index 85dd4d201ea..11d85f4705c 100644 --- a/crates/bin/cairo-format/src/main.rs +++ b/crates/bin/cairo-format/src/main.rs @@ -3,9 +3,7 @@ use std::path::Path; use std::process::ExitCode; use std::sync::atomic::{AtomicBool, Ordering}; -use cairo_lang_formatter::{ - CairoFormatter, CollectionsBreakingBehavior, FormatOutcome, FormatterConfig, StdinFmt, -}; +use cairo_lang_formatter::{CairoFormatter, FormatOutcome, FormatterConfig, StdinFmt}; use cairo_lang_utils::logging::init_logging; use clap::Parser; use colored::Colorize; @@ -39,24 +37,24 @@ struct FormatterArgs { #[arg(short, long, default_value_t = false)] print_parsing_errors: bool, /// Enable sorting the module level items (imports, mod definitions...). - #[arg(short, long, default_value_t = false)] - sort_mod_level_items: bool, + #[arg(short, long)] + sort_mod_level_items: Option, /// Controls tuple breaking behavior. Set to 'line-by-line' (default) to format each /// tuple item on a new line, or 'single-break-point' to keep as many items as possible on the /// same line (as space permits). Defaults to line-by-line. - #[arg(long, default_value_t = false)] - tuple_line_breaking: bool, + #[arg(long)] + tuple_line_breaking: Option, /// Controls fixed array breaking behavior. Set to 'single-break-point' (default) to format /// each array item on a new line, or 'line-by-line' to keep as many items as possible on the /// same line (as space permits). Defaults to single line. - #[arg(long, default_value_t = true)] - fixed_array_line_breaking: bool, + #[arg(long)] + fixed_array_line_breaking: Option, /// Enable merging of `use` items. - #[arg(long, default_value_t = false)] - merge_use_items: bool, + #[arg(long)] + merge_use_items: Option, /// Enable duplicates in `use` items. - #[arg(long, default_value_t = false)] - allow_duplicates: bool, + #[arg(long)] + allow_duplicates: Option, /// A list of files and directories to format. Use "-" for stdin. files: Vec, } @@ -203,16 +201,8 @@ fn main() -> ExitCode { let args = FormatterArgs::parse(); let config = FormatterConfig::default() .sort_module_level_items(args.sort_mod_level_items) - .tuple_breaking_behavior(if args.tuple_line_breaking { - CollectionsBreakingBehavior::SingleBreakPoint - } else { - CollectionsBreakingBehavior::LineByLine - }) - .fixed_array_breaking_behavior(if args.fixed_array_line_breaking { - CollectionsBreakingBehavior::LineByLine - } else { - CollectionsBreakingBehavior::SingleBreakPoint - }) + .tuple_breaking_behavior(args.tuple_line_breaking.map(Into::into)) + .fixed_array_breaking_behavior(args.fixed_array_line_breaking.map(Into::into)) .merge_use_items(args.merge_use_items) .allow_duplicate_uses(args.allow_duplicates); let fmt = CairoFormatter::new(config); diff --git a/crates/cairo-lang-formatter/src/lib.rs b/crates/cairo-lang-formatter/src/lib.rs index 86b2a327a0b..3e45afa49b8 100644 --- a/crates/cairo-lang-formatter/src/lib.rs +++ b/crates/cairo-lang-formatter/src/lib.rs @@ -69,6 +69,18 @@ pub enum CollectionsBreakingBehavior { LineByLine, } +/// Impl CollectionsBreakingBehavior from bool, where true is `LineByLine` and false is +/// `SingleBreakPoint`. This adheres to the existing behavior of the formatter CLI. +impl From for CollectionsBreakingBehavior { + fn from(b: bool) -> Self { + if b { + CollectionsBreakingBehavior::LineByLine + } else { + CollectionsBreakingBehavior::SingleBreakPoint + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub struct FormatterConfig { @@ -107,26 +119,42 @@ impl FormatterConfig { } } - pub fn sort_module_level_items(mut self, sort_module_level_items: bool) -> Self { - self.sort_module_level_items = sort_module_level_items; + pub fn sort_module_level_items(mut self, sort_module_level_items: Option) -> Self { + if let Some(sort) = sort_module_level_items { + self.sort_module_level_items = sort; + } self } - pub fn tuple_breaking_behavior(mut self, behavior: CollectionsBreakingBehavior) -> Self { - self.tuple_breaking_behavior = behavior; + pub fn tuple_breaking_behavior( + mut self, + behavior: Option, + ) -> Self { + if let Some(behavior) = behavior { + self.tuple_breaking_behavior = behavior; + } self } - pub fn fixed_array_breaking_behavior(mut self, behavior: CollectionsBreakingBehavior) -> Self { - self.fixed_array_breaking_behavior = behavior; + pub fn fixed_array_breaking_behavior( + mut self, + behavior: Option, + ) -> Self { + if let Some(behavior) = behavior { + self.fixed_array_breaking_behavior = behavior; + } self } - pub fn merge_use_items(mut self, merge: bool) -> Self { - self.merge_use_items = merge; + pub fn merge_use_items(mut self, merge: Option) -> Self { + if let Some(merge) = merge { + self.merge_use_items = merge; + } self } - pub fn allow_duplicate_uses(mut self, allow: bool) -> Self { - self.allow_duplicate_uses = allow; + pub fn allow_duplicate_uses(mut self, allow: Option) -> Self { + if let Some(allow) = allow { + self.allow_duplicate_uses = allow; + } self } } diff --git a/crates/cairo-lang-formatter/src/test.rs b/crates/cairo-lang-formatter/src/test.rs index 098ac4ee9d9..8891314c2a9 100644 --- a/crates/cairo-lang-formatter/src/test.rs +++ b/crates/cairo-lang-formatter/src/test.rs @@ -8,7 +8,7 @@ use cairo_lang_utils::Upcast; use pretty_assertions::assert_eq; use test_case::test_case; -use crate::{CollectionsBreakingBehavior, FormatterConfig, get_formatted_file}; +use crate::{FormatterConfig, get_formatted_file}; #[salsa::database(SyntaxDatabase, FilesDatabase)] #[derive(Default)] @@ -146,19 +146,11 @@ fn format_and_compare_file( )); let config = FormatterConfig::default() - .sort_module_level_items(use_sorting) - .tuple_breaking_behavior(if tuple_line_breaking { - CollectionsBreakingBehavior::LineByLine - } else { - CollectionsBreakingBehavior::SingleBreakPoint - }) - .fixed_array_breaking_behavior(if fixed_array_line_breaking { - CollectionsBreakingBehavior::LineByLine - } else { - CollectionsBreakingBehavior::SingleBreakPoint - }) - .merge_use_items(merge_use_statements) - .allow_duplicate_uses(allow_duplicate_uses); + .sort_module_level_items(Some(use_sorting)) + .tuple_breaking_behavior(Some(tuple_line_breaking.into())) + .fixed_array_breaking_behavior(Some(fixed_array_line_breaking.into())) + .merge_use_items(Some(merge_use_statements)) + .allow_duplicate_uses(Some(allow_duplicate_uses)); let formatted_file = get_formatted_file(db, &syntax_root, config); let expected_file = diff --git a/crates/cairo-lang-starknet/cairo_level_tests/abi_dispatchers_tests.cairo b/crates/cairo-lang-starknet/cairo_level_tests/abi_dispatchers_tests.cairo index 0420e9080b4..fd1d33adb92 100644 --- a/crates/cairo-lang-starknet/cairo_level_tests/abi_dispatchers_tests.cairo +++ b/crates/cairo-lang-starknet/cairo_level_tests/abi_dispatchers_tests.cairo @@ -89,15 +89,10 @@ fn test_validate_gas_cost() { to: contract_address, selector: 0x2c0f7bf2d6cf5304c29171bf493feb222fef84bdaf17805a6574b0c2e8bcc87, calldata: [ - 0x4db5d32, - 0x0, - 0x896ba264a31df2, - 0x0, - 0x2, + 0x4db5d32, 0x0, 0x896ba264a31df2, 0x0, 0x2, 0x53c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8, 0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7, - 0x54767f773cc172172c3afc5265bd0a76089c24cdef409635d27ac1a1fa96ca8, - 0x65586264, + 0x54767f773cc172172c3afc5265bd0a76089c24cdef409635d27ac1a1fa96ca8, 0x65586264, ] .span(), },