Skip to content

Conversation

Ifechukwudaniel
Copy link

@Ifechukwudaniel Ifechukwudaniel commented Feb 12, 2025

Summary

Truncated fields for long fields, instead of printing a really long string, we should truncate it like 0x124...123 fixed the issue

Before

ksnip_20250212-052056

After

ksnip_20250212-042340

Related Issue

#60

Technical details

Truncating long strings fixed it

Topics for Discussion

PR Checklist

  • Added Tests
  • Cargo Tests Passing
  • Added Documentation
  • Breaking changes

Screenshots (if applicable)

Summary by CodeRabbit

  • New Features

    • Enhanced table display formatting now automatically truncates excessively long text, ensuring the output remains clean and easily readable.
  • Bug Fixes

    • Simplified handling of log filter parsing to improve efficiency.
  • Style

    • Improved code formatting for better readability and organization.
  • Chores

    • Removed duplicate module declaration to streamline the codebase.
    • Added a newline at the end of a file for consistency.

Copy link
Contributor

coderabbitai bot commented Feb 12, 2025

Walkthrough

The changes introduce a new function, truncate_string, in the CLI module. This function checks if a string exceeds a specified length, and if so, truncates it by retaining a fixed number of characters from both the start and the end. The to_table function has been updated to use this truncate functionality when processing records, ensuring that strings longer than 20 characters are succinctly displayed. Additionally, modifications were made to the LogFilter enum in the core module, code formatting improvements in the execution engine, removal of a duplicate module declaration, and the addition of a newline at the end of a file in the macros module.

Changes

File(s) changed Summary of changes
crates/cli/src/main.rs Added truncate_string function; updated the to_table function to apply truncation for strings longer than 20 characters by preserving 4 prefix and 3 suffix characters. Added a test module for truncate_string.
crates/core/src/common/logs.rs Modified try_from implementation for the LogFilter enum to simplify the address_filter_type handling by removing an unnecessary argument in the Address::to_checksum call. No changes to exported entities.
crates/core/src/interpreter/backend/execution_engine.rs Reformatted import statements and method signatures for run and run_get_expr for better readability without changing functionality. Match arms in run_get_expr reformatted for clarity. No changes to exported entities.
crates/core/src/interpreter/backend/mod.rs Removed duplicate declaration of the execution_engine module, ensuring a single public declaration.
crates/macros/src/lib.rs Added a newline at the end of the file. No functional changes or alterations to exported entities.

Sequence Diagram(s)

sequenceDiagram
    participant TT as to_table
    participant TS as truncate_string
    participant Record as Record String

    Record->>TT: Pass string value
    TT->>TT: Check if string length > 20
    alt Long String
        TT->>TS: Call truncate_string(input, 4, 3)
        TS-->>TT: Return truncated string
    else
        TT-->>TT: Use original string
    end
    TT->>Record: Return processed string for display
Loading

Poem

Oh, I hop through lines of code today,
Finding strings that stretch away.
With a clever slice at start and end,
I'll trim them down—on me they depend!
Cheers from a rabbit, coding all the way!
🐇💻


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
crates/cli/src/main.rs (1)

86-91: Extract magic numbers into named constants.

The hardcoded values for string length threshold and truncation parameters should be constants at the module level for better maintainability.

+const MAX_FIELD_LENGTH: usize = 20;
+const PREFIX_LENGTH: usize = 4;
+const SUFFIX_LENGTH: usize = 3;

 for record in reader.into_records() {
     let record = record?;
     let iter = record.iter().map(|s| { 
-        if s.len() > 20 {
-           return  truncate_string(s, 4, 3)
+        if s.len() > MAX_FIELD_LENGTH {
+           return truncate_string(s, PREFIX_LENGTH, SUFFIX_LENGTH)
         }
         s.to_owned()
     });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9b791a8 and 8864d3b.

📒 Files selected for processing (1)
  • crates/cli/src/main.rs (2 hunks)

Comment on lines 128 to 134
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), "");
    }
}

@Ifechukwudaniel
Copy link
Author

Ifechukwudaniel commented Feb 12, 2025

@iankressin please merge

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
crates/cli/src/main.rs (2)

86-91: Extract magic numbers as constants.

The hardcoded values should be constants for better maintainability and reusability.

+const MAX_FIELD_LENGTH: usize = 20;
+const PREFIX_LENGTH: usize = 4;
+const SUFFIX_LENGTH: usize = 3;
+
 let iter = record.iter().map(|s| {
-    if s.len() > 20 {
-        return truncate_string(s, 4, 3);
+    if s.len() > MAX_FIELD_LENGTH {
+        return truncate_string(s, PREFIX_LENGTH, SUFFIX_LENGTH);
     }
     s.to_owned()
 });

Also, consider adding validation to ensure PREFIX_LENGTH + SUFFIX_LENGTH < MAX_FIELD_LENGTH.


143-154: Consider adding more edge cases to tests.

While the current test coverage is good, consider adding these edge cases:

 #[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), "");
+    // Edge cases
+    assert_eq!(truncate_string("test", 0, 0), "test");
+    assert_eq!(truncate_string("abcdef", 3, 2), "abc...ef");
+    assert_eq!(truncate_string("abc", 2, 2), "abc");
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8864d3b and b00e3a2.

📒 Files selected for processing (5)
  • crates/cli/src/main.rs (2 hunks)
  • crates/core/src/common/logs.rs (1 hunks)
  • crates/core/src/interpreter/backend/execution_engine.rs (12 hunks)
  • crates/core/src/interpreter/backend/mod.rs (1 hunks)
  • crates/macros/src/lib.rs (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • crates/macros/src/lib.rs
  • crates/core/src/interpreter/backend/execution_engine.rs
  • crates/core/src/interpreter/backend/mod.rs
🔇 Additional comments (2)
crates/cli/src/main.rs (1)

127-140: Implementation looks good!

The function handles edge cases well and includes proper input validation.

crates/core/src/common/logs.rs (1)

145-146: LGTM! Good cleanup.

Removing the unnecessary None argument simplifies the code without affecting functionality.

@Ifechukwudaniel
Copy link
Author

@iankressin i am done with this , if you cool with it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant