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

Commit 12a4317e authored by Hsin-chen Chuang's avatar Hsin-chen Chuang
Browse files

floss: dbus_propmap: Recursively call DBusArg::log to the fields

This defers to the fields' DBusArg::log if they implemented it. Before
this, even though RawAddress has a custom DBusArg::log, it won't be
called if the RawAddress is inside other structures and the default
Debug format is used.

Bug: 342337056
Tag: #floss
Test: mmm packages/modules/Bluetooth
Flag: EXEMPT, Floss-only changes
Change-Id: I97edd0236cc6bad4e162c4f02406639ca64b9a79
parent 4c54cf6f
Loading
Loading
Loading
Loading
+27 −8
Original line number Diff line number Diff line
@@ -610,14 +610,14 @@ pub fn dbus_propmap(attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut field_idents = quote! {};

    let mut insert_map_fields = quote! {};
    for field in ast.fields {
        let field_ident = field.ident;

        if field_ident.is_none() {
            continue;
        }
    let mut log_format = String::new();
    let mut log_args = quote! {};

        let field_str = field_ident.as_ref().unwrap().clone().to_string();
    for field in ast.fields {
        let Some(field_ident) = field.ident else { continue };

        let field_str = field_ident.to_string();

        let field_type = if let Type::Path(t) = field.ty {
            t
@@ -673,7 +673,26 @@ pub fn dbus_propmap(attr: TokenStream, item: TokenStream) -> TokenStream {
            let field_data__ = DBusArg::to_dbus(data__.#field_ident)?;
            map__.insert(String::from(#field_str), dbus::arg::Variant(Box::new(field_data__)));
        };

        if !log_format.is_empty() {
            log_format.push_str(", ");
        }
        log_format.push_str(field_str.as_str());
        log_format.push_str(": {:?}");

        log_args = quote! {
            #log_args
            <#field_type as DBusArg>::log(&data__.#field_ident),
        };
    }

    // Give an example type: struct BluetoothDevice { address: RawAddress, name: String }
    // At this point the |log_format| would be: "address: {:?}, name: {:?}"
    // Now, wrap it with curly braces and prepend the structure name so it becomes:
    //     "BluetoothDevice { address: {:?}, name: {:?} }"
    log_format.insert_str(0, " {{ ");
    log_format.push_str(" }}");
    log_format.insert_str(0, struct_ident.to_string().as_str());

    let gen = quote! {
        #[allow(dead_code)]
@@ -701,8 +720,8 @@ pub fn dbus_propmap(attr: TokenStream, item: TokenStream) -> TokenStream {
                return Ok(map__);
            }

            fn log(data: &#struct_ident) -> String {
                String::from(format!("{:?}", data))
            fn log(data__: &#struct_ident) -> String {
                format!(#log_format, #log_args)
            }
        }
    };