Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1790bbc9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Update for Rust v1.62.0"

parents 3d11751f f0b6afce
Loading
Loading
Loading
Loading
+194 −141
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ use crate::ast;
use anyhow::{anyhow, bail, Context, Result};
use quote::{format_ident, quote};
use std::collections::HashMap;
use std::fmt::Write;
use std::path::Path;
use syn::parse_quote;

@@ -22,9 +23,12 @@ fn generate_preamble(path: &Path) -> Result<String> {
        .file_name()
        .and_then(|path| path.to_str())
        .ok_or_else(|| anyhow!("could not find filename in {:?}", path))?;
    code.push_str(&format!("// @generated rust packets from {filename}\n\n"));
    let _ = write!(code, "// @generated rust packets from {filename}\n\n");

    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            use bytes::{BufMut, Bytes, BytesMut};
            use num_derive::{FromPrimitive, ToPrimitive};
            use num_traits::{FromPrimitive, ToPrimitive};
@@ -32,13 +36,21 @@ fn generate_preamble(path: &Path) -> Result<String> {
            use std::fmt;
            use std::sync::Arc;
            use thiserror::Error;
    });
        }
    );

    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            type Result<T> = std::result::Result<T, Error>;
    });
        }
    );

    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            #[derive(Debug, Error)]
            pub enum Error {
                #[error("Packet parsing failed")]
@@ -52,20 +64,29 @@ fn generate_preamble(path: &Path) -> Result<String> {
                #[error("when parsing field {obj}.{field}, {value} is not a valid {type_} value")]
                InvalidEnumValueError { obj: String, field: String, value: u64, type_: String },
            }
    });
        }
    );

    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            #[derive(Debug, Error)]
            #[error("{0}")]
            pub struct TryFromError(&'static str);
    });
        }
    );

    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            pub trait Packet {
                fn to_bytes(self) -> Bytes;
                fn to_vec(self) -> Vec<u8>;
            }
    });
        }
    );

    Ok(code)
}
@@ -226,7 +247,10 @@ fn generate_packet_decl(
    let child_name = format_ident!("{id}Child");
    if has_children {
        let child_data_idents = child_idents.iter().map(|ident| format_ident!("{ident}Data"));
        code.push_str(&quote_block! {
        let _ = write!(
            code,
            "{}",
            &quote_block! {
                #[derive(Debug)]
                enum #data_child_ident {
                    #(#child_idents(Arc<#child_data_idents>),)*
@@ -248,7 +272,8 @@ fn generate_packet_decl(
                    #(#child_idents(#child_decl_packet_name),)*
                    None,
                }
        });
            }
        );
    }

    let data_name = format_ident!("{id}Data");
@@ -261,13 +286,17 @@ fn generate_packet_decl(
        .iter()
        .map(|field| generate_field(field, parse_quote!()))
        .collect::<Result<Vec<_>>>()?;
    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            #[derive(Debug)]
            struct #data_name {
                #(#plain_fields,)*
                #child_field
            }
    });
        }
    );

    let parent = parent_id.as_ref().map(|parent_id| match packets.get(parent_id.as_str()) {
        Some(ast::Decl::Packet { id, .. }) => {
@@ -281,25 +310,33 @@ fn generate_packet_decl(
    });

    let packet_name = format_ident!("{id}Packet");
    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            #[derive(Debug, Clone)]
            pub struct #packet_name {
                #parent
                #ident: Arc<#data_name>,
            }
    });
        }
    );

    let builder_name = format_ident!("{id}Builder");
    let pub_fields = fields
        .iter()
        .map(|field| generate_field(field, parse_quote!(pub)))
        .collect::<Result<Vec<_>>>()?;
    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            #[derive(Debug)]
            pub struct #builder_name {
                #(#pub_fields,)*
            }
    });
        }
    );

    // TODO(mgeisler): use the `Buf` trait instead of tracking
    // the offset manually.
@@ -336,7 +373,10 @@ fn generate_packet_decl(
        })
    });

    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            impl #data_name {
                fn conforms(bytes: &[u8]) -> bool {
                    // TODO(mgeisler): return Boolean expression directly.
@@ -366,9 +406,13 @@ fn generate_packet_decl(
                    ret
                }
            }
    });
        }
    );

    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            impl Packet for #packet_name {
                fn to_bytes(self) -> Bytes {
                    let mut buffer = BytesMut::new();
@@ -390,7 +434,8 @@ fn generate_packet_decl(
                    packet.to_vec()
                }
            }
    });
        }
    );

    let specialize = has_children.then(|| {
        quote! {
@@ -408,7 +453,10 @@ fn generate_packet_decl(
        .iter()
        .map(|field| generate_field_getter(&ident, field))
        .collect::<Result<Vec<_>>>()?;
    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            impl #packet_name {
                pub fn parse(bytes: &[u8]) -> Result<Self> {
                    Ok(Self::new(Arc::new(#data_name::parse(bytes)?)).unwrap())
@@ -423,14 +471,18 @@ fn generate_packet_decl(

                #(#field_getters)*
            }
    });
        }
    );

    let child = has_children.then(|| {
        quote! {
            child: #data_child_ident::None,
        }
    });
    code.push_str(&quote_block! {
    let _ = write!(
        code,
        "{}",
        &quote_block! {
            impl #builder_name {
                pub fn build(self) -> #packet_name {
                    let #ident = Arc::new(#data_name {
@@ -440,7 +492,8 @@ fn generate_packet_decl(
                    #packet_name::new(#ident).unwrap()
                }
            }
    });
        }
    );

    Ok(code)
}
+3 −3
Original line number Diff line number Diff line
@@ -375,12 +375,12 @@ pub async fn initiate(ctx: &impl Context) -> Result<(), ()> {
            AuthenticationMethod::NumericComparaison => {
                send_commitment(ctx, true).await;

                let _user_confirmation = user_confirmation_request(ctx).await?;
                user_confirmation_request(ctx).await?;
                Ok(())
            }
            AuthenticationMethod::PasskeyEntry => {
                if initiator.io_capability == hci::IoCapability::KeyboardOnly {
                    let _user_passkey = user_passkey_request(ctx).await?;
                    user_passkey_request(ctx).await?;
                } else {
                    ctx.send_hci_event(
                        hci::UserPasskeyNotificationBuilder {
@@ -397,7 +397,7 @@ pub async fn initiate(ctx: &impl Context) -> Result<(), ()> {
            }
            AuthenticationMethod::OutOfBand => {
                if initiator.oob_data_present != hci::OobDataPresent::NotPresent {
                    let _remote_oob_data = remote_oob_data_request(ctx).await?;
                    remote_oob_data_request(ctx).await?;
                }

                send_commitment(ctx, false).await;