Skip to content

Commit 3f51000

Browse files
committed
rust: pin-init: internal: add utility API for syn error handling
Add a function to turn a `syn::Result<TokenStream>` into a `TokenStream`, either containing the error or the normal stream. Add a nullable custom error type wrapping `syn::Error`. Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent b062668 commit 3f51000

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

  • rust/pin-init/internal/src

rust/pin-init/internal/src/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,47 @@ pub fn derive_zeroable(input: TokenStream) -> TokenStream {
3636
pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
3737
zeroable::maybe_derive(input.into()).into()
3838
}
39+
40+
#[expect(dead_code)]
41+
fn ok_or_compile_error(res: syn::Result<proc_macro2::TokenStream>) -> TokenStream {
42+
match res {
43+
Ok(stream) => stream,
44+
Err(error) => error.into_compile_error(),
45+
}
46+
.into()
47+
}
48+
49+
pub(crate) struct Error(Option<syn::Error>);
50+
51+
impl From<syn::Error> for Error {
52+
fn from(value: syn::Error) -> Self {
53+
Self(Some(value))
54+
}
55+
}
56+
57+
impl Error {
58+
#[expect(dead_code)]
59+
pub(crate) fn none() -> Self {
60+
Self(None)
61+
}
62+
63+
#[expect(dead_code)]
64+
pub(crate) fn combine(&mut self, error: impl Into<Self>) {
65+
let error = error.into();
66+
if let Some(this) = self.0.as_mut() {
67+
if let Some(error) = error.0 {
68+
this.combine(error);
69+
}
70+
} else {
71+
self.0 = error.0;
72+
}
73+
}
74+
}
75+
76+
impl quote::ToTokens for Error {
77+
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
78+
if let Some(error) = self.0.as_ref() {
79+
quote::ToTokens::to_tokens(&error.to_compile_error(), tokens);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)