Skip to content

Commit

Permalink
Demo Gen default value parameter attributes (#667)
Browse files Browse the repository at this point in the history
* Started drafting default value parameter attributes

* Add support for default values in the renderer

* Formatting

* Update snapshots

* Parse ints and floats

* Formatting
  • Loading branch information
ambiguousname authored Sep 3, 2024
1 parent d7ad173 commit 5ccea38
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 80 deletions.
26 changes: 25 additions & 1 deletion core/src/hir/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ pub struct Attrs {
#[non_exhaustive]
#[derive(Clone, Default, Debug)]
pub struct DemoInputCFG {
/// `#[diplomat(input(label = "..."))]`
/// `#[diplomat::demo(input(label = "..."))]`
/// Label that this input parameter should have. Let demo_gen pick a valid name if this is empty.
///
/// For instance <label for="v">Number Here</label><input name="v"/>
pub label: String,

/// `#[diplomat::demo(input(default_value = "..."))]`
/// Sets the default value for a parameter.
///
/// Should ALWAYS be a string. The HTML renderer is expected to do validation for us.
pub default_value: String,
}

#[non_exhaustive]
Expand Down Expand Up @@ -372,6 +378,24 @@ impl Attrs {
let s: syn::LitStr = value.parse()?;
this.demo_attrs.input_cfg.label = s.value();
Ok(())
} else if meta.path.is_ident("default_value") {
let value = meta.value()?;

let str_val: String;

let ahead = value.lookahead1();
if ahead.peek(syn::LitFloat) {
let s: syn::LitFloat = value.parse()?;
str_val = s.base10_parse::<f64>()?.to_string();
} else if ahead.peek(syn::LitInt) {
let s: syn::LitInt = value.parse()?;
str_val = s.base10_parse::<i64>()?.to_string();
} else {
let s: syn::LitStr = value.parse()?;
str_val = s.value();
}
this.demo_attrs.input_cfg.default_value = str_val;
Ok(())
} else {
Err(meta.error(format!(
"Unsupported ident {:?}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Method {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -101,6 +102,7 @@ Method {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand All @@ -125,6 +127,7 @@ Method {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -108,6 +109,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -151,6 +153,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand All @@ -172,6 +175,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -234,6 +238,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -291,6 +296,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -327,6 +333,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -364,6 +371,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand All @@ -385,6 +393,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down Expand Up @@ -429,6 +438,7 @@ TypeContext {
external: false,
input_cfg: DemoInputCFG {
label: "",
default_value: "",
},
},
},
Expand Down
22 changes: 22 additions & 0 deletions docs/demo_gen.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ export const RenderInfo = {
};
```

The exact structure of `RenderInfo` is:

```js
export const RenderInfo = {
termini: {
"functionName": {
func: jsFunctionToCall, // Always present.
funcName: "jsFunctionToCall", // String value of the function name. Always present.
parameters: [ // Always present.
{
name: "Param Name", // Always present. Modified by `#[diplomat::demo(input(label = "..."))]`
type: "type_name", // Always present. Could be: string, number, boolean, Array<string>, Array<number>, Array<boolean>, or some specific JS binding class name (i.e., MyEnum).
// In your HTML renderer, you should generally assume that any type that is not a primitive is an enum. #[diplomat::demo(external)] parameters are also exposed here, so be prepared to include carve-outs for those exceptions.
defaultValue: "defaultValue" // Only present if `#[diplomat::demo(input(default_value = "..."))]` is present.
}
]
}
}
};

```

#### Example Part 3

For `FixedDecimalFormatter.formatWrite`, let's look at the rust definition again:
Expand Down
9 changes: 6 additions & 3 deletions example/demo_gen/demo/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 45 additions & 22 deletions example/demo_gen/demo/rendering/rendering.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub mod ffi {
pub struct FixedDecimalFormatterOptions {
#[diplomat::demo(input(label = "ICU4X Fixed Decimal Grouping Strategy"))]
pub grouping_strategy: FixedDecimalGroupingStrategy,
#[diplomat::demo(input(label = "Useless Config (Ignore)"))]
#[diplomat::demo(input(label = "Useless Config (Ignore)", default_value = "true"))]
pub some_other_config: bool,
}

Expand Down
3 changes: 2 additions & 1 deletion example/src/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub mod ffi {
/// Construct an [`FixedDecimal`] from an integer.
#[diplomat::attr(auto, constructor)]
pub fn new(
#[diplomat::demo(input(label = "ICU4XFixedDecimal Value"))] v: i32,
#[diplomat::demo(input(label = "ICU4XFixedDecimal Value", default_value = 1000))]
v: i32,
) -> Box<FixedDecimal> {
Box::new(FixedDecimal(fixed_decimal::FixedDecimal::from(v)))
}
Expand Down
Loading

0 comments on commit 5ccea38

Please sign in to comment.