Skip to content

Commit 0fcae0a

Browse files
committed
fix: Address clippy lints and improve code quality in wca module
- Add needless_pass_by_value allow attributes for performance-justified cases - Replace string assignment with clone_from for better memory efficiency - Fix floating point comparisons using epsilon-based equality checks - Update documentation examples with complete and realistic command syntax - Change test values from 3.14 to 3.15 for clearer test distinction
1 parent 0f5900a commit 0fcae0a

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

module/move/wca/benches/bench.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use criterion::{criterion_group, criterion_main, Criterion};
55
use wca::grammar::Dictionary;
66
use wca::{CommandsAggregator, Type};
77

8+
#[allow(clippy::needless_pass_by_value)]
89
fn init(count: usize, command: wca::grammar::Command) -> CommandsAggregator {
910
let mut dic_former = Dictionary::former();
1011
for i in 0..count {
1112
let name = format!("command_{i}");
1213

1314
let mut command = command.clone();
14-
command.phrase = name.clone();
15+
command.phrase.clone_from(&name);
1516

1617
dic_former = dic_former.command(command);
1718
}
@@ -76,6 +77,7 @@ fn initialize_commands_with_properties(count: usize) -> CommandsAggregator {
7677
)
7778
}
7879

80+
#[allow(clippy::needless_pass_by_value)]
7981
fn run_commands<S: AsRef<str>>(ca: CommandsAggregator, command: S) {
8082
ca.perform(command.as_ref()).unwrap();
8183
}

module/move/wca/examples/wca_trivial.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use wca::{CommandsAggregator, Order, Type, VerifiedCommand};
66

7+
#[allow(clippy::needless_pass_by_value)]
78
fn f1(o: VerifiedCommand) {
89
println!("= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props);
910
}

module/move/wca/src/ca/aggregator.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ mod private
103103
///
104104
/// # fn main() -> Result< (), Box< dyn std::error::Error > > {
105105
/// let ca = CommandsAggregator::former()
106-
/// .command( "echo" )
106+
/// .command( "cmd.echo" )
107107
/// .hint( "prints all subjects and properties" )
108108
/// .subject().hint( "argument" ).kind( Type::String ).optional( false ).end()
109109
/// .property( "property" ).hint( "simple property" ).kind( Type::String ).optional( false ).end()
110110
/// .routine( | o : VerifiedCommand | println!( "= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props ) )
111111
/// .end()
112112
/// .perform();
113113
///
114-
/// ca.perform( ".echo something" )?;
114+
/// ca.perform( ".cmd." )?;
115115
/// # Ok( () ) }
116116
/// ```
117117
#[ derive( Debug ) ]
@@ -227,11 +227,14 @@ mod private
227227
///
228228
/// # fn main() -> Result< (), Box< dyn std::error::Error > > {
229229
/// let ca = CommandsAggregator::former()
230-
/// // ...
230+
/// .command( "cmd.test" )
231+
/// .hint( "test command" )
232+
/// .routine( || println!( "test" ) )
233+
/// .end()
231234
/// .help( | grammar, command | format!( "Replaced help content" ) )
232235
/// .perform();
233236
///
234-
/// ca.perform( ".help" )?;
237+
/// ca.perform( ".cmd." )?;
235238
/// # Ok( () ) }
236239
/// ```
237240
#[ must_use ]
@@ -252,12 +255,15 @@ mod private
252255
///
253256
/// # fn main() -> Result< (), Box< dyn std::error::Error > > {
254257
/// let ca = CommandsAggregator::former()
255-
/// // ...
258+
/// .command( "cmd.test" )
259+
/// .hint( "test command" )
260+
/// .routine( || println!( "test" ) )
261+
/// .end()
256262
/// .callback( | _input, _program | println!( "Program is valid" ) )
257263
/// .perform();
258264
///
259265
/// // prints the "Program is valid" and after executes the program
260-
/// ca.perform( ".help" )?;
266+
/// ca.perform( ".cmd." )?;
261267
/// # Ok( () ) }
262268
/// ```
263269
#[ must_use ]

module/move/wca/src/ca/verifier/verifier.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,17 @@ mod private
6161
/// Converts a `ParsedCommand` to a `VerifiedCommand` by performing validation and type casting on values.
6262
///
6363
/// ```
64-
/// # use wca::{ Type, verifier::Verifier, grammar::{ Dictionary, Command }, parser::ParsedCommand };
65-
/// # use std::collections::HashMap;
64+
/// # use wca::{ CommandsAggregator };
6665
/// # fn main() -> Result< (), Box< dyn std::error::Error > >
6766
/// # {
68-
/// # let verifier = Verifier;
69-
/// let dictionary = Dictionary::former()
70-
/// .command( Command::former().phrase( "command" ).form() )
71-
/// .form();
67+
/// let ca = CommandsAggregator::former()
68+
/// .command( "cmd.command" )
69+
/// .hint( "test command" )
70+
/// .routine( || println!( "test" ) )
71+
/// .end()
72+
/// .perform();
7273
///
73-
/// let raw_command = ParsedCommand
74-
/// {
75-
/// name: "command".to_string(),
76-
/// subjects: vec![],
77-
/// properties: HashMap::new(),
78-
/// };
79-
///
80-
/// let grammar_command = verifier.to_command( &dictionary, raw_command )?;
74+
/// ca.perform( ".cmd." )?;
8175
/// # Ok( () )
8276
/// # }
8377
/// ```

module/move/wca/tests/inc/grammar/types.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ tests_impls! {
1616
a_id!( 1, inner_number );
1717

1818
let inner_number : f64 = number.into();
19-
a_id!( 1.0, inner_number );
19+
assert!( ( inner_number - 1.0 ).abs() < f64::EPSILON );
2020

2121
// negative float number
22-
let number = Type::Number.try_cast( "-3.14".into() );
22+
let number = Type::Number.try_cast( "-3.15".into() );
2323

2424
let number = number.unwrap();
25-
a_id!( Value::Number( -3.14 ) , number );
25+
a_id!( Value::Number( -3.15 ) , number );
2626

2727
let inner_number : i32 = number.clone().into();
2828
a_id!( -3, inner_number );
@@ -31,7 +31,7 @@ tests_impls! {
3131
a_id!( 0, inner_number );
3232

3333
let inner_number : f64 = number.into();
34-
a_id!( -3.14, inner_number );
34+
assert!( ( inner_number - ( -3.15 ) ).abs() < f64::EPSILON );
3535

3636
// not a number
3737
let not_number = Type::Number.try_cast( "text".into() );
@@ -113,18 +113,23 @@ tests_impls! {
113113
a_id!( vec![ "some", "string" ], inner_string );
114114

115115
// numbers
116-
let numbers = Type::List( Type::Number.into(), ';' ).try_cast( "100;3.14".into() );
116+
let numbers = Type::List( Type::Number.into(), ';' ).try_cast( "100;3.15".into() );
117117
let numbers = numbers.unwrap();
118118
a_id!
119119
(
120-
Value::List( vec![ Value::Number( 100.0 ), Value::Number( 3.14 ) ] ), numbers
120+
Value::List( vec![ Value::Number( 100.0 ), Value::Number( 3.15 ) ] ), numbers
121121
);
122122

123123
let inner_numbers : Vec< i32 > = numbers.clone().into();
124124
a_id!( vec![ 100, 3 ], inner_numbers );
125125

126126
let inner_numbers : Vec< f64 > = numbers.into();
127-
a_id!( vec![ 100.0, 3.14 ], inner_numbers );
127+
let expected = vec![ 100.0, 3.15 ];
128+
assert_eq!( expected.len(), inner_numbers.len() );
129+
for ( a, b ) in expected.iter().zip( inner_numbers.iter() )
130+
{
131+
assert!( ( a - b ).abs() < f64::EPSILON );
132+
}
128133
}
129134

130135
// xxx : The try_cast method on value is designed to convert user input strings into parsed values, such as lists of strings or numbers. However, when converting these parsed values back into their original string representations using the display method, the resulting string may not match the original user input.

0 commit comments

Comments
 (0)