Skip to content

Conversation

@Mr-Punder
Copy link

Description

Implements JSON pretty-printing functionality as requested in the issue Indent and format #37. Adds --indent parameter to control output formatting while maintaining rekson's core JSON fixing capabilities.

Changes

  • Added --indent parameter with three modes:
    • No --indent: Preserve original spacing (no formatting changes)
    • --indent 0: Compact output (collapse all spaces)
    • --indent N: Pretty-print with N spaces indentation
  • New IndentFormatter struct in rekson-lib/src/formatter.rs for handling indentation logic
  • Updated CLI in src/cli.rs to parse --indent parameter with proper error handling
  • Enhanced library API with process_bytes_with_indent() function
  • Updated web interface to support formatting options
  • Comprehensive tests covering all indentation scenarios

Implementation Details

  • Formatter processes tokens stream and applies indentation rules
  • Supports both --indent 4 and --indent=4 syntax
  • Zero-space mode produces truly compact JSON output
  • Maintains rekson's existing JSON fixing capabilities (comments, quotes, commas, etc.)
  • No tabs support as specified in requirements

Testing

# Test basic functionality
echo '{"a":3}' | rekson --indent 2

# Test compact mode  
echo '{"a": 3, "b": [1, 2]}' | rekson --indent 0

# Test without formatting
echo '{"a":3}' | rekson

# Run test suite
cargo test

Examples

# Input: {"name":"test","values":[1,2,3]}
# Output with --indent 2:
{
  "name": "test", 
  "values": [
    1,
    2,
    3
  ]
}

# Output with --indent 0:
{"name":"test","values":[1,2,3]}

Fixes the pretty-printing requirement from the original issue while maintaining backward compatibility.

@Mr-Punder Mr-Punder changed the title Add JSON pretty-printing with --indent parameter feat: Add JSON pretty-printing with --indent parameter Jul 5, 2025
@pinbraerts pinbraerts self-assigned this Jul 6, 2025
Copy link
Owner

@pinbraerts pinbraerts left a comment

Choose a reason for hiding this comment

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

Надо чуть-чуть полшлифовать пр. По логике не особо смотрел, если работает, то норм.

Copy link
Owner

Choose a reason for hiding this comment

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

Удали из пр и добавь .vscode в .gitignore

{
let mut lexer = lexer::Lexer::default();
let mut parser = parser::Parser::default();
let mut formatter = indent.map(|size| formatter::IndentFormatter::new(size));
Copy link
Owner

Choose a reason for hiding this comment

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

По идее можно

indent.map(formatter::IndentFormatter::new)

print_help();
std::process::exit(0);
}
"--indent" => {
Copy link
Owner

Choose a reason for hiding this comment

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

let value = args.get(i + 1).ok_or_else(|| Error("--indent requires a value" ))?;
let indent = value.parse::<usize>()?;
return Ok(Args { indent: Some(indent) });

return Err("--indent requires a value".to_string());
}
}
arg if arg.starts_with("--indent=") => {
Copy link
Owner

Choose a reason for hiding this comment

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

Зачем этот случай поддерживать, если можно его не поддерживать? Давай забьём на =

Copy link
Owner

Choose a reason for hiding this comment

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

Тут явно что-то лишнее

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.

2 participants