Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ pub fn to_table<S: Serialize + core::fmt::Debug>(data: Vec<S>) -> Result<Table,

for record in reader.into_records() {
let record = record?;
let iter = record.iter().map(|s| s.to_owned());
let iter = record.iter().map(|s| {
if s.len() > 20 {
return truncate_string(s, 4, 3)
}
s.to_owned()
});
builder.push_record(iter);
}

Expand Down Expand Up @@ -118,3 +123,12 @@ pub async fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}


fn truncate_string(input: &str, prefix_len: usize, suffix_len: usize) -> String {
if input.len() <= prefix_len + suffix_len {
return input.to_string(); // No need to truncate
}

format!("{}...{}", &input[..prefix_len], &input[input.len() - suffix_len..])
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation and tests for the truncation function.

The function needs validation and test coverage:

  1. Validate that prefix_len + suffix_len > 0
  2. Handle cases where prefix_len + suffix_len > input.len()
  3. Add unit tests as mentioned in PR checklist
 fn truncate_string(input: &str, prefix_len: usize, suffix_len: usize) -> String {
+    // Validate input parameters
+    if prefix_len == 0 && suffix_len == 0 {
+        return input.to_string();
+    }
+
+    // Ensure we don't panic on short inputs
+    let total_len = prefix_len.saturating_add(suffix_len);
     if input.len() <= prefix_len + suffix_len {
         return input.to_string(); // No need to truncate
     }

     format!("{}...{}", &input[..prefix_len], &input[input.len() - suffix_len..])
 }

Consider adding these test cases:

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

    #[test]
    fn test_truncate_string() {
        assert_eq!(truncate_string("abcdefghijk", 4, 3), "abcd...ijk");
        assert_eq!(truncate_string("short", 4, 3), "short");
        assert_eq!(truncate_string("a", 4, 3), "a");
        assert_eq!(truncate_string("", 4, 3), "");
    }
}