Skip to content

Commit

Permalink
Merge branch 'main' into loops
Browse files Browse the repository at this point in the history
  • Loading branch information
pufferfish101007 committed Aug 17, 2023
2 parents c790098 + 6c40eac commit d0acbaf
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 202 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ jobs:
args: --all -- --check

buildwasm:
name: Build WASM
name: Build WASM & website
if: github.ref_name == 'main'
runs-on: ubuntu-latest
steps:
- name: Checkout sources
Expand Down Expand Up @@ -101,9 +102,7 @@ jobs:
run: chmod +x build.sh && ./build.sh -pWV

- name: Move files for gh pages
run: |
[ $GITHUB_REF != "refs/head/main" ] && exit 0
mv ./playground/dist docs && cp docs/index.html docs/404.html
run: mv ./playground/dist docs && cp docs/index.html docs/404.html

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
Expand Down
61 changes: 53 additions & 8 deletions enum-field-getter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro::TokenStream;
use proc_macro_error::{emit_warning, proc_macro_error};
use proc_macro_error::{emit_warning, proc_macro_error, abort_call_site};
use quote::{format_ident, quote};
use syn::{parse_macro_input, Data, DeriveInput, Fields, Type};

Expand All @@ -13,7 +13,9 @@ pub fn enum_field_getter(stream: TokenStream) -> TokenStream {
let variants = enum_data.variants.iter();
let name = info.ident;
let mut field_info: HashMap<String, (Type, Vec<String>)> = HashMap::new();
let mut tuple_field_info: HashMap<usize, (Type, Vec<String>)> = HashMap::new();
let mut incompatible = HashSet::<String>::new();
let mut tuple_incompatible = HashSet::<usize>::new();
for variant in variants {
if let Fields::Named(_) = variant.fields {
for field in &variant.fields {
Expand All @@ -31,28 +33,31 @@ pub fn enum_field_getter(stream: TokenStream) -> TokenStream {
}).or_insert(df);
}
} else if let Fields::Unnamed(_) = variant.fields {
emit_warning!(variant, "getters are not emitted for tuple variants");
/*for field in &variant.fields {
let ident = field.ident.clone().unwrap().to_string();
for (i, field) in variant.fields.iter().enumerate() {
let field_ty = field.ty.clone();
let df = (field_ty.clone(), vec![variant.ident.to_string()]);
field_info.entry(ident.clone()).and_modify(|info| {
tuple_field_info.entry(i).and_modify(|info| {
let (ty, used_variants) = info;
if quote!{#field_ty}.to_string() != quote!{#ty}.to_string() {
emit_warning!(field, "fields must be the same type across all variants - no getter will be emitted for this field");
incompatible.insert(ident.clone());
tuple_incompatible.insert(i);
} else {
used_variants.push(variant.ident.to_string());
}
}).or_insert(df);
}*/
}
}
}
for removeable in incompatible {
field_info.remove(&removeable);
}
for tuple_removeable in tuple_incompatible {
tuple_field_info.remove(&tuple_removeable);
}
let getters = field_info.keys().map(|k| format_ident!("{}", k));
let getters_mut = field_info.keys().map(|k| format_ident!("{}_mut", k));
let types = field_info.values().map(|v| v.0.clone());
let types_mut = types.clone();
let field_info_vec = field_info.iter().collect::<Vec<_>>();
let matches = field_info_vec.iter().map(|(k, v)| {
let variants =
Expand All @@ -70,17 +75,57 @@ pub fn enum_field_getter(stream: TokenStream) -> TokenStream {
}
}
});
let matches_mut = matches.clone();
let tuple_getters = tuple_field_info.keys().map(|k| format_ident!("get_{}", k));
let tuple_getters_mut = tuple_field_info.keys().map(|k| format_ident!("get_{}_mut", k));
let tuple_types = tuple_field_info.values().map(|v| v.0.clone());
let tuple_types_mut = tuple_types.clone();
let tuple_field_info_vec = tuple_field_info.iter().collect::<Vec<_>>();
let tuple_matches = tuple_field_info_vec.iter().map(|(k, v)| {
let variants =
v.1.clone()
.iter()
.map(|v| format_ident!("{}", v))
.collect::<Vec<_>>();
let preceding = vec![format_ident!("_"); **k];
let preceding_quote = vec![quote! { #(#preceding,)* }; variants.len()];
let field = vec![format_ident!("val_{}", k); variants.len()];
quote! {
match self {
#(
Self::#variants(#preceding_quote #field, .. ) => Some(#field),
)*
_ => None,
}
}
});
let tuple_matches_mut = tuple_matches.clone();
quote! {
impl #name {
#(
pub fn #getters (&self) -> Option<&#types> {
#matches
}
)*
#(
pub fn #tuple_getters (&self) -> Option<&#tuple_types> {
#tuple_matches
}
)*
#(
pub fn #getters_mut (&mut self) -> Option<&mut #types_mut> {
#matches_mut
}
)*
#(
pub fn #tuple_getters_mut (&mut self) -> Option<&mut #tuple_types_mut> {
#tuple_matches_mut
}
)*
}
}
.into()
} else {
panic!("macro can only be used on enums");
abort_call_site!("macro can only be used on enums");
}
}
13 changes: 6 additions & 7 deletions playground/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { RouterLink, RouterView } from 'vue-router'

<template>
<div class="wrapper">
<nav>
<RouterLink to="/"><img alt="HyperQuark logo" class="logo" src="/logo.png" />HyperQuark</RouterLink>
<RouterLink to="/about">About</RouterLink>
<a href="https://github.com/hyperquark/">Github</a>
</nav>
</div>

<nav>
<RouterLink to="/"><img alt="HyperQuark logo" class="logo" src="/logo.png" />HyperQuark</RouterLink>
<RouterLink to="/about">About</RouterLink>
<a href="https://github.com/hyperquark/">Github</a>
</nav>
</div>
<RouterView />
</template>

Expand Down
19 changes: 18 additions & 1 deletion playground/router/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { createRouter, createWebHistory } from 'vue-router';
import { h, ref } from 'vue';
import Loading from '../components/Loading.vue';

const view = (name) => () => import(`../views/${name}.vue`);
let componentCache = Object.setPrototypeOf({}, null);

const view = (name) => ({
setup() {
let component = componentCache[name];
const loading = ref(!Boolean(component));
if (loading.value) {
import(`../views/${name}.vue`).then((c) => {
loading.value = false;
component = c.default;
componentCache[name] = component;
});
}
return () => loading.value ? h(Loading) : h(component);
}
});

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
Expand Down
Loading

0 comments on commit d0acbaf

Please sign in to comment.