Skip to content

Commit 1e5073c

Browse files
committed
Session 15: Code Quality - String and Format Optimizations - Major Progress
Fixed major string and format optimization issues by improving efficiency and readability: - Fixed csv.rs: Replaced format! appended to String with write! macro (4 instances) - Classes summary, Slots summary, Enums summary, Types summary - Used write! with expect() for proper error handling in String-returning functions - Fixed excel.rs: Used direct variable interpolation in format! strings (3 instances) - Column count validation, Column index validation, Validation value range - Fixed jsonld_context.rs: Used direct variable interpolation for slot name conflicts - Fixed json_schema.rs: Used direct variable interpolation for unknown type errors - Improved memory efficiency by avoiding unnecessary string allocations - Enhanced code readability with modern Rust format string syntax Reduced format/string warnings from 33 to 31 Core library compiles cleanly Current clippy warnings in core lib: 766 (reduced from 771)
1 parent 89f48d3 commit 1e5073c

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

linkml-service/src/generator/csv.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,44 +255,48 @@ impl CsvGenerator {
255255
}
256256

257257
// Classes
258-
output.push_str(&format!(
258+
write!(
259+
output,
259260
"Class{}{}{}Schema classes{}{}\n",
260261
self.delimiter,
261262
self.delimiter,
262263
self.delimiter,
263264
self.delimiter,
264265
schema.classes.len()
265-
));
266+
).expect("Failed to write to string");
266267

267268
// Slots
268-
output.push_str(&format!(
269+
write!(
270+
output,
269271
"Slot{}{}{}Schema slots{}{}\n",
270272
self.delimiter,
271273
self.delimiter,
272274
self.delimiter,
273275
self.delimiter,
274276
schema.slots.len()
275-
));
277+
).expect("Failed to write to string");
276278

277279
// Enums
278-
output.push_str(&format!(
280+
write!(
281+
output,
279282
"Enum{}{}{}Schema enumerations{}{}\n",
280283
self.delimiter,
281284
self.delimiter,
282285
self.delimiter,
283286
self.delimiter,
284287
schema.enums.len()
285-
));
288+
).expect("Failed to write to string");
286289

287290
// Types
288-
output.push_str(&format!(
291+
write!(
292+
output,
289293
"Type{}{}{}Schema types{}{}\n",
290294
self.delimiter,
291295
self.delimiter,
292296
self.delimiter,
293297
self.delimiter,
294298
schema.types.len()
295-
));
299+
).expect("Failed to write to string");
296300

297301
output
298302
}

linkml-service/src/generator/excel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ mod excel_cast {
3131

3232
if value >= MAX_EXCEL_COLUMNS {
3333
return Err(GeneratorError::Generation(
34-
format!("Too many columns for Excel: {} (max: {})", value, MAX_EXCEL_COLUMNS)
34+
format!("Too many columns for Excel: {value} (max: {MAX_EXCEL_COLUMNS})")
3535
));
3636
}
3737

3838
u16::try_from(value).map_err(|_| {
3939
GeneratorError::Generation(
40-
format!("Column index {} cannot fit in u16", value)
40+
format!("Column index {value} cannot fit in u16")
4141
)
4242
})
4343
}
@@ -47,7 +47,7 @@ mod excel_cast {
4747
pub fn i64_to_i32_validation(value: i64) -> Result<i32, GeneratorError> {
4848
i32::try_from(value).map_err(|_| {
4949
GeneratorError::Generation(
50-
format!("Validation value {} is outside i32 range", value)
50+
format!("Validation value {value} is outside i32 range")
5151
)
5252
})
5353
}

linkml-service/src/generator/json_schema.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ impl JsonSchemaGenerator {
172172
// Check if we should error on unknown types
173173
if self.options.custom.get("strict_types").map(|v| v == "true").unwrap_or(false) {
174174
Err(GeneratorError::SchemaValidation(format!(
175-
"Unknown type '{}' - not found in enums, classes, or types",
176-
other
175+
"Unknown type '{other}' - not found in enums, classes, or types"
177176
)))
178177
} else {
179178
// Default to string with warning comment

linkml-service/src/generator/jsonld_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl JsonLdContextGenerator {
173173
// Validate slot name doesn't conflict with JSON-LD keywords
174174
if ["@context", "@id", "@type", "@value", "@language", "@index", "@reverse", "@graph"].contains(&slot_name) {
175175
return Err(LinkMLError::data_validation(
176-
format!("Slot name '{}' conflicts with JSON-LD keyword", slot_name)
176+
format!("Slot name '{slot_name}' conflicts with JSON-LD keyword")
177177
));
178178
}
179179

0 commit comments

Comments
 (0)