Skip to content

feat: add into, optional, const and skip options with enhanced visibility parser (closes #30, #31)#125

Open
mahdi739 wants to merge 4 commits intojbaublitz:mainfrom
mahdi739:add-into-optional-const-skip-enhanced-visibility
Open

feat: add into, optional, const and skip options with enhanced visibility parser (closes #30, #31)#125
mahdi739 wants to merge 4 commits intojbaublitz:mainfrom
mahdi739:add-into-optional-const-skip-enhanced-visibility

Conversation

@mahdi739
Copy link
Copy Markdown

This PR introduces into, optional, const, and skip options for related macros.

  • into is only allowed for set and set_with. It enables setters to accept std::convert::Into<T> for a field type T.
    Example:
#[derive(Setters, Default)]
pub struct Config {
    #[getset(set = "into")]
    name: String,
}

fn example() {
    Config::default().set_name("val");
}
  • optional is only allowed for set and set_with, and the field must be an Option<T> type. It allows setters to accept T instead of Option<T>.
    Example:
#[derive(Setters, Default)]
pub struct Config {
    #[getset(set = "optional")]
    name: Option<String>,
}
fn example() {
    Config::default().set_name("val".to_string());
}
  • optional and into can be used together:
#[derive(Setters, Default)]
pub struct Config {
    #[getset(set = "into optional")]
    name: Option<String>,
}
fn example() {
    Config::default().set_name("val");
}
  • const makes the related method const. Due to its strict constraints, any compile-time errors that arise when using it must be handled by the user.
    Example:
#[derive(Getters)]
struct MathConstants {
    #[getset(get = "pub const")]
    pi: f64,
}

const PI: &f64 = MathConstants { pi: 3.14159 }.pi();
  • skip excludes the attribute individually. It is incompatible with other options and must be used alone for that specific attribute. Note that it's different than the skip attribute.
    Example:
#[derive(CopyGetters, Getters, Default)]
#[getset(get_copy)]
pub struct Config {
    #[getset(get_copy = "skip", get)]
    name: String,
    id: usize,
    number: u32,
}
fn example() {
    let name: &String = Config::default().name();
}

Additionally, the visibility parser has been improved to support more complex visibility specifiers, such as pub(in crate).

@mahdi739 mahdi739 changed the title feat: add into, optional, const and skip options with enhanced visibility parser feat: add into, optional, const and skip options with enhanced visibility parser (closes #30, #31) Jun 26, 2025
@jbaublitz
Copy link
Copy Markdown
Owner

Hi @mahdi739, thanks for your contribution. Could you resolve the CI issues? I'll take a look after that.

@mahdi739
Copy link
Copy Markdown
Author

mahdi739 commented Jul 3, 2025

Hi @jbaublitz. I didn't understand the error. But I can resolve the clippy warnings.

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