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

Commit 5191c3b3 authored by Martin Geisler's avatar Martin Geisler Committed by Automerger Merge Worker
Browse files

Revert^2 "pdl: Format generated code using prettyplease" am: f7049e15

parents d53b4f38 f7049e15
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -150,13 +150,8 @@ rust_test_host {

genrule_defaults {
    name: "pdl_rust_generator_defaults",
    cmd: "set -o pipefail;" +
        " $(location :pdl) --output-format rust $(in) |" +
        " $(location :rustfmt) > $(out)",
    tools: [
        ":pdl",
        ":rustfmt",
    ],
    cmd: "$(location :pdl) --output-format rust $(in) > $(out)",
    tools: [":pdl"],
    defaults_visibility: [
        "//external/uwb/src",
        "//packages/modules/Bluetooth:__subpackages__",
+13 −17
Original line number Diff line number Diff line
@@ -967,24 +967,20 @@ fn generate_decl(
    scope: &lint::Scope<'_>,
    file: &analyzer_ast::File,
    decl: &analyzer_ast::Decl,
) -> String {
) -> proc_macro2::TokenStream {
    match &decl.desc {
        ast::DeclDesc::Packet { id, .. } => {
            generate_packet_decl(scope, file.endianness.value, id).to_string()
        }
        ast::DeclDesc::Packet { id, .. } => generate_packet_decl(scope, file.endianness.value, id),
        ast::DeclDesc::Struct { id, parent_id: None, .. } => {
            // TODO(mgeisler): handle structs with parents. We could
            // generate code for them, but the code is not useful
            // since it would require the caller to unpack everything
            // manually. We either need to change the API, or
            // implement the recursive (de)serialization.
            generate_struct_decl(scope, file.endianness.value, id).to_string()
        }
        ast::DeclDesc::Enum { id, tags, width } => {
            generate_enum_decl(id, tags, *width, false).to_string()
            generate_struct_decl(scope, file.endianness.value, id)
        }
        ast::DeclDesc::Enum { id, tags, width } => generate_enum_decl(id, tags, *width, false),
        ast::DeclDesc::CustomField { id, width: Some(width), .. } => {
            generate_custom_field_decl(id, *width).to_string()
            generate_custom_field_decl(id, *width)
        }
        _ => todo!("unsupported Decl::{:?}", decl),
    }
@@ -995,18 +991,18 @@ fn generate_decl(
/// The code is not formatted, pipe it through `rustfmt` to get
/// readable source code.
pub fn generate(sources: &ast::SourceDatabase, file: &analyzer_ast::File) -> String {
    let mut code = String::new();

    let source = sources.get(file.file).expect("could not read source");
    code.push_str(&preamble::generate(Path::new(source.name())));
    let preamble = preamble::generate(Path::new(source.name()));

    let scope = lint::Scope::new(file);
    for decl in &file.declarations {
        code.push_str(&generate_decl(&scope, file, decl));
        code.push_str("\n\n");
    }
    let decls = file.declarations.iter().map(|decl| generate_decl(&scope, file, decl));
    let code = quote! {
        #preamble

    code
        #(#decls)*
    };
    let syntax_tree = syn::parse2(code).expect("Could not parse code");
    prettyplease::unparse(&syntax_tree)
}

#[cfg(test)]
+5 −13
Original line number Diff line number Diff line
@@ -12,13 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use quote::quote;
use std::path::Path;

use crate::quote_block;

/// Generate the file preamble.
pub fn generate(path: &Path) -> String {
    let mut code = String::new();
pub fn generate(path: &Path) -> proc_macro2::TokenStream {
    // TODO(mgeisler): Make the  generated code free from warnings.
    //
    // The code either needs
@@ -50,7 +48,7 @@ pub fn generate(path: &Path) -> String {
    // mod foo { include_str!("generated.rs") }
    // use foo::*;
    // fn after() {}
    code.push_str(&quote_block! {
    quote! {
        #[doc = #module_doc_string]

        use bytes::{Buf, BufMut, Bytes, BytesMut};
@@ -75,9 +73,7 @@ pub fn generate(path: &Path) -> String {
                &self.0
            }
        }
    });

    code.push_str(&quote_block! {
        #[derive(Debug, Error)]
        pub enum Error {
            #[error("Packet parsing failed")]
@@ -97,16 +93,12 @@ pub fn generate(path: &Path) -> String {
            #[error("expected child {expected}, got {actual}")]
            InvalidChildError { expected: &'static str, actual: String },
        }
    });

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

    code
    }
}

#[cfg(test)]
@@ -116,7 +108,7 @@ mod tests {

    #[test]
    fn test_generate_preamble() {
        let actual_code = generate(Path::new("some/path/foo.pdl"));
        let actual_code = generate(Path::new("some/path/foo.pdl")).to_string();
        assert_snapshot_eq("tests/generated/preamble.rs", &format_rust(&actual_code));
    }
}