doku icon indicating copy to clipboard operation
doku copied to clipboard

Generate output based on `[default()]` attribute

Open Nutomic opened this issue 3 years ago • 2 comments

We generate doku output using command doku::to_json_fmt_val(&fmt, &Settings::default(). But even so, the default values are completely ignored, and we need to specify #[doku(example = ...)] as well, which usually means writing the same thing twice.

For example this Rust code:

  /// Address where pictrs is available (for image hosting)
  #[default("http://pictrs:8080")]
  pub url: String,

Generates this output:

    # Address where pictrs is available (for image hosting)
    url: "string"

Additionally, i think it would make sense that specifying #[default(None)] for an Option would have the same effect as #[doku(skip)].

Nutomic avatar Jul 01 '22 13:07 Nutomic

Ah, I see - so what happens in your case is:

#[derive(Serialize, SmartDefault, Document)]
struct Settings {
    #[default(None)]
    pictrs_config: Option<PictrsConfig>,
}

#[derive(Serialize, SmartDefault, Document)]
struct PictrsConfig {
    #[default(Url::parse("http://pictrs:8080").unwrap())]
    url: Url,
}

fn main() {
    let fmt = /* ... */;
    
    println!("{}", doku::to_json_fmt_val(&fmt, &Settings::default()));
}

Note that you're using #[default(None)] for pictrs_config, which makes Settings::default() - from Doku's point of view - equal to:

Settings {
    pictrs_config: None,
}

... and that object does not provide any value for pictrs_config.url (making Doku use its own default value of "string").

Had you written #[default(Some(Default::default()))], Doku would get:

Settings {
    pictrs_config: Some(PictrsConfig {
        url: "https://pictrs:8080"
    }),
}

... and from that it would find out that pictrs_config.url = "http://pictrs:8080".

Of course, I guess doing #[default(Some(Default::default()))] doesn't really help you - 😄 - so it looks like I'll have to rethink this feature a bit.

Additionally, i think it would make sense that (...)

Sure, sounds doable 🙂

Patryk27 avatar Jul 01 '22 14:07 Patryk27

Ah that makes sense. For that particular value, it should actually be fine with Default = Some(...), so im changing that. I think the problem is that between default and doku(example) attributes, it can get a bit confusing which one will be used. But not sure how that could be improved.

Nutomic avatar Jul 01 '22 15:07 Nutomic