reinhardt-utils 0.1.0-rc.22

Utility functions aggregator for Reinhardt
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Parameter representation utilities for logging
//! Based on SQLAlchemy's _repr_params functionality

use serde_json::Value;

/// Configuration for parameter representation
pub struct ReprParamsConfig {
	/// Maximum number of characters to display before truncation.
	pub max_chars: usize,
	/// Number of batch entries to show when truncating multi-parameter sets.
	pub batches: usize,
	/// Whether the parameters represent multiple bound parameter sets.
	pub is_multi: bool,
}

impl Default for ReprParamsConfig {
	fn default() -> Self {
		Self {
			max_chars: 300,
			batches: 10,
			is_multi: false,
		}
	}
}
/// Represent parameters in a truncated form suitable for logging
///
/// # Examples
///
/// ```
/// use reinhardt_utils::logging::params::{repr_params, ReprParamsConfig};
/// use serde_json::json;
///
/// // Simple parameter representation
/// let params = json!({"user_id": 42, "email": "test@example.com"});
/// let config = ReprParamsConfig::default();
/// let output = repr_params(&params, &config);
///
/// // Output includes the parameter values
/// assert!(output.contains("user_id"));
/// assert!(output.contains("42"));
/// ```
pub fn repr_params(params: &Value, config: &ReprParamsConfig) -> String {
	match params {
		Value::Array(arr) if config.is_multi && !arr.is_empty() => repr_multi_params(arr, config),
		Value::Array(arr) => repr_array(arr, config),
		Value::Object(obj) => repr_object(obj, config),
		_ => format!("{:?}", params),
	}
}

fn repr_multi_params(params: &[Value], config: &ReprParamsConfig) -> String {
	let total = params.len();

	if total <= config.batches {
		return format!("{:?}", params);
	}

	let show_count = config.batches / 2;
	let first_items: Vec<String> = params
		.iter()
		.take(show_count)
		.map(|v| format!("{:?}", v))
		.collect();
	let last_items: Vec<String> = params
		.iter()
		.rev()
		.take(show_count)
		.map(|v| format!("{:?}", v))
		.collect::<Vec<_>>()
		.into_iter()
		.rev()
		.collect();

	format!(
		"[{}  ... displaying {} of {} total bound parameter sets ...  {}]",
		first_items.join(", "),
		config.batches,
		total,
		last_items.join(", ")
	)
}

// Fixes #802: Find byte index at the n-th character boundary for safe UTF-8 slicing.
// Returns `s.len()` if `n` >= number of characters in `s`.
fn byte_index_at_char(s: &str, n: usize) -> usize {
	s.char_indices().nth(n).map(|(i, _)| i).unwrap_or(s.len())
}

fn repr_array(arr: &[Value], config: &ReprParamsConfig) -> String {
	let repr = format!("{:?}", arr);
	let char_count = repr.chars().count();

	if char_count <= config.max_chars {
		return repr;
	}

	let half_chars = config.max_chars / 2;
	let truncated_chars = char_count - config.max_chars;

	// Fixes #802: Use char_indices() for character-aware slicing
	let start_end = byte_index_at_char(&repr, half_chars);
	let tail_start = byte_index_at_char(&repr, char_count - half_chars);

	format!(
		"{}  ... ({} characters truncated) ...  {}",
		&repr[..start_end],
		truncated_chars,
		&repr[tail_start..]
	)
}

fn repr_object(obj: &serde_json::Map<String, Value>, config: &ReprParamsConfig) -> String {
	let repr = format!("{:?}", obj);
	let char_count = repr.chars().count();

	if char_count <= config.max_chars {
		return repr;
	}

	// For huge dicts, truncate from middle
	if obj.len() > 100 {
		let half_chars = config.max_chars / 2;
		let truncated_params = obj.len() - 10; // Show ~10 params

		// Fixes #802: Use char_indices() for character-aware slicing
		let start_end = byte_index_at_char(&repr, half_chars);
		let tail_start = byte_index_at_char(&repr, char_count - half_chars);

		format!(
			"{} ... {} parameters truncated ... {}",
			&repr[..start_end],
			truncated_params,
			&repr[tail_start..]
		)
	} else {
		let half_chars = config.max_chars / 2;
		let truncated_chars = char_count - config.max_chars;

		// Fixes #802: Use char_indices() for character-aware slicing
		let start_end = byte_index_at_char(&repr, half_chars);
		let tail_start = byte_index_at_char(&repr, char_count - half_chars);

		format!(
			"{}  ... ({} characters truncated) ...  {}",
			&repr[..start_end],
			truncated_chars,
			&repr[tail_start..]
		)
	}
}
/// Truncate a single parameter value if it's too long
///
/// # Examples
///
/// ```
/// use reinhardt_utils::logging::params::truncate_param;
///
/// // Short strings are returned unchanged
/// let short_text = "Hello";
/// assert_eq!(truncate_param(short_text, 100), "Hello");
///
/// // Long strings are truncated with indication
/// let long_text = "a".repeat(1000);
/// let truncated = truncate_param(&long_text, 50);
/// assert!(truncated.len() < 1000);
/// assert!(truncated.contains("characters truncated"));
/// ```
pub fn truncate_param(value: &str, max_chars: usize) -> String {
	let char_count = value.chars().count();

	if char_count <= max_chars {
		return value.to_string();
	}

	let half_chars = max_chars / 2;
	let truncated_chars = char_count - max_chars;

	// Fixes #802: Use char_indices() for character-aware slicing
	let start_end = byte_index_at_char(value, half_chars);
	let tail_start = byte_index_at_char(value, char_count - half_chars);

	format!(
		"{} ... ({} characters truncated) ... {}",
		&value[..start_end],
		truncated_chars,
		&value[tail_start..]
	)
}

#[cfg(test)]
mod tests {
	use super::*;
	use rstest::rstest;
	use serde_json::json;

	#[rstest]
	fn test_repr_params_large_list_of_dict() {
		let params: Vec<Value> = (0..100).map(|i| json!({"data": i.to_string()})).collect();

		let config = ReprParamsConfig {
			max_chars: 300,
			batches: 10,
			is_multi: true,
		};

		let result = repr_params(&Value::Array(params), &config);
		eprintln!("LARGE_LIST OUTPUT: {}", result);

		// Should show first few and last few with exact message format
		assert!(
			result.contains("displaying 10 of 100 total bound parameter sets"),
			"Expected truncation message not found in: {}",
			result
		);
		// First element should contain "data": "0"
		assert!(
			result.contains(r#""data""#) && result.contains(r#""0""#),
			"Expected first element with data: 0 not found in: {}",
			result
		);
		// Last element should contain "data": "99"
		assert!(
			result.contains(r#""99""#),
			"Expected last element with data: 99 not found in: {}",
			result
		);
		// Should start with "[" and end with "]"
		assert!(result.starts_with('[') && result.ends_with(']'));
	}

	#[rstest]
	fn test_repr_params_positional_array() {
		let params = json!([[1, 2, 3], 5]);

		let config = ReprParamsConfig {
			max_chars: 300,
			batches: 10,
			is_multi: false,
		};

		let result = repr_params(&params, &config);
		// Verify exact debug format output
		assert_eq!(
			result,
			"[Array [Number(1), Number(2), Number(3)], Number(5)]"
		);
	}

	#[rstest]
	fn test_repr_params_unknown_list() {
		let large_array: Vec<i32> = (0..300).collect();
		let params = json!([large_array, 5]);

		let config = ReprParamsConfig {
			max_chars: 80,
			batches: 10,
			is_multi: false,
		};

		let result = repr_params(&params, &config);
		// Should be truncated with exact message format
		assert!(
			result.contains("characters truncated"),
			"Expected truncation message not found in: {}",
			result
		);
		// Should have ellipsis pattern: "...  ... (N characters truncated) ...  ..."
		assert!(
			result.matches("...").count() >= 2,
			"Expected ellipsis pattern not found in: {}",
			result
		);
		// Should start with "[" (array format)
		assert!(
			result.starts_with('['),
			"Expected array start not found in: {}",
			result
		);
	}

	#[rstest]
	fn test_repr_params_named_dict() {
		let mut params = serde_json::Map::new();
		for i in 0..10 {
			params.insert(format!("key_{}", i), json!(i));
		}

		let config = ReprParamsConfig {
			max_chars: 300,
			batches: 10,
			is_multi: false,
		};

		let result = repr_params(&Value::Object(params.clone()), &config);
		// Should fit without truncation
		assert!(
			!result.contains("truncated"),
			"Unexpected truncation in: {}",
			result
		);
		// Should contain all keys
		for i in 0..10 {
			assert!(
				result.contains(&format!(r#""key_{}""#, i)),
				"Expected key_{}  not found in: {}",
				i,
				result
			);
		}
		// Should start with "{" (object format from Debug)
		assert!(
			result.starts_with('{'),
			"Expected object start not found in: {}",
			result
		);
	}

	#[rstest]
	fn test_repr_params_huge_named_dict() {
		let mut params = serde_json::Map::new();
		for i in 0..800 {
			params.insert(format!("key_{}", i), json!(i));
		}

		let config = ReprParamsConfig {
			max_chars: 1400,
			batches: 10,
			is_multi: false,
		};

		let result = repr_params(&Value::Object(params), &config);
		// Should be truncated with exact message format for huge dicts
		assert!(
			result.contains("parameters truncated"),
			"Expected parameters truncation message not found in: {}",
			result
		);
		// Should have ellipsis pattern: "... N parameters truncated ..."
		assert!(
			result.matches("...").count() >= 2,
			"Expected ellipsis pattern not found in: {}",
			result
		);
		// Should start with "{" (object format)
		assert!(
			result.starts_with('{'),
			"Expected object start not found in: {}",
			result
		);
	}

	#[rstest]
	fn test_repr_params_ismulti_named_dict() {
		let param: serde_json::Map<String, Value> =
			(0..10).map(|i| (format!("key_{}", i), json!(i))).collect();

		let params: Vec<Value> = (0..50).map(|_| Value::Object(param.clone())).collect();

		let config = ReprParamsConfig {
			max_chars: 80,
			batches: 5,
			is_multi: true,
		};

		let result = repr_params(&Value::Array(params), &config);
		// Should show truncation with exact message format
		assert!(
			result.contains("displaying 5 of 50 total bound parameter sets"),
			"Expected multi-batch truncation message not found in: {}",
			result
		);
		// Should start with "[" and end with "]"
		assert!(
			result.starts_with('[') && result.ends_with(']'),
			"Expected array format not found in: {}",
			result
		);
	}

	#[rstest]
	fn test_truncate_param() {
		let large_param = "a".repeat(5000);
		let result = truncate_param(&large_param, 298);

		// Should be truncated to approximately max_chars + overhead
		assert!(
			result.len() < 5000,
			"Expected truncated length, got: {}",
			result.len()
		);
		assert!(
			result.len() > 298,
			"Result should be longer than max_chars due to truncation message"
		);
		// Should contain exact truncation message format
		assert!(
			result.contains("characters truncated"),
			"Expected truncation message not found in: {}",
			result
		);
		// Should start and end with repeated 'a'
		assert!(
			result.starts_with("aaaa"),
			"Expected start pattern not found in: {}",
			result
		);
		assert!(
			result.ends_with("aaaa"),
			"Expected end pattern not found in: {}",
			result
		);
		// Should have ellipsis pattern: "aaa ... (N characters truncated) ... aaa"
		assert!(
			result.matches("...").count() == 2,
			"Expected exactly 2 ellipsis markers, found: {}",
			result.matches("...").count()
		);
	}

	#[rstest]
	fn test_truncate_param_small() {
		let small_param = "small";
		let result = truncate_param(small_param, 100);

		assert_eq!(result, "small");
	}

	#[rstest]
	fn test_repr_array_with_multibyte_utf8_does_not_panic() {
		// Arrange
		let params = json!(["こんにちは世界", "テスト文字列", "日本語データ"]);
		let arr = params.as_array().unwrap();
		let config = ReprParamsConfig {
			max_chars: 20,
			batches: 10,
			is_multi: false,
		};

		// Act
		let result = repr_array(arr, &config);

		// Assert
		assert!(result.contains("characters truncated"));
	}

	#[rstest]
	fn test_repr_object_with_multibyte_utf8_does_not_panic() {
		// Arrange
		let mut obj = serde_json::Map::new();
		for i in 0..10 {
			obj.insert(format!("キー_{}", i), json!(format!("値_{}", i)));
		}
		let config = ReprParamsConfig {
			max_chars: 20,
			batches: 10,
			is_multi: false,
		};

		// Act
		let result = repr_object(&obj, &config);

		// Assert
		assert!(result.contains("characters truncated"));
	}

	#[rstest]
	fn test_repr_object_huge_with_multibyte_utf8_does_not_panic() {
		// Arrange
		let mut obj = serde_json::Map::new();
		for i in 0..200 {
			obj.insert(format!("キー_{}", i), json!(format!("値_{}", i)));
		}
		let config = ReprParamsConfig {
			max_chars: 50,
			batches: 10,
			is_multi: false,
		};

		// Act
		let result = repr_object(&obj, &config);

		// Assert
		assert!(result.contains("parameters truncated"));
	}

	#[rstest]
	fn test_truncate_param_with_multibyte_utf8_does_not_panic() {
		// Arrange
		let multibyte_param = "".repeat(500);

		// Act
		let result = truncate_param(&multibyte_param, 50);

		// Assert
		assert!(result.contains("characters truncated"));
		assert!(result.starts_with(""));
		assert!(result.ends_with(""));
	}

	#[rstest]
	fn test_truncate_param_with_mixed_ascii_and_multibyte() {
		// Arrange
		let mixed = format!("{}abc{}", "日本語".repeat(50), "中文字".repeat(50));

		// Act
		let result = truncate_param(&mixed, 30);

		// Assert
		assert!(result.contains("characters truncated"));
	}

	#[rstest]
	fn test_byte_index_at_char_with_ascii() {
		// Arrange
		let s = "hello";

		// Act & Assert
		assert_eq!(byte_index_at_char(s, 0), 0);
		assert_eq!(byte_index_at_char(s, 3), 3);
		assert_eq!(byte_index_at_char(s, 5), 5);
		assert_eq!(byte_index_at_char(s, 10), 5); // beyond end
	}

	#[rstest]
	fn test_byte_index_at_char_with_multibyte() {
		// Arrange
		let s = "あいう"; // Each char is 3 bytes

		// Act & Assert
		assert_eq!(byte_index_at_char(s, 0), 0);
		assert_eq!(byte_index_at_char(s, 1), 3);
		assert_eq!(byte_index_at_char(s, 2), 6);
		assert_eq!(byte_index_at_char(s, 3), 9);
		assert_eq!(byte_index_at_char(s, 10), 9); // beyond end
	}

	// Regression test for #762: truncate_param must produce valid UTF-8 output when
	// truncating strings containing multibyte characters. Previously, byte-level slicing
	// could split in the middle of a multibyte sequence, causing a panic or corrupted output.
	#[rstest]
	#[case("あいうえお".repeat(30), 20, "", "")] // 3-byte CJK chars (hiragana)
	#[case("日本語テスト".repeat(30), 20, "", "")] // 3-byte CJK chars (kanji + katakana)
	#[case("中文测试".repeat(30), 20, "", "")] // 3-byte CJK chars (Chinese)
	#[case("αβγδεζηθ".repeat(30), 20, "α", "θ")] // 2-byte Greek letters
	fn test_truncate_param_multibyte_produces_valid_utf8_regression(
		#[case] input: String,
		#[case] max_chars: usize,
		#[case] expected_start: &str,
		#[case] expected_end: &str,
	) {
		// Arrange - input is longer than max_chars and contains only multibyte characters

		// Act
		let result = truncate_param(&input, max_chars);

		// Assert - output must be valid UTF-8 (str is always valid UTF-8 in Rust,
		// but byte-level slicing at non-char boundaries would have panicked before #762 fix)
		assert!(
			result.contains("characters truncated"),
			"Regression #762: truncation message must be present, got: {}",
			result
		);
		assert!(
			result.starts_with(expected_start),
			"Regression #762: result must start with a complete character '{}', got: {}",
			expected_start,
			result
		);
		assert!(
			result.ends_with(expected_end),
			"Regression #762: result must end with a complete character '{}', got: {}",
			expected_end,
			result
		);
		// Verify the split position is on a character boundary by checking char count
		let prefix: &str = result.split(" ... ").next().unwrap_or("");
		let prefix_char_count = prefix.chars().count();
		assert_eq!(
			prefix_char_count,
			max_chars / 2,
			"Regression #762: prefix must contain exactly half of max_chars characters, got {}",
			prefix_char_count
		);
	}

	// Regression test for #762: truncate_param with a string that starts with ASCII
	// and transitions to multibyte. The split point may land at the transition boundary.
	#[rstest]
	fn test_truncate_param_ascii_then_multibyte_split_regression() {
		// Arrange - 10 ASCII chars then many 3-byte CJK chars
		let input = format!("{}{}", "abcdefghij", "".repeat(200));

		// Act
		let result = truncate_param(&input, 30);

		// Assert - result must contain truncation message and be valid UTF-8
		assert!(
			result.contains("characters truncated"),
			"Regression #762: truncation message expected, got: {}",
			result
		);
		// Verify all slices are on valid char boundaries (no panic means the fix works)
		let _ = result.chars().count(); // would panic if result contained invalid UTF-8
	}
}