Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make macro compile error messages more informative #42

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions bleps-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ pub fn gatt(input: TokenStream) -> TokenStream {

for elem in ast.elems {
match elem {
syn::Expr::Struct(s) => {
Expr::Struct(s) => {
if path_to_string(s.path) != "service" {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Service definition must be given as 'service { ... }'"); }.into();
}

let mut service = Service::default();
Expand All @@ -54,7 +54,7 @@ pub fn gatt(input: TokenStream) -> TokenStream {
let name = if let Member::Named(name) = field.member {
name.to_string()
} else {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Service has an unnamed field"); }.into();
};

match name.as_str() {
Expand All @@ -63,18 +63,18 @@ pub fn gatt(input: TokenStream) -> TokenStream {
if let Lit::Str(s) = value.lit {
service.uuid = s.value();
} else {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Service field 'uuid' must be a string literal"); }.into();
}
} else {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Service field 'uuid' must be a string literal"); }.into();
}
}
"characteristics" => {
if let Expr::Array(characteristics) = field.expr {
for characteristic in characteristics.elems {
if let Expr::Struct(s) = characteristic {
if path_to_string(s.path) != "characteristic" {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Characteristic definition must be given as 'characteristic { ... }'"); }.into();
}

let mut charact = Characteristic::default();
Expand All @@ -83,7 +83,7 @@ pub fn gatt(input: TokenStream) -> TokenStream {
let name = if let Member::Named(name) = field.member {
name.to_string()
} else {
return quote! { compile_error!("Unexpected"); }
return quote! { compile_error!("Characteristic has an unnamed field"); }
.into();
};

Expand All @@ -93,83 +93,83 @@ pub fn gatt(input: TokenStream) -> TokenStream {
if let Lit::Str(s) = value.lit {
charact.uuid = s.value();
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'uuid' must be a string literal"); }.into();
}
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'uuid' must be a string literal"); }.into();
}
}
"data" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
charact.data = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'data' must be a path"); }.into();
}
}
"value" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
charact.value = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'value' must be a path"); }.into();
}
}
"read" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
charact.read = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'read' must be a path"); }.into();
}
}
"write" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
charact.write = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'write' must be a path"); }.into();
}
}
"description" => {
if let Expr::Lit(value) = field.expr {
if let Lit::Str(s) = value.lit {
charact.description = Some(s.value());
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'description' must be a string literal"); }.into();
}
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'description' must be a string literal"); }.into();
}
}
"notify" => {
if let Expr::Lit(value) = field.expr {
if let Lit::Bool(s) = value.lit {
charact.notify = s.value();
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'notify' must be a boolean"); }.into();
}
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'notify' must be a boolean"); }.into();
}
}
"notify_cb" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
charact.notify_cb = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'notify_cb' must be a path"); }.into();
}
}
"name" => {
if let Expr::Lit(value) = field.expr {
if let Lit::Str(s) = value.lit {
charact.name = Some(s.value());
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'name' must be a string literal"); }.into();
}
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'name' must be a string literal"); }.into();
}
}
"descriptors" => {
Expand All @@ -179,7 +179,7 @@ pub fn gatt(input: TokenStream) -> TokenStream {
if path_to_string(s.path)
!= "descriptor"
{
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Descriptor definition must be given as 'descriptor { ... }'"); }.into();
}

let mut desc =
Expand All @@ -192,7 +192,7 @@ pub fn gatt(input: TokenStream) -> TokenStream {
{
name.to_string()
} else {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Descriptor has an unnamed field"); }.into();
};

match name.as_str() {
Expand All @@ -201,38 +201,38 @@ pub fn gatt(input: TokenStream) -> TokenStream {
if let Lit::Str(s) = value.lit {
desc.uuid = s.value();
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Descriptor field 'uuid' must be a string literal"); }.into();
}
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Descriptor field 'uuid' must be a string literal"); }.into();
}
}
"read" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
desc.read = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Descriptor field 'read' must be a path"); }.into();
}
}
"write" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
desc.write = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Descriptor field 'write' must be a path"); }.into();
}
}
"value" => {
if let Expr::Path(p) = field.expr {
let name = path_to_string(p.path);
desc.value = Some(name);
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Descriptor field 'value' must be a path"); }.into();
}
}
_ => {
return quote! { compile_error!("Unexpected"); }
return quote! { compile_error!(concat!("Unexpected descriptor field '", #name, "'")); }
.into()
}
}
Expand All @@ -241,32 +241,32 @@ pub fn gatt(input: TokenStream) -> TokenStream {
}
}
} else {
return quote!{ compile_error!("Unexpected"); }.into();
return quote!{ compile_error!("Characteristic field 'descriptors' must be an array"); }.into();
}
}
_ => {
return quote! { compile_error!("Unexpected"); }
return quote! { compile_error!(concat!("Unexpected characteristic field '", #name, "'")); }
.into()
}
}
}

service.characteristics.push(charact);
} else {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Characteristic definition must be given as 'characteristic { ... }'"); }.into();
}
}
} else {
return quote! { compile_error!("Unexpected"); }.into();
return quote! { compile_error!("Service field 'characteristics' must be an array"); }.into();
}
}
_ => return quote! { compile_error!("Unexpected"); }.into(),
_ => return quote! { compile_error!(concat!("Unexpected service field '", #name, "'")); }.into(),
}
}

services.push(service);
}
_ => return quote! { compile_error!("Unexpected"); }.into(),
_ => return quote! { compile_error!("Service definition must be given as 'service { ... }'"); }.into(),
};
}

Expand Down
Loading