Skip to content

Conversation

@thomasqueirozb
Copy link
Contributor

@thomasqueirozb thomasqueirozb commented Jan 26, 2026

Summary

Add required optional default to Parameter


Last commit was generated with the following code:

#!/usr/bin/env python3
"""
Convert the pattern:
    let x = self
        .field
        .as_ref()
        .map(|expr| expr.resolve(ctx))
        .transpose()?
        .unwrap_or_else(|| DEFAULT_*.clone());

to:
    let x = self
        .field
        .map_resolve_with_default(ctx, || DEFAULT_*.clone())?;
"""

import re
import sys
from pathlib import Path

# Pattern to match the old style with flexible whitespace
# self and .field_name are on separate lines
PATTERN = re.compile(
    r'self\s+\.(\w+)\s+'
    r'\.as_ref\(\)\s+'
    r'\.map\(\|expr\| expr\.resolve\(ctx\)\)\s+'
    r'\.transpose\(\)\?\s+'
    r'\.unwrap_or_else\(\|\| (DEFAULT_\w+\.clone\(\))\)',
    re.MULTILINE | re.DOTALL
)

def convert_file(filepath: Path) -> bool:
    """Convert a single file. Returns True if changes were made."""
    content = filepath.read_text()

    def replace(match):
        field_name = match.group(1)
        default = match.group(2)  # DEFAULT_*.clone()
        return f'self\n            .{field_name}\n            .map_resolve_with_default(ctx, || {default})?'

    new_content = PATTERN.sub(replace, content)

    if new_content != content:
        filepath.write_text(new_content)
        return True
    return False

def main():
    stdlib_dir = Path(__file__).parent.parent / 'src' / 'stdlib'

    if not stdlib_dir.exists():
        print(f"Error: {stdlib_dir} does not exist")
        sys.exit(1)

    changed_files = []
    for rs_file in sorted(stdlib_dir.glob('*.rs')):
        if convert_file(rs_file):
            changed_files.append(rs_file.name)
            print(f"Converted: {rs_file.name}")

    if changed_files:
        print(f"\nConverted {len(changed_files)} files")
    else:
        print("No files needed conversion")

if __name__ == '__main__':
    main()

Changes Checklist

Defaults that differ from source code

  • decode_lz4.rs (default buf size differs from docs vs source code)
  • encode_lz4.rs (default prepend_size differed from docs vs source code)
  • format_number.rs (grouping_separator had wrong default "," in docs, actual behavior is no default)
  • parse_bytes.rs (DEFAULT_BASE was Integer(2) but should be Bytes("2"))
  • parse_etld.rs (DEFAULT_PLUS_PARTS was Boolean(false) but should be Integer(0))
  • parse_groks.rs (DEFAULT_ALIASES was Boolean(true) but should be empty object)

Changes that don't map 1:1 with source code

  • encode_zstd.rs (zstd level 0 means "use default 3", so docs and code are consistent)
  • slice.rs (end has no runtime default, actual default is None meaning "to end")
  • uuid_v7.rs (no runtime default, None means use Utc::now())

No changes between docs and source code

  • ceil.rs
  • compact.rs
  • contains.rs
  • crc.rs
  • decode_base64.rs
  • decode_punycode.rs
  • del.rs
  • encode_base64.rs
  • encode_gzip.rs
  • encode_key_value.rs
  • encode_percent.rs
  • encode_punycode.rs
  • encode_zlib.rs
  • ends_with.rs
  • find.rs
  • flatten.rs
  • floor.rs
  • format_int.rs
  • from_unix_timestamp.rs
  • hmac.rs
  • log.rs
  • map_keys.rs
  • map_values.rs
  • match_array.rs
  • merge.rs
  • parse_apache_log.rs
  • parse_aws_alb_log.rs
  • parse_common_log.rs
  • parse_csv.rs
  • parse_json.rs
  • parse_key_value.rs
  • parse_nginx_log.rs
  • parse_regex_all.rs
  • parse_regex.rs
  • parse_url.rs
  • parse_user_agent.rs
  • parse_xml.rs
  • remove.rs
  • replace_with.rs
  • replace.rs
  • round.rs
  • sha2.rs
  • sha3.rs
  • shannon_entropy.rs
  • sieve.rs
  • starts_with.rs
  • to_unix_timestamp.rs
  • unflatten.rs
  • xxhash.rs

Change Type

  • Bug fix
  • New feature
  • Non-functional (chore, refactoring, docs)
  • Performance

Is this a breaking change?

  • Yes
  • No

How did you test this PR?

./scripts/checks.sh

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on
    our guidelines.
  • No. A maintainer will apply the "no-changelog" label to this PR.

References

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