Skip to content

Commit

Permalink
Updated Formatting Style
Browse files Browse the repository at this point in the history
  • Loading branch information
Redfire75369 committed Nov 25, 2023
1 parent 5321174 commit 3b59e0a
Show file tree
Hide file tree
Showing 69 changed files with 755 additions and 256 deletions.
14 changes: 12 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ pub(crate) enum Command {

#[command(about = "Runs a JavaScript file")]
Run {
#[arg(help = "The JavaScript file to run, Default: 'main.js'", required(false), default_value = "main.js")]
#[arg(
help = "The JavaScript file to run, Default: 'main.js'",
required(false),
default_value = "main.js"
)]
path: String,

#[arg(help = "Sets logging level, Default: ERROR", short, long, required(false), default_value = "ERROR")]
#[arg(
help = "Sets logging level, Default: ERROR",
short,
long,
required(false),
default_value = "ERROR"
)]
log_level: String,

#[arg(help = "Sets logging level to DEBUG", short, long)]
Expand Down
5 changes: 4 additions & 1 deletion ion-proc/src/attribute/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ impl Parse for Name {
if !string.starts_with('[') && !string.ends_with(']') {
Ok(Name::String(literal))
} else {
Err(Error::new(literal.span(), "Function name must not start with '[' or end with ']'"))
Err(Error::new(
literal.span(),
"Function name must not start with '[' or end with ']'",
))
}
} else if let Ok(other) = input.parse() {
Ok(Name::Symbol(other))
Expand Down
8 changes: 6 additions & 2 deletions ion-proc/src/class/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ pub(super) fn get_accessor_name(mut name: String, is_setter: bool) -> String {
name
}

pub(super) fn impl_accessor(ion: &TokenStream, method: ItemFn, ty: &Type, is_setter: bool) -> Result<(Method, Parameters)> {
pub(super) fn impl_accessor(
ion: &TokenStream, method: ItemFn, ty: &Type, is_setter: bool,
) -> Result<(Method, Parameters)> {
let expected_args = is_setter as i32;
let error_message = if is_setter {
format!("Expected Setter to have {} argument", expected_args)
Expand Down Expand Up @@ -130,7 +132,9 @@ pub(super) fn impl_accessor(ion: &TokenStream, method: ItemFn, ty: &Type, is_set
Ok((accessor, parameters))
}

pub(super) fn insert_accessor(accessors: &mut HashMap<String, Accessor>, name: String, getter: Option<Method>, setter: Option<Method>) {
pub(super) fn insert_accessor(
accessors: &mut HashMap<String, Accessor>, name: String, getter: Option<Method>, setter: Option<Method>,
) {
match accessors.entry(name) {
Entry::Occupied(mut o) => match (getter, setter) {
(Some(g), Some(s)) => *o.get_mut() = Accessor(Some(g), Some(s)),
Expand Down
21 changes: 17 additions & 4 deletions ion-proc/src/class/impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ pub(super) fn impl_js_class_impl(r#impl: &mut ItemImpl) -> Result<[ItemImpl; 2]>
let ion = &crate_from_attributes(&r#impl.attrs);

if !r#impl.generics.params.is_empty() {
return Err(Error::new(r#impl.generics.span(), "Native Class Impls cannot have generics."));
return Err(Error::new(
r#impl.generics.span(),
"Native Class Impls cannot have generics.",
));
}

if let Some(r#trait) = &r#impl.trait_ {
return Err(Error::new(r#trait.1.span(), "Native Class Impls cannot be for a trait."));
return Err(Error::new(
r#trait.1.span(),
"Native Class Impls cannot be for a trait.",
));
}

let r#type = *r#impl.self_ty.clone();
Expand Down Expand Up @@ -67,14 +73,21 @@ pub(super) fn impl_js_class_impl(r#impl: &mut ItemImpl) -> Result<[ItemImpl; 2]>

let constructor = match constructor {
Some(constructor) => constructor,
None => return Err(Error::new(r#impl.span(), "Native Class Impls must contain a constructor.")),
None => {
return Err(Error::new(
r#impl.span(),
"Native Class Impls must contain a constructor.",
))
}
};

let ident: Ident = parse2(quote_spanned!(r#type.span() => #r#type))?;
class_definition(ion, r#impl.span(), &r#type, &ident, constructor, specs)
}

fn parse_class_method(ion: &TokenStream, r#fn: &mut ImplItemFn, specs: &mut PrototypeSpecs, r#type: &Type) -> Result<Option<Method>> {
fn parse_class_method(
ion: &TokenStream, r#fn: &mut ImplItemFn, specs: &mut PrototypeSpecs, r#type: &Type,
) -> Result<Option<Method>> {
match &r#fn.vis {
Visibility::Public(_) => (),
_ => return Ok(None),
Expand Down
31 changes: 25 additions & 6 deletions ion-proc/src/class/impl/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ impl PrototypeSpecs {
}

pub(super) fn into_functions(self) -> Vec<Method> {
let mut functions = Vec::with_capacity(self.methods.0.len() + self.methods.1.len() + self.accessors.0.len() + self.accessors.1.len());
let mut functions = Vec::with_capacity(
self.methods.0.len() + self.methods.1.len() + self.accessors.0.len() + self.accessors.1.len(),
);

functions.extend(self.methods.0);
functions.extend(self.methods.1);
Expand All @@ -60,7 +62,9 @@ impl SpecFunctions {
}
}

fn methods_to_spec_function(ion: &TokenStream, span: Span, class: &Ident, methods: &[Method], r#static: bool) -> Result<ImplItemFn> {
fn methods_to_spec_function(
ion: &TokenStream, span: Span, class: &Ident, methods: &[Method], r#static: bool,
) -> Result<ImplItemFn> {
let ident = if r#static {
parse_quote!(ION_STATIC_FUNCTIONS)
} else {
Expand Down Expand Up @@ -96,11 +100,18 @@ fn methods_to_spec_function(ion: &TokenStream, span: Span, class: &Ident, method
.collect();
specs.push(parse_quote!(::mozjs::jsapi::JSFunctionSpec::ZERO));

spec_function(span, &ident, &function_ident, &specs, parse_quote!(::mozjs::jsapi::JSFunctionSpec))
spec_function(
span,
&ident,
&function_ident,
&specs,
parse_quote!(::mozjs::jsapi::JSFunctionSpec),
)
}

fn properties_to_spec_function(
ion: &TokenStream, span: Span, class: &Ident, properties: &[Property], accessors: &HashMap<String, Accessor>, r#static: bool,
ion: &TokenStream, span: Span, class: &Ident, properties: &[Property], accessors: &HashMap<String, Accessor>,
r#static: bool,
) -> Result<ImplItemFn> {
let ident: Ident = if r#static {
parse_quote!(ION_STATIC_PROPERTIES)
Expand All @@ -117,10 +128,18 @@ fn properties_to_spec_function(
accessors.values().for_each(|accessor| specs.extend(accessor.to_specs(ion, class)));
specs.push(parse_quote!(::mozjs::jsapi::JSPropertySpec::ZERO));

spec_function(span, &ident, &function_ident, &specs, parse_quote!(::mozjs::jsapi::JSPropertySpec))
spec_function(
span,
&ident,
&function_ident,
&specs,
parse_quote!(::mozjs::jsapi::JSPropertySpec),
)
}

fn spec_function(span: Span, ident: &Ident, function_ident: &Ident, specs: &[TokenStream], ty: Type) -> Result<ImplItemFn> {
fn spec_function(
span: Span, ident: &Ident, function_ident: &Ident, specs: &[TokenStream], ty: Type,
) -> Result<ImplItemFn> {
parse2(quote_spanned!(span => fn #function_ident() -> &'static [#ty] {
static #ident: &[#ty] = &[
#(#specs),*
Expand Down
4 changes: 3 additions & 1 deletion ion-proc/src/class/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ pub(super) struct Method {
pub(super) names: Vec<Name>,
}

pub(super) fn impl_method<F>(ion: &TokenStream, mut method: ItemFn, ty: &Type, predicate: F) -> Result<(Method, Parameters)>
pub(super) fn impl_method<F>(
ion: &TokenStream, mut method: ItemFn, ty: &Type, predicate: F,
) -> Result<(Method, Parameters)>
where
F: FnOnce(&Signature) -> Result<()>,
{
Expand Down
3 changes: 2 additions & 1 deletion ion-proc/src/class/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl Property {

for attr in &con.attrs {
if attr.path().is_ident("ion") {
let args: Punctuated<PropertyAttribute, Token![,]> = attr.parse_args_with(Punctuated::parse_terminated)?;
let args: Punctuated<PropertyAttribute, Token![,]> =
attr.parse_args_with(Punctuated::parse_terminated)?;

for arg in args {
match arg {
Expand Down
41 changes: 34 additions & 7 deletions ion-proc/src/class/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ pub(super) fn impl_js_class_struct(r#struct: &mut ItemStruct) -> Result<[ItemImp
}

if !r#struct.generics.params.is_empty() {
return Err(Error::new(r#struct.generics.span(), "Native Class Structs cannot have generics."));
return Err(Error::new(
r#struct.generics.span(),
"Native Class Structs cannot have generics.",
));
}

let ident = &r#struct.ident;
let r#type: Type = parse2(quote_spanned!(ident.span() => #ident))?;
let super_field;
let super_type;

let err = Err(Error::new(r#struct.span(), "Native Class Structs must have at least a reflector field."));
let err = Err(Error::new(
r#struct.span(),
"Native Class Structs must have at least a reflector field.",
));
match &r#struct.fields {
Fields::Named(fields) => match fields.named.first() {
Some(field) => {
Expand All @@ -78,14 +84,24 @@ pub(super) fn impl_js_class_struct(r#struct: &mut ItemStruct) -> Result<[ItemImp
return Err(Error::new(super_type.span(), "Superclass Type must be a path."));
}

class_impls(ion, r#struct.span(), &ident.to_string(), &r#type, &super_field, &super_type)
class_impls(
ion,
r#struct.span(),
&ident.to_string(),
&r#type,
&super_field,
&super_type,
)
}

fn class_impls(ion: &TokenStream, span: Span, name: &str, r#type: &Type, super_field: &Member, super_type: &Type) -> Result<[ItemImpl; 6]> {
fn class_impls(
ion: &TokenStream, span: Span, name: &str, r#type: &Type, super_field: &Member, super_type: &Type,
) -> Result<[ItemImpl; 6]> {
let from_value = impl_from_value(ion, span, name, r#type, false)?;
let from_value_mut = impl_from_value(ion, span, name, r#type, true)?;

let derived_from = parse2(quote_spanned!(span => unsafe impl #ion::class::DerivedFrom<#super_type> for #r#type {}))?;
let derived_from = quote_spanned!(span => unsafe impl #ion::class::DerivedFrom<#super_type> for #r#type {});
let derived_from = parse2(derived_from)?;
let castable = parse2(quote_spanned!(span => impl #ion::class::Castable for #r#type {}))?;

let native_object = parse2(quote_spanned!(span => impl #ion::class::NativeObject for #r#type {
Expand Down Expand Up @@ -150,12 +166,23 @@ fn class_impls(ion: &TokenStream, span: Span, name: &str, r#type: &Type, super_f
}))?;
operations_native_class.attrs.push(parse_quote!(#[doc(hidden)]));

Ok([from_value, from_value_mut, derived_from, castable, native_object, operations_native_class])
Ok([
from_value,
from_value_mut,
derived_from,
castable,
native_object,
operations_native_class,
])
}

fn impl_from_value(ion: &TokenStream, span: Span, name: &str, r#type: &Type, mutable: bool) -> Result<ItemImpl> {
let from_value_error = LitStr::new(&format!("Expected {}", name), span);
let function = if mutable { quote!(get_mut_private) } else { quote!(get_private) };
let function = if mutable {
quote!(get_mut_private)
} else {
quote!(get_private)
};
let mutable = mutable.then(<Token![mut]>::default);

parse2(
Expand Down
37 changes: 28 additions & 9 deletions ion-proc/src/function/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ impl Parameter {

for attr in &pat_ty.attrs {
if attr.path().is_ident("ion") {
let args: Punctuated<ParameterAttribute, Token![,]> = attr.parse_args_with(Punctuated::parse_terminated)?;
let args: Punctuated<ParameterAttribute, Token![,]> =
attr.parse_args_with(Punctuated::parse_terminated)?;

use ParameterAttribute as PA;
for arg in args {
Expand Down Expand Up @@ -134,7 +135,9 @@ impl Parameter {
use Parameter as P;
let ty = self.get_type_without_lifetimes();
match self {
P::Regular { pat, conversion, strict, option, .. } => regular_param_statement(ion, pat, &ty, option.as_deref(), conversion, *strict),
P::Regular { pat, conversion, strict, option, .. } => {
regular_param_statement(ion, pat, &ty, option.as_deref(), conversion, *strict)
}
P::VarArgs { pat, conversion, strict, .. } => varargs_param_statement(pat, &ty, conversion, *strict),
P::Context(pat, _) => parse2(quote!(let #pat: #ty = __cx;)),
P::Arguments(pat, _) => parse2(quote!(let #pat: #ty = __args;)),
Expand All @@ -156,7 +159,8 @@ impl ThisParameter {
_ => {
for attr in &pat_ty.attrs {
if attr.path().is_ident("ion") {
let args: Punctuated<ParameterAttribute, Token![,]> = attr.parse_args_with(Punctuated::parse_terminated)?;
let args: Punctuated<ParameterAttribute, Token![,]> =
attr.parse_args_with(Punctuated::parse_terminated)?;
for arg in args {
if let ParameterAttribute::This(_) = arg {
return parse_this(pat, ty, class_ty.is_some(), span).map(Some);
Expand Down Expand Up @@ -191,7 +195,11 @@ impl ThisParameter {
pub(crate) fn to_statement(&self, ion: &TokenStream, is_class: bool) -> Result<Stmt> {
let ThisParameter { pat, ty, kind } = self;
let self_ = parse_quote!(self_);
let pat = if is_class && **pat == parse_quote!(self) { &self_ } else { pat };
let pat = if is_class && **pat == parse_quote!(self) {
&self_
} else {
pat
};
let mut ty = ty.clone();
visit_type_mut(&mut LifetimeRemover, &mut ty);

Expand Down Expand Up @@ -228,7 +236,10 @@ impl Parameters {
Ok(Some(this_param)) => {
if let Pat::Ident(ident) = (*this_param.pat).clone() {
if let Some(this) = &this {
return Some(Err(Error::new(this.1.span(), "Unable to have multiple this/self parameters")));
return Some(Err(Error::new(
this.1.span(),
"Unable to have multiple this/self parameters",
)));
}
this = Some((this_param, ident.ident.clone(), i));
}
Expand Down Expand Up @@ -301,7 +312,9 @@ impl Parameters {
Parameter::Regular { pat, ty, .. } | Parameter::VarArgs { pat, ty, .. } => {
parse2(quote_spanned!(pat.span() => #pat: #ty)).unwrap()
}
Parameter::Context(pat, ty) | Parameter::Arguments(pat, ty) => parse2(quote_spanned!(pat.span() => #pat: #ty)).unwrap(),
Parameter::Context(pat, ty) | Parameter::Arguments(pat, ty) => {
parse2(quote_spanned!(pat.span() => #pat: #ty)).unwrap()
}
})
.collect::<Vec<_>>(),
);
Expand All @@ -310,8 +323,12 @@ impl Parameters {
args.insert(
*index,
match kind {
ThisKind::Ref(lt, mutability) => parse2(quote_spanned!(pat.span() => #pat: &#lt #mutability #ty)).unwrap(),
ThisKind::Object(lt, mutability) => parse2(quote_spanned!(pat.span() => #pat: &#lt #mutability #ty)).unwrap(),
ThisKind::Ref(lt, mutability) => {
parse2(quote_spanned!(pat.span() => #pat: &#lt #mutability #ty)).unwrap()
}
ThisKind::Object(lt, mutability) => {
parse2(quote_spanned!(pat.span() => #pat: &#lt #mutability #ty)).unwrap()
}
ThisKind::Owned => parse2(quote_spanned!(pat.span() => #pat: #ty)).unwrap(),
},
);
Expand All @@ -331,7 +348,9 @@ impl Parameters {
}
}

fn regular_param_statement(ion: &TokenStream, pat: &Pat, ty: &Type, option: Option<&Type>, conversion: &Expr, strict: bool) -> Result<Stmt> {
fn regular_param_statement(
ion: &TokenStream, pat: &Pat, ty: &Type, option: Option<&Type>, conversion: &Expr, strict: bool,
) -> Result<Stmt> {
let pat_str = format_pat(pat);
let not_found_error = if let Some(pat) = pat_str {
format!("Argument {} at index {{}} was not found.", pat)
Expand Down
5 changes: 4 additions & 1 deletion ion-proc/src/function/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pub(crate) fn impl_wrapper_fn(
let inner = impl_inner_fn(function.clone(), &parameters, class_ty.is_none());

let wrapper_generics: [GenericParam; 2] = [parse_quote!('cx), parse_quote!('a)];
let mut wrapper_args: Vec<FnArg> = vec![parse_quote!(__cx: &'cx #ion::Context), parse_quote!(__args: &'a mut #ion::Arguments<'cx>)];
let mut wrapper_args: Vec<FnArg> = vec![
parse_quote!(__cx: &'cx #ion::Context),
parse_quote!(__args: &'a mut #ion::Arguments<'cx>),
];

let argument_checker = argument_checker(ion, &function.sig.ident, parameters.nargs.0);

Expand Down
5 changes: 4 additions & 1 deletion ion-proc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ fn impl_body(span: Span, data: &Data) -> Result<Block> {
}
}))
}
Data::Union(_) => Err(Error::new(span, "#[derive(Traceable) is not implemented for union types.")),
Data::Union(_) => Err(Error::new(
span,
"#[derive(Traceable) is not implemented for union types.",
)),
}
}

Expand Down
Loading

0 comments on commit 3b59e0a

Please sign in to comment.