Skip to content

Commit

Permalink
Fix some code formatting issues (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
teohhanhui authored Aug 14, 2023
1 parent cec4146 commit 28b4d02
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 55 deletions.
12 changes: 6 additions & 6 deletions autosurgeon-derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl ReconcileWith {
module_name,
)
} else {
let func = quote! {#module_name::reconcile};
let func = quote!(#module_name::reconcile);
crate::reconcile::field_wrapper::nokey_wrapper(ty, wrapper_tyname, func)
}
}
Expand All @@ -105,7 +105,9 @@ impl ReconcileWith {
Self::Function { .. } => None,
Self::Module { module_name, .. } | Self::With { module_name, .. } => {
let k = syn::Lifetime::new("'k", Span::mixed_site());
Some(quote! { type Key<#k> = #module_name::Key<#k>; })
Some(quote! {
type Key<#k> = #module_name::Key<#k>;
})
}
}
}
Expand Down Expand Up @@ -171,10 +173,8 @@ impl HydrateWith {

pub(crate) fn hydrate_with(&self) -> TokenStream {
match self {
Self::Function { function_name } => quote! { #function_name },
Self::Module { module_name } => quote! {
#module_name::hydrate
},
Self::Function { function_name } => quote!(#function_name),
Self::Module { module_name } => quote!(#module_name::hydrate),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion autosurgeon-derive/src/hydrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ fn gen_newtype_struct_wrapper(

let inner_ty = &field.ty;

let inner_ty = quote_spanned!(field.span() => #inner_ty);
let inner_ty = quote_spanned!(field.span()=> #inner_ty);

if let Some(hydrate_with) = attrs.hydrate_with().map(|h| h.hydrate_with()) {
Ok(quote! {
Expand Down
7 changes: 5 additions & 2 deletions autosurgeon-derive/src/hydrate/named_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ impl<'a> NamedField<'a> {
let #name = #function_name(doc, &#obj_ident, #string_name.into())?;
}
} else {
quote_spanned!(self.field.span() => let #name = autosurgeon::hydrate_prop(doc, &#obj_ident, #string_name)?;)
let span = self.field.span();
quote_spanned! {span=>
let #name = autosurgeon::hydrate_prop(doc, &#obj_ident, #string_name)?;
}
}
}

pub(crate) fn initializer(&self) -> TokenStream {
let name = &self.name;
quote! {#name}
quote!(#name)
}
}
3 changes: 2 additions & 1 deletion autosurgeon-derive/src/hydrate/newtype_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl<'a> NewtypeField<'a> {
let #target = #hydrate_func(doc, obj, #prop_ident.into())?;
}
} else {
quote_spanned! {self.field.span() =>
let span = self.field.span();
quote_spanned! {span=>
let #target = autosurgeon::hydrate_prop(doc, obj, #prop_ident)?;
}
}
Expand Down
7 changes: 5 additions & 2 deletions autosurgeon-derive/src/hydrate/unnamed_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ impl UnnamedField {
let #name = #function_name(doc, &#obj_ident, #idx.into())?;
}
} else {
quote_spanned!(self.field.span() => let #name = autosurgeon::hydrate_prop(doc, &#obj_ident, #idx)?;)
let span = self.field.span();
quote_spanned! {span=>
let #name = autosurgeon::hydrate_prop(doc, &#obj_ident, #idx)?;
}
}
}

pub(crate) fn initializer(&self) -> TokenStream {
let name = self.name();
quote! { #name }
quote!(#name)
}

fn name(&self) -> syn::Ident {
Expand Down
12 changes: 6 additions & 6 deletions autosurgeon-derive/src/reconcile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn derive_reconcile(input: proc_macro::TokenStream) -> proc_macro::TokenStre
get_key,
}) => {
let key_lifetime = syn::Lifetime::new("'k", Span::mixed_site());
let key_type_def = key_type_def.unwrap_or_else(|| quote! {});
let key_type_def = key_type_def.unwrap_or_else(|| quote!());
let key_type = key_type.unwrap_or(quote! {
type Key<#key_lifetime> = autosurgeon::reconcile::NoKey;
});
Expand Down Expand Up @@ -214,14 +214,14 @@ fn newtype_struct_impl(field: &syn::Field) -> Result<ReconcileImpl, error::Deriv
key_type: reconcile_with.key_type(),
key_type_def: None,
hydrate_key: reconcile_with.hydrate_key(),
get_key: reconcile_with.get_key(quote! {&self.0}),
get_key: reconcile_with.get_key(quote!(&self.0)),
})
} else {
Ok(ReconcileImpl {
reconcile: quote_spanned! {field.span() => self.0.reconcile(reconciler)},
key_type: Some(
quote! { type Key<#key_lifetime> = <#field_ty as Reconcile>::Key<#key_lifetime>; },
),
reconcile: quote_spanned!(field.span()=> self.0.reconcile(reconciler)),
key_type: Some(quote! {
type Key<#key_lifetime> = <#field_ty as Reconcile>::Key<#key_lifetime>;
}),
key_type_def: None,
hydrate_key: Some(quote! {
fn hydrate_key<#key_lifetime, D: autosurgeon::ReadDoc>(
Expand Down
53 changes: 34 additions & 19 deletions autosurgeon-derive/src/reconcile/enum_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ impl<'a> Variant<'a> {
match self {
Self::Unit { name } => {
let name_string = name.to_string();
Ok(quote! { Self::#name => reconciler.str(#name_string) })
Ok(quote! {
Self::#name => reconciler.str(#name_string)
})
}
Self::NewType {
name,
Expand All @@ -81,7 +83,7 @@ impl<'a> Variant<'a> {
let name_string = name.to_string();
let ty = inner_ty;
let reconciler = attrs.reconcile_with().map(|reconcile_with| {
quote!{
quote! {
struct ___EnumNewtypeVisitor<'a>(&'a #ty);
impl<'a> autosurgeon::Reconcile for ___EnumNewtypeVisitor<'a> {
type Key<'k> = #reconcile_with::Key<'a>;
Expand All @@ -102,7 +104,7 @@ impl<'a> Variant<'a> {
m.retain(|k, _| k == #name_string)?;
m.put(#name_string, ___EnumNewtypeVisitor(&v))?;
}
}).unwrap_or_else(|| quote!{
}).unwrap_or_else(|| quote! {
m.retain(|k, _| k == #name_string)?;
m.put(#name_string, v)?;
});
Expand Down Expand Up @@ -194,7 +196,7 @@ impl<'a> EnumKeyInnerType<'a> {
key_lifetime: &syn::Lifetime,
) -> Option<TokenStream> {
match self {
EnumKeyInnerType::Unit => Some(quote! { #variant_name}),
EnumKeyInnerType::Unit => Some(quote!(#variant_name)),
EnumKeyInnerType::NewType(nt) => {
Some(if let Some(reconcile_with) = nt.attrs.reconcile_with() {
quote! {
Expand Down Expand Up @@ -236,10 +238,14 @@ impl<'a> EnumKeyInnerType<'a> {
Self::NewType(t) => {
let prop = variant_name.to_string();
if let Some(reconcile_with) = t.attrs.reconcile_with() {
quote! {Ok(#reconcile_with::hydrate_key(doc, &#obj_id_ident, #prop.into())?.map(#key_type_name::#variant_name)), }
quote! {
Ok(#reconcile_with::hydrate_key(doc, &#obj_id_ident, #prop.into())?.map(#key_type_name::#variant_name)),
}
} else {
let t = t.ty;
quote! {Ok(<#t as autosurgeon::Reconcile>::hydrate_key(doc, &#obj_id_ident, #prop.into())?.map(#key_type_name::#variant_name)), }
quote! {
Ok(<#t as autosurgeon::Reconcile>::hydrate_key(doc, &#obj_id_ident, #prop.into())?.map(#key_type_name::#variant_name)),
}
}
}
Self::Struct(keyfield) => {
Expand Down Expand Up @@ -307,7 +313,7 @@ impl<'a> EnumKeyVariant<'a> {
if EnumKeyInnerType::Unit == self.ty {
let name = &self.name;
let name_str = self.name.to_string();
let variant_name = quote! {#outer_name::#name};
let variant_name = quote!(#outer_name::#name);
Some(quote! {
#name_str => Ok(autosurgeon::reconcile::LoadKey::Found(#variant_name)),
})
Expand Down Expand Up @@ -423,11 +429,12 @@ impl<'a> EnumKey<'a> {
.filter_map(|v| v.key_type_variant_def(&key_lifetime));
let name = self.type_name();
let name_with_lifetime = if self.has_lifetime() {
quote! {#name<#key_lifetime>}
quote!(#name<#key_lifetime>)
} else {
quote! {#name}
quote!(#name)
};
Some(quote_spanned! { Span::mixed_site() =>
let span = Span::mixed_site();
Some(quote_spanned! {span=>
#[derive(Clone, PartialEq)]
#[allow(clippy::derive_partial_eq_without_eq)]
#vis enum #name_with_lifetime {
Expand Down Expand Up @@ -491,7 +498,11 @@ impl<'a> EnumKey<'a> {
_ => Ok(autosurgeon::reconcile::LoadKey::KeyNotFound)
},
Value::Object(ObjType::Map) => {
let Some(automerge::iter::MapRangeItem{key: discriminant_str, ..}) = doc.map_range(&#outer_id_ident, ..).next() else {
let Some(automerge::iter::MapRangeItem {
key: discriminant_str,
..
}) = doc.map_range(&#outer_id_ident, ..).next()
else {
return Ok(autosurgeon::reconcile::LoadKey::KeyNotFound);
};
match discriminant_str {
Expand All @@ -510,9 +521,13 @@ impl<'a> EnumKey<'a> {
let key_type = self.type_name();
let k = syn::Lifetime::new("'k", Span::mixed_site());
if self.has_lifetime() {
Some(quote! {type Key<#k> = #key_type<#k>;})
Some(quote! {
type Key<#k> = #key_type<#k>;
})
} else {
Some(quote! {type Key<#k> = #key_type;})
Some(quote! {
type Key<#k> = #key_type;
})
}
} else {
None
Expand Down Expand Up @@ -592,14 +607,12 @@ impl<'a> Field for EnumUnnamedField<'a> {

fn as_prop(&self) -> TokenStream {
let idx = self.idx;
quote! {#idx}
quote!(#idx)
}

fn accessor(&self) -> TokenStream {
let name = self.name();
quote! {
self.#name
}
quote!(self.#name)
}

fn name(&self) -> syn::Ident {
Expand Down Expand Up @@ -737,11 +750,13 @@ fn enum_with_fields_variant<F: VariantWithFields>(
let field_defs = fields.iter().map(|f| {
let name = f.name();
let ty = f.ty();
quote! {#name: &'__reconcile_visitor #ty}
quote! {
#name: &'__reconcile_visitor #ty
}
});
let matchers = fields.iter().map(|f| {
let name = f.name();
quote! {#name}
quote!(#name)
});
let constructors = fields.iter().map(|f| f.name());

Expand Down
30 changes: 15 additions & 15 deletions autosurgeon-derive/src/reconcile/struct_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,26 @@ pub(super) trait Field {
let value = quote!(#wrapper_tyname(&#accessor));
(wrapper, value)
}
None => (quote! {}, quote!(&#accessor)),
None => (quote!(), quote!(&#accessor)),
};
let get = match reconciler_ty {
ReconcilerType::Map => quote_spanned!(self.span() => #reconciler_ident.entry(#prop)),
ReconcilerType::Seq => quote_spanned!(self.span() => #reconciler_ident.get(#prop)?),
ReconcilerType::Map => quote_spanned!(self.span()=> #reconciler_ident.entry(#prop)),
ReconcilerType::Seq => quote_spanned!(self.span()=> #reconciler_ident.get(#prop)?),
};
let insert = match reconciler_ty {
ReconcilerType::Seq => {
quote_spanned!(self.span() => #reconciler_ident.insert(#prop, #value)?;)
quote_spanned!(self.span()=> #reconciler_ident.insert(#prop, #value)?;)
}
ReconcilerType::Map => {
quote_spanned!(self.span() => #reconciler_ident.put(#prop, #value)?;)
quote_spanned!(self.span()=> #reconciler_ident.put(#prop, #value)?;)
}
};
let update = match reconciler_ty {
ReconcilerType::Seq => {
quote_spanned!(self.span() => #reconciler_ident.set(#prop, #value)?;)
quote_spanned!(self.span()=> #reconciler_ident.set(#prop, #value)?;)
}
ReconcilerType::Map => {
quote_spanned!(self.span() => #reconciler_ident.put(#prop, #value)?;)
quote_spanned!(self.span()=> #reconciler_ident.put(#prop, #value)?;)
}
};
quote! {
Expand Down Expand Up @@ -134,12 +134,12 @@ impl<'a> Field for NamedField<'a> {

fn as_prop(&self) -> TokenStream {
let propname = &self.name.to_string();
quote! {#propname}
quote!(#propname)
}

fn accessor(&self) -> TokenStream {
let propname = &self.name;
quote! {self.#propname}
quote!(self.#propname)
}

fn name(&self) -> syn::Ident {
Expand Down Expand Up @@ -197,14 +197,12 @@ impl<'a> Field for TupleField<'a> {

fn as_prop(&self) -> TokenStream {
let idx = self.index;
quote! {#idx}
quote!(#idx)
}

fn accessor(&self) -> TokenStream {
let idx = syn::Index::from(self.index);
quote! {
self.#idx
}
quote!(self.#idx)
}

fn name(&self) -> syn::Ident {
Expand Down Expand Up @@ -277,7 +275,9 @@ impl<'a, F: Field + Clone> KeyField<'a, F> {
fn key_type_def(&self) -> proc_macro2::TokenStream {
let ty = &self.ty;
let lifetime = syn::Lifetime::new("'k", Span::mixed_site());
quote! {type Key<#lifetime> = std::borrow::Cow<#lifetime, #ty>;}
quote! {
type Key<#lifetime> = std::borrow::Cow<#lifetime, #ty>;
}
}

pub(super) fn key_type(&self) -> &syn::Type {
Expand Down Expand Up @@ -350,7 +350,7 @@ impl<'a, F: Field + Clone> KeyField<'a, F> {

pub(super) fn prop(&self) -> TokenStream {
let key_prop = self.field.as_prop();
quote! {#key_prop}
quote!(#key_prop)
}

fn get_key(&self) -> proc_macro2::TokenStream {
Expand Down
11 changes: 8 additions & 3 deletions autosurgeon/src/hydrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,14 @@ pub fn hydrate_path<'a, D: ReadDoc, H: Hydrate, P: IntoIterator<Item = Prop<'a>>
if obj == &automerge::ROOT {
return Ok(Some(hydrate(doc)?));
} else {
let Some(Parent{obj: parent_obj, prop: parent_prop, ..}) = doc.parents(obj)?.next() else {
return Ok(None)
};
let Some(Parent {
obj: parent_obj,
prop: parent_prop,
..
}) = doc.parents(obj)?.next()
else {
return Ok(None);
};
return hydrate_prop(doc, parent_obj, parent_prop);
}
}
Expand Down

0 comments on commit 28b4d02

Please sign in to comment.