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

Commit 8dc122e0 authored by Martin Geisler's avatar Martin Geisler
Browse files

Revert "pdl: Format generated code using prettyplease"

Revert submission 2586907

Reason for revert: http://b/283107621

Reverted changes: /q/submissionid:2586907

Change-Id: I96f1d7a70308baee85f31d60630e2ff869c91bfa
parent d004a70d
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -146,8 +146,13 @@ rust_test_host {

genrule_defaults {
    name: "pdl_rust_generator_defaults",
    cmd: "$(location :pdl) --output-format rust $(in) > $(out)",
    tools: [":pdl"],
    cmd: "set -o pipefail;" +
        " $(location :pdl) --output-format rust $(in) |" +
        " $(location :rustfmt) > $(out)",
    tools: [
        ":pdl",
        ":rustfmt",
    ],
    defaults_visibility: [
        "//external/uwb/src",
        "//packages/modules/Bluetooth:__subpackages__",
+17 −13
Original line number Diff line number Diff line
@@ -964,20 +964,24 @@ fn generate_decl(
    scope: &lint::Scope<'_>,
    file: &analyzer_ast::File,
    decl: &analyzer_ast::Decl,
) -> proc_macro2::TokenStream {
) -> String {
    match &decl.desc {
        ast::DeclDesc::Packet { id, .. } => generate_packet_decl(scope, file.endianness.value, id),
        ast::DeclDesc::Packet { id, .. } => {
            generate_packet_decl(scope, file.endianness.value, id).to_string()
        }
        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)
            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()
        }
        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)
            generate_custom_field_decl(id, *width).to_string()
        }
        _ => todo!("unsupported Decl::{:?}", decl),
    }
@@ -988,18 +992,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");
    let preamble = preamble::generate(Path::new(source.name()));
    code.push_str(&preamble::generate(Path::new(source.name())));

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

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

#[cfg(test)]
+13 −5
Original line number Diff line number Diff line
@@ -12,11 +12,13 @@
// 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) -> proc_macro2::TokenStream {
pub fn generate(path: &Path) -> String {
    let mut code = String::new();
    // TODO(mgeisler): Make the  generated code free from warnings.
    //
    // The code either needs
@@ -48,7 +50,7 @@ pub fn generate(path: &Path) -> proc_macro2::TokenStream {
    // mod foo { include_str!("generated.rs") }
    // use foo::*;
    // fn after() {}
    quote! {
    code.push_str(&quote_block! {
        #[doc = #module_doc_string]

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

    code.push_str(&quote_block! {
        #[derive(Debug, Error)]
        pub enum Error {
            #[error("Packet parsing failed")]
@@ -93,12 +97,16 @@ pub fn generate(path: &Path) -> proc_macro2::TokenStream {
            #[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)]
@@ -108,7 +116,7 @@ mod tests {

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