Skip to content

Commit 04bb8da

Browse files
committed
awk: #[cfg_attr(test, derive(Debug))] and some clippy
1 parent 199f206 commit 04bb8da

File tree

8 files changed

+68
-44
lines changed

8 files changed

+68
-44
lines changed

awk/src/compiler.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ fn normalize_builtin_function_arguments(
343343
}
344344
}
345345

346-
#[derive(Debug, PartialEq, Eq)]
346+
#[cfg_attr(test, derive(Debug))]
347+
#[derive(PartialEq, Eq)]
347348
enum ExprKind {
348349
LValue,
349350
Number,
@@ -373,7 +374,8 @@ pub enum GlobalName {
373374
type NameMap = HashMap<String, GlobalName>;
374375
type LocalMap = HashMap<String, VarId>;
375376

376-
#[derive(Debug, Default)]
377+
#[cfg_attr(test, derive(Debug))]
378+
#[derive(Default)]
377379
struct Instructions {
378380
opcodes: Vec<OpCode>,
379381
source_locations: Vec<SourceLocation>,
@@ -1702,7 +1704,8 @@ fn location_end(loc: &InputLocation) -> usize {
17021704
}
17031705
}
17041706

1705-
#[derive(Debug, Clone)]
1707+
#[cfg_attr(test, derive(Debug))]
1708+
#[derive(Clone)]
17061709
pub struct CompilerErrors {
17071710
errors: Vec<PestError>,
17081711
}

awk/src/interpreter/array.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ use std::{
1414

1515
use super::AwkValue;
1616

17-
#[derive(Debug, Clone, PartialEq)]
17+
#[cfg_attr(test, derive(Debug))]
18+
#[derive(Clone, PartialEq)]
1819
pub struct KeyIterator {
1920
index: usize,
2021
}
2122

2223
pub type Key = Rc<str>;
2324

24-
#[derive(Debug, Clone, Copy, PartialEq)]
25+
#[cfg_attr(test, derive(Debug))]
26+
#[derive(Clone, Copy, PartialEq)]
2527
pub struct ValueIndex {
2628
index: usize,
2729
}
2830

2931
pub type KeyValuePair = (Key, AwkValue);
3032

31-
#[derive(Debug, Clone, PartialEq, Default)]
33+
#[cfg_attr(test, derive(Debug))]
34+
#[derive(Clone, PartialEq, Default)]
3235
pub struct Array {
3336
key_map: HashMap<Key, usize>,
3437
pairs: Vec<Option<KeyValuePair>>,

awk/src/interpreter/format.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ pub fn parse_conversion_specifier_args(iter: &mut Chars) -> Result<(char, Format
314314
Ok((next, result))
315315
}
316316

317-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
317+
#[cfg_attr(test, derive(Debug))]
318+
#[derive(PartialEq, Eq, Clone, Copy)]
318319
pub enum IntegerFormat {
319320
Decimal,
320321
Octal,

awk/src/interpreter/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,8 @@ fn is_valid_record_index(index: usize) -> Result<(), String> {
675675
}
676676
}
677677

678-
#[derive(Debug, Clone, PartialEq)]
678+
#[cfg_attr(test, derive(Debug))]
679+
#[derive(Clone, PartialEq)]
679680
enum AwkValueVariant {
680681
Number(f64),
681682
String(AwkString),
@@ -688,14 +689,16 @@ enum AwkValueVariant {
688689
UninitializedScalar,
689690
}
690691

691-
#[derive(Debug, Clone, Copy, PartialEq)]
692+
#[cfg_attr(test, derive(Debug))]
693+
#[derive(Clone, Copy, PartialEq)]
692694
enum AwkRefType {
693695
None,
694696
Field(u16),
695697
SpecialGlobalVar(SpecialVar),
696698
}
697699

698-
#[derive(Debug, Clone, PartialEq)]
700+
#[cfg_attr(test, derive(Debug))]
701+
#[derive(Clone, PartialEq)]
699702
struct AwkValue {
700703
value: AwkValueVariant,
701704
ref_type: AwkRefType,
@@ -889,14 +892,16 @@ impl From<Array> for AwkValue {
889892
}
890893
}
891894

892-
#[derive(Debug, Clone, PartialEq)]
895+
#[cfg_attr(test, derive(Debug))]
896+
#[derive(Clone, PartialEq)]
893897
struct ArrayIterator {
894898
array: *mut AwkValue,
895899
iter_var: *mut AwkValue,
896900
key_iter: KeyIterator,
897901
}
898902

899-
#[derive(Debug, Clone, PartialEq)]
903+
#[cfg_attr(test, derive(Debug))]
904+
#[derive(Clone, PartialEq)]
900905
struct ArrayElementRef {
901906
array: *mut AwkValue,
902907
value_index: ValueIndex,

awk/src/interpreter/string.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
use core::fmt;
1111
use std::{ffi::CString, ops::Deref, rc::Rc};
1212

13-
#[derive(Debug, Clone, PartialEq)]
13+
#[cfg_attr(test, derive(Debug))]
14+
#[derive(Clone, PartialEq)]
1415
enum AwkStringVariant {
1516
Owned(String),
1617
Shared(Rc<str>),
1718
}
1819

19-
#[derive(Debug, Clone)]
20+
#[cfg_attr(test, derive(Debug))]
21+
#[derive(Clone)]
2022
pub struct AwkString {
2123
value: AwkStringVariant,
2224
pub is_numeric: bool,

awk/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn exit_if_error<T, U: Display>(r: Result<T, U>) -> T {
4949
match r {
5050
Ok(v) => v,
5151
Err(err) => {
52-
eprintln!("{}", err);
52+
eprintln!("{err}");
5353
std::process::exit(1);
5454
}
5555
}
@@ -65,10 +65,10 @@ fn main() -> Result<(), Box<dyn Error>> {
6565
let mut sources = Vec::new();
6666
for source_file in &args.program_files {
6767
let mut file = std::fs::File::open(source_file)
68-
.map_err(|_| format!("could not open file '{}'", source_file))?;
68+
.map_err(|_| gettext!("could not open file '{}'", source_file))?;
6969
let mut contents = String::new();
7070
file.read_to_string(&mut contents)
71-
.map_err(|_| format!("could not read file '{}'", source_file))?;
71+
.map_err(|_| gettext!("could not read file '{}'", source_file))?;
7272
sources.push(SourceFile {
7373
contents,
7474
filename: source_file.clone(),
@@ -92,7 +92,7 @@ fn main() -> Result<(), Box<dyn Error>> {
9292
args.separator_string,
9393
))
9494
} else {
95-
eprintln!("missing program argument");
95+
eprintln!("{}", gettext("missing program argument"));
9696
1
9797
};
9898
std::process::exit(return_status);

awk/src/program.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
//
99

1010
use crate::regex::Regex;
11-
use core::fmt;
1211
use std::{collections::HashMap, rc::Rc};
1312

1413
pub type VarId = u32;
1514

16-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
15+
#[cfg_attr(test, derive(Debug))]
16+
#[derive(PartialEq, Eq, Clone, Copy)]
1717
pub enum OpCode {
1818
// binary operations
1919
Add,
@@ -108,7 +108,8 @@ pub enum OpCode {
108108
Invalid,
109109
}
110110

111-
#[derive(Clone, Debug, PartialEq)]
111+
#[cfg_attr(test, derive(Debug))]
112+
#[derive(Clone, PartialEq)]
112113
pub enum Constant {
113114
Number(f64),
114115
String(Rc<str>),
@@ -133,38 +134,44 @@ impl From<Rc<Regex>> for Constant {
133134
}
134135
}
135136

136-
#[derive(Debug, PartialEq, Clone, Copy)]
137+
#[cfg_attr(test, derive(Debug))]
138+
#[derive(PartialEq, Clone, Copy)]
137139
pub struct SourceLocation {
138140
pub line: u32,
139141
pub column: u32,
140142
}
141143

142-
#[derive(Debug, PartialEq, Default)]
144+
#[cfg_attr(test, derive(Debug))]
145+
#[derive(PartialEq, Default)]
143146
pub struct DebugInfo {
144147
pub file: Rc<str>,
145148
pub source_locations: Vec<SourceLocation>,
146149
}
147150

148-
#[derive(Debug, PartialEq)]
151+
#[cfg_attr(test, derive(Debug))]
152+
#[derive(PartialEq)]
149153
pub struct Action {
150154
pub instructions: Vec<OpCode>,
151155
pub debug_info: DebugInfo,
152156
}
153157

154-
#[derive(Debug, PartialEq)]
158+
#[cfg_attr(test, derive(Debug))]
159+
#[derive(PartialEq)]
155160
pub enum Pattern {
156161
Expr(Action),
157162
Range { start: Action, end: Action },
158163
All,
159164
}
160165

161-
#[derive(Debug, PartialEq)]
166+
#[cfg_attr(test, derive(Debug))]
167+
#[derive(PartialEq)]
162168
pub struct AwkRule {
163169
pub pattern: Pattern,
164170
pub action: Action,
165171
}
166172

167-
#[derive(Debug, PartialEq, Default)]
173+
#[cfg_attr(test, derive(Debug))]
174+
#[derive(PartialEq, Default)]
168175
pub struct Function {
169176
pub name: Rc<str>,
170177
pub parameters_count: usize,
@@ -182,13 +189,14 @@ pub struct Program {
182189
pub functions: Vec<Function>,
183190
}
184191

185-
impl fmt::Debug for Program {
186-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
192+
#[cfg(test)]
193+
impl core::fmt::Debug for Program {
194+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
187195
writeln!(f, "Program:")?;
188196
for action in &self.begin_actions {
189197
writeln!(f, " BEGIN {{")?;
190198
for instruction in &action.instructions {
191-
writeln!(f, " {:?}", instruction)?;
199+
writeln!(f, " {instruction:?}")?;
192200
}
193201
writeln!(f, " }}")?;
194202
}
@@ -198,18 +206,18 @@ impl fmt::Debug for Program {
198206
Pattern::Expr(action) => {
199207
writeln!(f, " /")?;
200208
for instruction in &action.instructions {
201-
writeln!(f, " {:?}", instruction)?;
209+
writeln!(f, " {instruction:?}")?;
202210
}
203211
writeln!(f, " / {{")?;
204212
}
205213
Pattern::Range { start, end } => {
206214
writeln!(f, " /")?;
207215
for instruction in &start.instructions {
208-
writeln!(f, " {:?}", instruction)?;
216+
writeln!(f, " {instruction:?}")?;
209217
}
210218
writeln!(f, " /, /")?;
211219
for instruction in &end.instructions {
212-
writeln!(f, " {:?}", instruction)?;
220+
writeln!(f, " {instruction:?}")?;
213221
}
214222
writeln!(f, " / {{")?;
215223
}
@@ -218,36 +226,37 @@ impl fmt::Debug for Program {
218226
}
219227
}
220228
for instruction in &rule.action.instructions {
221-
writeln!(f, " {:?}", instruction)?;
229+
writeln!(f, " {instruction:?}")?;
222230
}
223231
writeln!(f, " }}")?;
224232
}
225233

226234
for action in &self.end_actions {
227235
writeln!(f, " END {{")?;
228236
for instruction in &action.instructions {
229-
writeln!(f, " {:?}", instruction)?;
237+
writeln!(f, " {instruction:?}")?;
230238
}
231239
writeln!(f, " }}")?;
232240
}
233241
writeln!(f, "Functions:")?;
234242
for function in &self.functions {
235243
writeln!(f, " {}({}) {{", function.name, function.parameters_count)?;
236244
for instruction in &function.instructions {
237-
writeln!(f, " {:?}", instruction)?;
245+
writeln!(f, " {instruction:?}")?;
238246
}
239247
writeln!(f, " }}")?;
240248
}
241249
writeln!(f, "Constants:")?;
242250
for (i, constant) in self.constants.iter().enumerate() {
243-
writeln!(f, " {}: {:?}", i, constant)?;
251+
writeln!(f, " {i}: {constant:?}")?;
244252
}
245253
Ok(())
246254
}
247255
}
248256

249257
#[repr(u32)]
250-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
258+
#[cfg_attr(test, derive(Debug))]
259+
#[derive(PartialEq, Eq, Clone, Copy)]
251260
pub enum SpecialVar {
252261
Argc,
253262
Argv,
@@ -271,7 +280,8 @@ pub enum SpecialVar {
271280
}
272281

273282
#[repr(u32)]
274-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
283+
#[cfg_attr(test, derive(Debug))]
284+
#[derive(PartialEq, Eq, Clone, Copy)]
275285
pub enum BuiltinFunction {
276286
// arithmetic functions
277287
Atan2,

awk/src/regex.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
// SPDX-License-Identifier: MIT
88
//
99

10-
use core::fmt;
1110
use std::ffi::CString;
12-
use std::fmt::Formatter;
1311
use std::ptr;
1412

1513
fn regex_compilation_result(
@@ -41,7 +39,8 @@ pub struct Regex {
4139
regex_string: CString,
4240
}
4341

44-
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
42+
#[cfg_attr(test, derive(Debug))]
43+
#[derive(Copy, Clone, Default, PartialEq, Eq)]
4544
pub struct RegexMatch {
4645
pub start: usize,
4746
pub end: usize,
@@ -126,8 +125,9 @@ impl Drop for Regex {
126125
}
127126
}
128127

129-
impl fmt::Debug for Regex {
130-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
128+
#[cfg(test)]
129+
impl core::fmt::Debug for Regex {
130+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
131131
writeln!(f, "/{}/", self.regex_string.to_str().unwrap())
132132
}
133133
}

0 commit comments

Comments
 (0)