reallyme-buffa-codegen 0.1.2

ReallyMe fork: Shared code generation logic for buffa (descriptor → Rust source)
Documentation
// Modified by ReallyMe Technologies Limited.
//! Vtable-mode reflection codegen: config validation and emitted impls.

use super::*;
use crate::ReflectMode;

#[test]
fn reflect_mode_maps_to_config_flags() {
    let mut c = CodeGenConfig::default();

    ReflectMode::Off.apply(&mut c);
    assert!(!c.generate_reflection && !c.generate_reflection_vtable);

    ReflectMode::Bridge.apply(&mut c);
    assert!(c.generate_reflection && !c.generate_reflection_vtable);

    ReflectMode::VTable.apply(&mut c);
    assert!(c.generate_reflection && c.generate_reflection_vtable);
}

/// A config with both bridge reflection and vtable mode enabled.
fn vtable_config() -> CodeGenConfig {
    CodeGenConfig {
        generate_reflection: true,
        generate_reflection_vtable: true,
        ..Default::default()
    }
}

/// A small proto3 message with a scalar and a string field.
fn msg_file() -> FileDescriptorProto {
    let mut file = proto3_file("vt.proto");
    file.message_type.push(DescriptorProto {
        name: Some("Msg".to_string()),
        field: vec![
            make_field("id", 1, Label::LABEL_OPTIONAL, Type::TYPE_INT32),
            make_field("name", 2, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
        ],
        ..Default::default()
    });
    file
}

#[test]
fn vtable_emits_reflect_message_and_element_impls() {
    let files = generate(&[msg_file()], &["vt.proto".to_string()], &vtable_config())
        .expect("should generate");
    let content = joined(&files);

    assert!(
        content.contains("impl<'a> ::buffa_descriptor::reflect::ReflectMessage for MsgView<'a>"),
        "vtable mode must emit ReflectMessage for the view: {content}"
    );
    assert!(
        content.contains("impl<'a> ::buffa_descriptor::reflect::ReflectElement for MsgView<'a>"),
        "vtable mode must emit ReflectElement for the view: {content}"
    );
    // The memoized index accessor is an inherent associated fn (collision-free
    // across sibling views).
    assert!(
        content.contains("fn __buffa_reflect_message_index()"),
        "vtable mode must emit the memoized MessageIndex accessor: {content}"
    );
}

#[test]
fn vtable_without_reflection_is_rejected() {
    let config = CodeGenConfig {
        generate_reflection: false,
        generate_reflection_vtable: true,
        ..Default::default()
    };
    let err = generate(&[msg_file()], &["vt.proto".to_string()], &config)
        .expect_err("vtable without reflection must error");
    assert!(
        err.to_string().contains("generate_reflection"),
        "error should name the missing prerequisite: {err}"
    );
}

#[test]
fn vtable_without_views_emits_owned_only() {
    // Owned vtable is self-contained, so views-off + vtable is allowed: it
    // emits the owned `impl ReflectMessage` but no view impls (there are no
    // views).
    let config = CodeGenConfig {
        generate_reflection: true,
        generate_reflection_vtable: true,
        generate_views: false,
        ..Default::default()
    };
    let files =
        generate(&[msg_file()], &["vt.proto".to_string()], &config).expect("should generate");
    let content = joined(&files);
    assert!(
        content.contains("impl ::buffa_descriptor::reflect::ReflectMessage for Msg"),
        "owned ReflectMessage must be emitted: {content}"
    );
    assert!(
        !content.contains("ReflectMessage for MsgView"),
        "no view impls when views are off: {content}"
    );
}

#[test]
fn bridge_only_does_not_emit_vtable_impls() {
    let config = CodeGenConfig {
        generate_reflection: true,
        generate_reflection_vtable: false,
        ..Default::default()
    };
    let files =
        generate(&[msg_file()], &["vt.proto".to_string()], &config).expect("should generate");
    let content = joined(&files);
    // Bridge mode emits Reflectable on the owned type but no ReflectMessage on
    // the view.
    assert!(
        content.contains("impl ::buffa_descriptor::reflect::Reflectable for Msg"),
        "bridge mode must still emit Reflectable: {content}"
    );
    assert!(
        !content.contains("ReflectMessage for MsgView"),
        "bridge-only must not emit the vtable ReflectMessage impl: {content}"
    );
}

#[test]
fn to_dynamic_scales_its_memory_bounds_and_floors_them_at_the_defaults() {
    // `to_dynamic` cannot report a failure, so its re-decode must not be able
    // to reject bytes the encoder on the line above just produced. The two
    // decoders measure the same message in different units, so the defaults
    // can reject; `usize::MAX` would instead leave the conversion with no
    // ceiling at all. Scaling to the input and flooring at the default is the
    // narrow path between those, and both halves are load-bearing — without
    // the floor a small message gets a *tighter* bound than a plain decode.
    let files = generate(&[msg_file()], &["vt.proto".to_string()], &vtable_config())
        .expect("should generate");
    let flat: String = joined(&files)
        .chars()
        .filter(|c| !c.is_whitespace())
        .collect();

    assert!(
        flat.contains(".saturating_mul(128)"),
        "the element bound must scale with the encoded length"
    );
    assert!(
        flat.contains(".max(::buffa::DEFAULT_ELEMENT_MEMORY_LIMIT)"),
        "the scaled element bound must be floored at the default, not capped by it"
    );
    assert!(
        flat.contains(".max(::buffa::DEFAULT_UNKNOWN_FIELD_LIMIT)"),
        "the unknown-field bound must be floored at the default too"
    );
    assert!(
        !flat.contains("with_element_memory_limit(usize::MAX)"),
        "an unbounded re-decode would leave decode_view -> to_dynamic with no ceiling"
    );
}

#[test]
fn implicit_float_has_arms_compare_bit_patterns() {
    // `-0.0 != 0.0` is false, so a value comparison would report a field
    // holding negative zero as unset although the encoder writes it.
    let mut file = proto3_file("vt_float.proto");
    file.message_type.push(DescriptorProto {
        name: Some("Floats".to_string()),
        field: vec![
            make_field("ratio", 1, Label::LABEL_OPTIONAL, Type::TYPE_FLOAT),
            make_field("weight", 2, Label::LABEL_OPTIONAL, Type::TYPE_DOUBLE),
        ],
        ..Default::default()
    });
    let files = generate(&[file], &["vt_float.proto".to_string()], &vtable_config())
        .expect("should generate");
    let flat: String = joined(&files)
        .chars()
        .filter(|c| !c.is_whitespace())
        .collect();

    for arm in [
        "1u32=>self.ratio.to_bits()!=0,",
        "2u32=>self.weight.to_bits()!=0,",
    ] {
        assert_eq!(
            flat.matches(arm).count(),
            2,
            "the owned and the view `has` must each emit `{arm}`"
        );
    }
}

/// An editions file whose enums are open by default, with one enum that
/// declares itself closed and one that keeps the default.
fn editions_enum_override_file() -> FileDescriptorProto {
    use crate::generated::descriptor::{feature_set::EnumType, Edition, EnumOptions, FeatureSet};

    let closed_options = EnumOptions {
        features: FeatureSet {
            enum_type: Some(EnumType::CLOSED),
            ..Default::default()
        }
        .into(),
        ..Default::default()
    };
    let closed = EnumDescriptorProto {
        name: Some("Closed".to_string()),
        value: vec![enum_value("CLOSED_ZERO", 0), enum_value("CLOSED_ONE", 1)],
        options: closed_options.into(),
        ..Default::default()
    };
    let open = EnumDescriptorProto {
        name: Some("Open".to_string()),
        value: vec![enum_value("OPEN_ZERO", 0), enum_value("OPEN_ONE", 1)],
        ..Default::default()
    };

    let mut closed_values = make_field("closed_values", 1, Label::LABEL_REPEATED, Type::TYPE_ENUM);
    closed_values.type_name = Some(".ed.Closed".to_string());
    let mut open_values = make_field("open_values", 2, Label::LABEL_REPEATED, Type::TYPE_ENUM);
    open_values.type_name = Some(".ed.Open".to_string());

    FileDescriptorProto {
        name: Some("ed_enum.proto".to_string()),
        package: Some("ed".to_string()),
        syntax: Some("editions".to_string()),
        edition: Some(Edition::EDITION_2023),
        enum_type: vec![closed, open],
        message_type: vec![DescriptorProto {
            name: Some("Holder".to_string()),
            field: vec![closed_values, open_values],
            ..Default::default()
        }],
        ..Default::default()
    }
}

#[test]
fn vtable_closed_enum_override_in_open_edition_gets_element_impl() {
    let files = generate(
        &[editions_enum_override_file()],
        &["ed_enum.proto".to_string()],
        &vtable_config(),
    )
    .expect("should generate");
    let content = joined(&files);

    // The enum's own `enum_type = CLOSED` overrides the file default, so its
    // repeated field is typed as a bare `Closed` and needs the element impl.
    assert!(
        content.contains("impl ::buffa_descriptor::reflect::ReflectElement for Closed"),
        "an enum that declares itself closed must get ReflectElement: {content}"
    );
    // The open enum is wrapped in `EnumValue<Open>`, covered by buffa-descriptor.
    assert!(
        !content.contains("impl ::buffa_descriptor::reflect::ReflectElement for Open"),
        "an open enum must not get its own ReflectElement impl: {content}"
    );
}