From d6a2642a6ef2cb2860ec2ba98f9b94b5085f73c3 Mon Sep 17 00:00:00 2001 From: Alexis Fontaine Date: Thu, 13 Oct 2022 23:56:26 +0200 Subject: [PATCH 1/2] Rename the derive macro helper attribute `builder` to `prop` --- docs/next/basics/components.md | 4 ++-- packages/sycamore-macro/src/component/mod.rs | 4 ++-- packages/sycamore-macro/src/lib.rs | 2 +- packages/sycamore-macro/src/props.rs | 14 +++++++------- packages/sycamore-router/src/router.rs | 6 +++--- packages/sycamore/src/suspense.rs | 2 +- website/src/content.rs | 2 +- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/next/basics/components.md b/docs/next/basics/components.md index 588d868b9..f2f6ddc6c 100644 --- a/docs/next/basics/components.md +++ b/docs/next/basics/components.md @@ -118,14 +118,14 @@ view! { ## Default props -Some property fields might have a default value. Use the `#[builder(default)]` attribute to allow +Some property fields might have a default value. Use the `#[prop(default)]` attribute to allow omitting the property when constructing the component. ```rust #[derive(Props)] struct MyProps { name: String, - #[builder(default)] + #[prop(default)] email: String, } diff --git a/packages/sycamore-macro/src/component/mod.rs b/packages/sycamore-macro/src/component/mod.rs index bc86096a2..00e3ca91f 100644 --- a/packages/sycamore-macro/src/component/mod.rs +++ b/packages/sycamore-macro/src/component/mod.rs @@ -306,14 +306,14 @@ fn inline_props_impl(item: &mut ItemFn) -> Result { syn::GenericParam::Type(ty) => { let ty = &ty.ident; Some(quote! { - #[builder(default, setter(skip))] + #[prop(default, setter(skip))] #phantom_ident: ::std::marker::PhantomData<#ty> }) } syn::GenericParam::Lifetime(lt) => { let lt = <.lifetime; Some(quote! { - #[builder(default, setter(skip))] + #[prop(default, setter(skip))] #phantom_ident: ::std::marker::PhantomData<&#lt ()> }) } diff --git a/packages/sycamore-macro/src/lib.rs b/packages/sycamore-macro/src/lib.rs index 1201dd350..f1c026e9e 100644 --- a/packages/sycamore-macro/src/lib.rs +++ b/packages/sycamore-macro/src/lib.rs @@ -68,7 +68,7 @@ pub fn component(args: TokenStream, item: TokenStream) -> TokenStream { } /// The derive macro for `Props`. The macro creates a builder-like API used in the [`view!`] macro. -#[proc_macro_derive(Props, attributes(builder))] +#[proc_macro_derive(Props, attributes(prop))] pub fn derive_props(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); diff --git a/packages/sycamore-macro/src/props.rs b/packages/sycamore-macro/src/props.rs index 2593e6379..8961bf1d1 100644 --- a/packages/sycamore-macro/src/props.rs +++ b/packages/sycamore-macro/src/props.rs @@ -669,7 +669,7 @@ mod struct_info { pub fn new(attrs: &[syn::Attribute]) -> Result { let mut result = TypeBuilderAttr::default(); for attr in attrs { - if path_to_single_string(&attr.path).as_deref() != Some("builder") { + if path_to_single_string(&attr.path).as_deref() != Some("prop") { continue; } @@ -747,7 +747,7 @@ mod struct_info { let call_func = quote!(#call_func); Error::new_spanned( &call.func, - format!("Illegal builder setting group {}", call_func), + format!("Illegal prop setting group {}", call_func), ) })?; match subsetting_name.as_str() { @@ -759,7 +759,7 @@ mod struct_info { } _ => Err(Error::new_spanned( &call.func, - format!("Illegal builder setting group name {}", subsetting_name), + format!("Illegal prop setting group name {}", subsetting_name), )), } } @@ -875,7 +875,7 @@ mod field_info { impl FieldBuilderAttr { pub fn with(mut self, attrs: &[syn::Attribute]) -> Result { for attr in attrs { - if path_to_single_string(&attr.path).as_deref() != Some("builder") { + if path_to_single_string(&attr.path).as_deref() != Some("prop") { continue; } @@ -965,7 +965,7 @@ mod field_info { let call_func = quote!(#call_func); Error::new_spanned( &call.func, - format!("Illegal builder setting group {}", call_func), + format!("Illegal prop setting group {}", call_func), ) })?; match subsetting_name.as_ref() { @@ -977,7 +977,7 @@ mod field_info { } _ => Err(Error::new_spanned( &call.func, - format!("Illegal builder setting group name {}", subsetting_name), + format!("Illegal prop setting group name {}", subsetting_name), )), } } @@ -1011,7 +1011,7 @@ mod field_info { if let (Some(skip), None) = (&self.setter.skip, &self.default) { return Err(Error::new( *skip, - "#[builder(skip)] must be accompanied by default or default_code", + "#[prop(skip)] must be accompanied by default or default_code", )); } diff --git a/packages/sycamore-router/src/router.rs b/packages/sycamore-router/src/router.rs index 9c70a28eb..1c772a4e1 100644 --- a/packages/sycamore-router/src/router.rs +++ b/packages/sycamore-router/src/router.rs @@ -151,7 +151,7 @@ where { view: F, integration: I, - #[builder(default, setter(skip))] + #[prop(default, setter(skip))] _phantom: PhantomData<&'a (R, G)>, } @@ -184,7 +184,7 @@ where view: F, integration: I, route: R, - #[builder(default, setter(skip))] + #[prop(default, setter(skip))] _phantom: PhantomData<&'a G>, } @@ -301,7 +301,7 @@ where { view: F, route: R, - #[builder(default, setter(skip))] + #[prop(default, setter(skip))] _phantom: PhantomData<&'a (R, G)>, } diff --git a/packages/sycamore/src/suspense.rs b/packages/sycamore/src/suspense.rs index 062b7fbd0..5475c4358 100644 --- a/packages/sycamore/src/suspense.rs +++ b/packages/sycamore/src/suspense.rs @@ -21,7 +21,7 @@ struct SuspenseState { #[derive(Props, Debug)] pub struct SuspenseProps<'a, G: GenericNode> { /// The fallback [`View`] to display while the child nodes are being awaited. - #[builder(default)] + #[prop(default)] fallback: View, children: Children<'a, G>, } diff --git a/website/src/content.rs b/website/src/content.rs index ea6ba2027..e67a4be04 100644 --- a/website/src/content.rs +++ b/website/src/content.rs @@ -71,7 +71,7 @@ pub fn OutlineView(cx: Scope, outline: Vec) -> View { #[derive(Props)] pub struct ContentProps { pub data: MarkdownPage, - #[builder(default, setter(strip_option))] + #[prop(default, setter(strip_option))] pub sidebar: Option<(String, SidebarData)>, } From 6cf06986c439e4973a081192494d34245fc55b68 Mon Sep 17 00:00:00 2001 From: Alexis Fontaine Date: Fri, 14 Oct 2022 00:26:52 +0200 Subject: [PATCH 2/2] Remove needless borrows --- docs/src/main.rs | 4 ++-- packages/sycamore-macro/src/props.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/src/main.rs b/docs/src/main.rs index fa01c7ead..2084b4ff7 100644 --- a/docs/src/main.rs +++ b/docs/src/main.rs @@ -138,7 +138,7 @@ fn build_dir(base: &Path, output: &Path) -> Result<(), Box> { let page = parse(entry.path())?; let output_dir: PathBuf = out_dir.join(output); let output_path: PathBuf = output_dir - .join(entry.path().strip_prefix(&base).unwrap()) + .join(entry.path().strip_prefix(base).unwrap()) .with_extension("json"); let output_json = serde_json::to_string(&page).unwrap(); @@ -163,7 +163,7 @@ fn generate_sitemap_for_dir( .map(|e| e.unwrap()) .filter(|e| e.path().is_file()) { - let path = entry.path().strip_prefix(&dir).unwrap().with_extension(""); + let path = entry.path().strip_prefix(dir).unwrap().with_extension(""); let path_str = path.iter().fold(String::new(), |acc, c| { format!("{}/{}", acc, c.to_str().unwrap()) }); diff --git a/packages/sycamore-macro/src/props.rs b/packages/sycamore-macro/src/props.rs index 8961bf1d1..d4b375974 100644 --- a/packages/sycamore-macro/src/props.rs +++ b/packages/sycamore-macro/src/props.rs @@ -127,7 +127,7 @@ mod struct_info { ref name, ref builder_name, .. - } = *self; + } = self; let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl(); let all_fields_param = syn::GenericParam::Type( syn::Ident::new("PropsFields", proc_macro2::Span::call_site()).into(), @@ -270,7 +270,7 @@ mod struct_info { pub fn field_impl(&self, field: &FieldInfo) -> Result { let StructInfo { ref builder_name, .. - } = *self; + } = self; let descructuring = self.included_fields().map(|f| { if f.ordinal == field.ordinal { @@ -282,7 +282,7 @@ mod struct_info { }); let reconstructing = self.included_fields().map(|f| f.name); - let &FieldInfo { + let FieldInfo { name: ref field_name, ty: ref field_type, .. @@ -356,7 +356,7 @@ mod struct_info { { field.type_from_inside_option().ok_or_else(|| { Error::new_spanned( - &field_type, + field_type, "can't `strip_option` - field is not `Option<...>`", ) })? @@ -537,7 +537,7 @@ mod struct_info { ref name, ref builder_name, .. - } = *self; + } = self; let generics = self.modify_generics(|g| { let index_after_lifetime_in_generics = g