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

JS + demo_gen: Enumerators Fix #710

Merged
merged 7 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions example/demo_gen/demo/index.mjs

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

29 changes: 14 additions & 15 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.

4 changes: 4 additions & 0 deletions example/js/lib/api/FixedDecimalGroupingStrategy.mjs

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

15 changes: 10 additions & 5 deletions feature_tests/demo_gen/demo/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ let termini = Object.assign({

{
name: "DiplomatStr",
type: "string"
type: "string",
typeUse: "string"
}

]
Expand All @@ -35,7 +36,8 @@ let termini = Object.assign({

{
name: "V",
type: "Array<number>"
type: "Array<number>",
typeUse: "Array<number>"
}

]
Expand All @@ -49,7 +51,8 @@ let termini = Object.assign({

{
name: "V",
type: "string"
type: "string",
typeUse: "string"
}

]
Expand All @@ -63,7 +66,8 @@ let termini = Object.assign({

{
name: "Foo",
type: "string"
type: "string",
typeUse: "string"
}

]
Expand All @@ -86,7 +90,8 @@ let termini = Object.assign({

{
name: "Input",
type: "string"
type: "string",
typeUse: "string"
}

]
Expand Down
29 changes: 14 additions & 15 deletions feature_tests/demo_gen/demo/rendering/rendering.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ class EnumTemplate extends ParameterTemplate {
let options = clone.querySelector("*[data-options]");

if (this.default === null) {
this.default = enumType.values.values().next().value;

for (let value of enumType.values) {
options.append(...(new EnumOption(value[0])).children);
for (let entry of enumType.getAllEntries()) {
options.append(...(new EnumOption(entry[0])).children);
}
}
}
Expand All @@ -171,7 +169,7 @@ class TerminusParams extends HTMLElement {

var newChild;

switch (param.type) {
switch (param.typeUse) {
case "string":
newChild = new StringTemplate(param);
this.#params[i] = "";
Expand All @@ -188,17 +186,18 @@ class TerminusParams extends HTMLElement {
newChild = new StringArrayTemplate(param);
this.#params[i] = [];
break;
case "enumerator":
newChild = new EnumTemplate(param, library[param.type]);
this.#params[i] = newChild.default
break;
case "external":
let updateParamEvent = (value) => {
this.#params[i] = value;
};
evaluateExternal(param, updateParamEvent);
break;
default:
if (param.type in library && "values" in library[param.type]) {
newChild = new EnumTemplate(param, library[param.type]);
this.#params[i] = newChild.default
} else {
let updateParamEvent = (value) => {
this.#params[i] = value;
};
evaluateExternal(param, updateParamEvent);
continue;
}
console.error("Unrecognized parameter: ", param);
break;
}

Expand Down
4 changes: 4 additions & 0 deletions feature_tests/js/api/ContiguousEnum.mjs

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

4 changes: 4 additions & 0 deletions feature_tests/js/api/ErrorEnum.mjs

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

4 changes: 4 additions & 0 deletions feature_tests/js/api/MyEnum.mjs

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

4 changes: 4 additions & 0 deletions feature_tests/js/api/OptionEnum.mjs

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

4 changes: 4 additions & 0 deletions feature_tests/js/api/RenamedAttrEnum.mjs

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

4 changes: 4 additions & 0 deletions feature_tests/js/api/UnimportedEnum.mjs

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

73 changes: 38 additions & 35 deletions tool/src/demo_gen/terminus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ pub struct OutParam {
/// Full string name of the param.
pub label: String,
pub default_value: String,
/// For typescript and RenderInfo output. Type that this parameter is.
/// For typescript and RenderInfo output. Type that this parameter is. We get this directly from Rust.
pub type_name: String,
/// Also for typescript and RenderInfo output. This is used for types where we might want to know more information, like if it's an enumerator, or a custom type to be set by the default renderer.
pub type_use: String,
}

/// Represents a function that we'll be using when constructing the ultimate output of a RenderTerminus function. See [`TerminusInfo`] for full output.
Expand Down Expand Up @@ -177,10 +179,10 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {

/// Helper function for quickly passing a parameter to both our node and the render terminus.
/// Appends to [TerminusInfo::out_params]
fn append_out_param(
fn append_out_param<P: TyPosition<StructPath = StructPath>>(
&mut self,
param_name: String,
type_name: String,
type_info: &Type<P>,
node: &mut MethodDependency,
attrs: Option<DemoInfo>,
) {
Expand All @@ -194,10 +196,37 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {

let default_value = attrs_default.input_cfg.default_value;

let type_name = match type_info {
Type::Primitive(p) => self.formatter.fmt_primitive_as_ffi(*p).to_string(),
Type::Enum(e) => self.formatter.fmt_type_name(e.tcx_id.into()).to_string(),
Type::Slice(hir::Slice::Str(..)) => self.formatter.fmt_string().to_string(),
Type::Slice(hir::Slice::Primitive(.., p)) => {
self.formatter.fmt_primitive_list_type(*p).to_string()
}
Type::Slice(hir::Slice::Strs(..)) => "Array<string>".to_string(),
_ => {
if let Some(i) = type_info.id() {
self.formatter.fmt_type_name(i).to_string()
} else {
panic!("Type {type_info:?} not recognized.");
}
}
};

let type_use = if attrs_default.external {
"external".into()
} else {
match type_info {
Type::Enum(..) => "enumerator".into(),
_ => type_name.clone(),
}
};

let out_param = OutParam {
param_name,
label,
type_name: type_name.clone(),
type_use,
default_value,
};

Expand Down Expand Up @@ -226,13 +255,8 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
// TODO: I think we need to check for struct and opaque types as to whether or not these have attributes that label them as provided as a parameter.
match param_type {
// Types we can easily coerce into out parameters (i.e., get easy user input from):
Type::Primitive(p) => {
self.append_out_param(
param_name,
self.formatter.fmt_primitive_as_ffi(*p).to_string(),
node,
Some(param_attrs),
);
Type::Primitive(..) => {
self.append_out_param(param_name, param_type, node, Some(param_attrs));
}
Type::Enum(e) => {
let type_name = self.formatter.fmt_type_name(e.tcx_id.into()).to_string();
Expand All @@ -242,31 +266,10 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
.push_error(format!("Found usage of disabled type {type_name}"))
}

self.append_out_param(param_name, type_name, node, Some(param_attrs));
}
Type::Slice(hir::Slice::Str(..)) => {
self.append_out_param(
param_name,
self.formatter.fmt_string().to_string(),
node,
Some(param_attrs),
);
}
Type::Slice(hir::Slice::Primitive(_, p)) => {
self.append_out_param(
param_name,
self.formatter.fmt_primitive_list_type(*p).to_string(),
node,
Some(param_attrs),
);
self.append_out_param(param_name, param_type, node, Some(param_attrs));
}
Type::Slice(hir::Slice::Strs(..)) => {
self.append_out_param(
param_name,
"Array<string>".to_string(),
node,
Some(param_attrs),
);
Type::Slice(..) => {
self.append_out_param(param_name, param_type, node, Some(param_attrs));
}
// Types we can't easily coerce into out parameters:
Type::Opaque(o) => {
Expand All @@ -280,7 +283,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
}

if all_attrs.demo_attrs.external {
self.append_out_param(param_name, type_name.into(), node, Some(param_attrs));
self.append_out_param(param_name, param_type, node, Some(param_attrs));
return;
}

Expand Down
Loading