Skip to content

Commit

Permalink
feat: add u8 field type support (#433)
Browse files Browse the repository at this point in the history
* Add u8 field type

* Represent Vec<u8> as a Uint8Array

* Add u8 widgets

* Fix misplaced lables

* Fix svelte slider props

* Ensure correct TS type is generated for Vec<u8>

* Skip ui generation for Vec<u8> types
  • Loading branch information
c12i committed Dec 18, 2024
1 parent 4a80462 commit a2812ae
Show file tree
Hide file tree
Showing 35 changed files with 104 additions and 22 deletions.
20 changes: 15 additions & 5 deletions src/scaffold/entry_type/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum FieldType {
#[serde(rename = "bool")]
Bool,
String,
#[serde(rename = "u8")]
U8,
#[serde(rename = "u32")]
U32,
#[serde(rename = "i32")]
Expand Down Expand Up @@ -61,6 +63,7 @@ impl std::fmt::Display for FieldType {
let str = match self {
FieldType::Bool => "bool",
FieldType::String => "String",
FieldType::U8 => "u8",
FieldType::U32 => "u32",
FieldType::I32 => "i32",
FieldType::F32 => "f32",
Expand All @@ -81,6 +84,7 @@ impl FieldType {
vec![
FieldType::String,
FieldType::Bool,
FieldType::U8,
FieldType::U32,
FieldType::I32,
FieldType::F32,
Expand Down Expand Up @@ -121,6 +125,7 @@ impl FieldType {
match self {
Bool => quote!(bool),
String => quote!(String),
U8 => quote!(u8),
U32 => quote!(u32),
I32 => quote!(i32),
F32 => quote!(f32),
Expand All @@ -143,6 +148,7 @@ impl FieldType {
match self {
Bool => "boolean",
String => "string",
U8 => "number",
U32 => "number",
I32 => "number",
F32 => "number",
Expand Down Expand Up @@ -515,11 +521,15 @@ impl EntryDefinition {
ts_type
),
Cardinality::Vector => {
format!(
" {}: Array<{}>;",
&field.field_name.to_case(Case::Snake),
ts_type
)
if matches!(field.field_type, FieldType::U8) {
format!(" {}: Uint8Array;", &field.field_name.to_case(Case::Snake),)
} else {
format!(
" {}: Array<{}>;",
&field.field_name.to_case(Case::Snake),
ts_type
)
}
}
};
ts_interface.push_str(&ts_field);
Expand Down
19 changes: 14 additions & 5 deletions src/scaffold/entry_type/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fn choose_field(
}

let widget = (!no_ui)
.then(|| choose_widget(&field_type, field_types_templates))
.then(|| choose_widget(&field_type, &cardinality, field_types_templates))
.transpose()?
.flatten();

Expand Down Expand Up @@ -338,7 +338,7 @@ fn choose_field(
};

let widget = (!no_ui)
.then(|| choose_widget(&field_type, field_types_templates))
.then(|| choose_widget(&field_type, &cardinality, field_types_templates))
.transpose()?
.flatten();

Expand All @@ -347,6 +347,7 @@ fn choose_field(

fn choose_widget(
field_type: &FieldType,
cardinality: &Cardinality,
field_types_templates: &FileTree,
) -> ScaffoldResult<Option<String>> {
let path = PathBuf::new().join(field_type.to_string());
Expand All @@ -364,9 +365,17 @@ fn choose_widget(
return Ok(None);
}

let should_scaffold_ui = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Should UI be generated for this field?")
.interact()?;
let should_scaffold_ui = {
// Skip widget prompt for Vec<u8> field types
if matches!(field_type, FieldType::U8) && matches!(cardinality, Cardinality::Vector)
{
false
} else {
Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Should UI be generated for this field?")
.interact()?
}
};

if !should_scaffold_ui {
return Ok(None);
Expand Down
2 changes: 1 addition & 1 deletion templates/generic/field-types/Vec/type.hbs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Array<{{field_type.type}}>
{{#if (eq field_type.type "u8")}}Uint8Array{{else}}Array<{{field_type.type}}>{{/if}}
1 change: 1 addition & 0 deletions templates/generic/field-types/u8/default.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#if (eq cardinality "vector")}}[]{{else}}0{{/if}}
1 change: 1 addition & 0 deletions templates/generic/field-types/u8/sample.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
1 change: 1 addition & 0 deletions templates/generic/field-types/u8/type.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
number
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ export class Create{{pascal_case entry_type.name}} extends LitElement {
{{#if (eq cardinality "option")}}
@property()
{{camel_case field_name}}: {{> (concat field_type.type "/type") }} | undefined;
{{else}}
@property()
{{else}}
{{#if (eq field_type.type "u8")}}
{{camel_case field_name}}!: Uint8Array;
{{else}}
{{camel_case field_name}}!: Array<{{> (concat field_type.type "/type") }}>;

{{/if}}
{{/if}}
{{/if}}
{{/if}}
Expand All @@ -46,8 +49,11 @@ export class Create{{pascal_case entry_type.name}} extends LitElement {

{{else}}
@state()
{{#if (eq field_type.type "u8")}}
_{{camel_case field_name}}: Uint8Array = [{{> (concat field_type.type "/" widget "/initial-value") field_type=field_type}}];
{{else}}
_{{camel_case field_name}}: Array<{{> (concat field_type.type "/type") }}> = [{{> (concat field_type.type "/" widget "/initial-value") field_type=field_type}}];

{{/if}}
{{/if}}
{{/if}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ export class Edit{{pascal_case entry_type.name}} extends LitElement {

{{else}}
@state()
{{#if (eq field_type.type "u8")}}
_{{camel_case field_name}}: Uint8Array = this.current{{pascal_case ../entry_type.name}}.{{snake_case field_name}};
{{else}}
_{{camel_case field_name}}: Array<{{> (concat field_type.type "/type") }}> = this.current{{pascal_case ../entry_type.name}}.{{snake_case field_name}};

{{/if}}
{{/if}}
{{/if}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${ {{variable_to_read}} }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<label for="{{label}}">{{label}}</label>
<input
name="{{label}}"
type="range"
min="0"
max="255"
.value=${ {{variable_to_read}} }
@change=${(e: any) => { {{variable_to_change}} = e.detail.value; } }
>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ interface Create{{pascal_case entry_type.name}}Props {
{{#each entry_type.fields}}
{{#if (not widget) }}
{{#if (eq cardinality "vector")}}
{{#if (eq field_type.type "u8")}}
{{camel_case field_name}}: Uint8Array,
{{else}}
{{camel_case field_name}}: {{> (concat field_type.type "/type") }}[],
{{/if}}
{{else}}
{{camel_case field_name}}{{#if (eq cardinality "single")}}{{/if}}: {{> (concat field_type.type "/type") }}{{#if (eq cardinality "option")}} | undefined{{/if}},
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ {{variable_to_read}} }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<label htmlFor="{{label}}">{{label}}</label>
<input type="number" name="{{label}}" {{#if variable_to_read}}value={ {{variable_to_read}} }{{/if}} onChange={e => set{{pascal_case variable_to_change}}(parseInt(e.target.value))} min="0" max="255" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ const appClientContext = getContext<ClientContext>(clientContext);
{{#if (not (eq cardinality "vector" ) )}}
let {{camel_case field_name}}: {{> (concat field_type.type "/type") }}{{#if (eq cardinality "option")}} | undefined{{/if}} = {{> (concat field_type.type "/" widget "/initial-value") field_type=field_type}};
{{else}}
{{#if (eq field_type.type "u8")}}
let {{camel_case field_name}}: Uint8Array = [{{> (concat field_type.type "/" widget "/initial-value") field_type=field_type}}];
{{else}}
let {{camel_case field_name}}: Array<{{> (concat field_type.type "/type")}}> = [{{> (concat field_type.type "/" widget "/initial-value") field_type=field_type}}];
{{/if}}
{{/if}}
{{/if}}
{{/each}}
{{#each entry_type.fields}}
{{#if (not widget) }}
{{#if (eq cardinality "vector")}}
{{#if (eq field_type.type "u8")}}
export let {{camel_case field_name}}!: Uint8Array;
{{else}}
export let {{camel_case field_name}}!: Array<{{> (concat field_type.type "/type") }}>;
{{/if}}
{{else}}
export let {{camel_case field_name}}{{#if (eq cardinality "single")}}!{{/if}}: {{> (concat field_type.type "/type") }}{{#if (eq cardinality "option")}} | undefined{{/if}};
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ let current{{pascal_case entry_type.name}}: {{pascal_case entry_type.name}} = de
{{#if (not (eq cardinality "vector" ) )}}
let {{camel_case field_name}}: {{> (concat field_type.type "/type")}} | undefined = current{{pascal_case ../entry_type.name}}.{{snake_case field_name}};
{{else}}
{{#if (eq field_type.type "u8")}}
let {{camel_case field_name}}: Uint8Array | undefined> = current{{pascal_case ../entry_type.name}}.{{snake_case field_name}};
{{else}}
let {{camel_case field_name}}: Array<{{> (concat field_type.type "/type")}} | undefined> = current{{pascal_case ../entry_type.name}}.{{snake_case field_name}};
{{/if}}
{{/if}}
{{/if}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name="{{label}}" type="range" min="0" bind:value="{{variable_to_read}}" />
<input name="{{label}}" type="range" min="0" bind:value={ {{variable_to_read}} } />
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name=""{{label}} type="range" bind:value="{{variable_to_read}}" />
<input name="{{label}}" type="range" bind:value={ {{variable_to_read}} } />
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name=""{{label}} type="range" min="0" bind:value="{{variable_to_read}}" />
<input name="{{label}}" type="range" min="0" bind:value={ {{variable_to_read}} } />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ {{variable_to_read}} }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name="{{label}}" type="range" min="0" max="255" bind:value={ {{variable_to_read}} } />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export default defineComponent({
{{#if (not (eq cardinality "vector" ) )}}
{{camel_case field_name}}: {{> (concat field_type.type "/type")}}{{#if (eq cardinality "option")}}| undefined{{/if}};
{{else}}
{{#if (eq field_type.type "u8")}}
{{camel_case field_name}}: Uint8Array;
{{else}}
{{camel_case field_name}}: Array<{{> (concat field_type.type "/type")}}>;
{{/if}}
{{/if}}
{{/if}}
{{/each}}
} {
Expand Down Expand Up @@ -102,7 +106,7 @@ export default defineComponent({
{{#if (eq cardinality "single") }}
{{snake_case field_name}}: this.{{camel_case field_name}}!,
{{else}}
{{snake_case field_name}}: this.{{camel_case field_name}}{{#if (eq cardinality "vector") }} as Array<{{> (concat field_type.type "/type") }}>{{/if}},
{{snake_case field_name}}: this.{{camel_case field_name}}{{#if (eq cardinality "vector") }}{{#if field_type.type "u8"}} as Uint8Array{{else}}as Array<{{> (concat field_type.type "/type") }}>{{/if}}{{/if}},
{{/if}}
{{/each}}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export default defineComponent({
{{#if (not (eq cardinality "vector" ) )}}
{{camel_case field_name}}: {{> (concat field_type.type "/type")}};
{{else}}
{{#if (eq field_type.type "u8")}}
{{camel_case field_name}}: Uint8Array;
{{else}}
{{camel_case field_name}}: Array<{{> (concat field_type.type "/type")}}>;
{{/if}}
{{/if}}
{{/if}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name=""{{label}} type="range" min="0" v-model="{{variable_to_read}}" />
<input name="{{label}}" type="range" min="0" v-model="{{variable_to_read}}" />
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name=""{{label}} type="range" v-model="{{variable_to_read}}" />
<input name="{{label}}" type="range" v-model="{{variable_to_read}}" />
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name=""{{label}} type="range" min="0" v-model="{{variable_to_read}}" />
<input name="{{label}}" type="range" min="0" v-model="{{variable_to_read}}" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{{raw}}}} {{ {{{{/raw}}}} {{variable_to_read}} {{{{raw}}}} }} {{{{/raw}}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<label for="{{label}}">{{label}}</label>
<input name="{{label}}" type="range" min="0" max="255" v-model="{{variable_to_read}}" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true

0 comments on commit a2812ae

Please sign in to comment.