Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve config function consistency #765

Merged
merged 2 commits into from
Apr 19, 2024

Conversation

suaviloquence
Copy link
Contributor

@suaviloquence suaviloquence commented Apr 19, 2024

Fixes #764

Config structs (Check, GlobalConfig, PackageSelection) now adhere to the following conventions for setter methods:

  • method is called set_[property]
  • method signature is fn(&mut self, T) -> &mut Self

Config structs (`Check`, `GlobalConfig`, `PackageSelection`) now
adhere to the following conventions for setter methods:

- method is called `set_[property]`
- method signature is `fn(&mut self, T) -> &mut Self`
@suaviloquence
Copy link
Contributor Author

suaviloquence commented Apr 19, 2024

Edited migration guide:

Migration Guide

Major changes

  • Check::check_release takes a new &mut GlobalConfig parameter, and GlobalConfig now holds log level and color settings.

    • Change:
      let mut check = Check::new(rustdoc);
      check.with_packages(packages)
           .with_log_level(Some(log_level))
           .with_color_choice(Some(termcolor::ColorChoice::Always));
      let result = check.check_release();
    • To:
      let mut config = GlobalConfig::new()
         .set_level(Some(log_level));
      config.set_color_choice(true); // or set_{err,out)_color_choice for finer-grained control
      
      let mut check = Check::new(rustdoc);
      check.with_packages(packages);
      let result = check.check_release(&mut config);
  • Set color choice in one of three ways:

    • Change:
      let choice: termcolor::ColorChoice = ...;
      config = config.set_color_choice(choice);
    • To: one of the three following options, depending on desired granularity
      1. Set global color choice for all crates using the anstream library:
        let choice: anstream::ColorChoice = ...;
        // set *before* creating `GlobalConfig` instance (if using default stdout/stderr)
        // or before calling `GlobalConfig::set_std{out,err}` (if configuring them)
        choice.write_global(); // configures `choice` for all crates using `anstream`
      2. Set whether to use colors for both stdout and stderr in the cargo-semver-checks library:
        let use_colors: bool = ...;
        config.set_color_choice(use_colors);
      3. Set whether to use colors for a specific stream in the cargo-semver-checks library:
        let use_colors_for_stderr: bool = ...;
        let use_colors_for_stdout: bool = ...;
        config.set_err_color_choice(use_colors_for_stderr);
        config.set_out_color_choice(use_colors_for_stdout);
  • config structs (Check, GlobalConfig, PackageSelection) now have a consistent setter convention: pub fn set_[property](&mut self, prop: T) -> &mut Self:

    • note: this does not apply to Check::with_[...]_features() methods as they are toggles vs property setters.
    • Change:
      selection.with_excluded_packages(packages);
      check.with_selection(selection).with_all_features();
    • To:
      selection.set_excluded_packages(packages);
      check.set_package_selection(selection).with_all_features();
    • Change:
      config = config.set_level(level);
    • To:
      config.set_log_level(level); // now takes and returns &mut Self instead of owned Self

New features

  • Users can configure the stdout and stderr streams for cargo-semver-checks to be different from default:
    let stderr: Box<dyn std::io::Write> = Box::new(stderr_buffer);
    config.set_stderr(stderr);

Minor changes

  • #[must_use] added on GlobalConfig::std{err,out}
  • GlobalConfig::shell_print takes an anstyle::Color instead of a termcolor::Color:
    • Change config.shell_print(status, message, termcolor::Color::Red, justified)
    • To config.shell_print(status, message, anstyle::Color::Ansi(anstyle::AnsiColor::Red, justified)
  • To write colors to stdout/stderr, use writeln!(config.stdout(), "{}styled text", anstyle::Style::new().bold().[...]); instead of the termcolor API
  • use GlobalConfig::{out,err}_color_choice() to query whether to print colors for the stdout/stderr stream instead of using GlobalConfig::is_err_tty as a heuristic
  • GlobalConfig::std{out,err} returns an opaque impl Write + '_ type instead of a termcolor::StandardStream

src/lib.rs Outdated Show resolved Hide resolved
`Check::with_[x]_features()` isn't a setter in the convention we're
using
@obi1kenobi obi1kenobi enabled auto-merge (squash) April 19, 2024 01:34
@obi1kenobi
Copy link
Owner

Great, ship it 🚀

@obi1kenobi obi1kenobi merged commit e5e2cb6 into obi1kenobi:main Apr 19, 2024
31 of 32 checks passed
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.

Configuration methods do not have consistent naming
2 participants