ridl-backend-rust 0.2.0

Compiles a RIDL IR package to Rust source, plus the generated interaction face.
Documentation
---
source: crates/ridl-backend-rust/src/tests.rs
expression: rust_for(decls)
---
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i64)]
pub enum GearPosition {
    PARK = 0,
    DRIVE = 1,
    REVERSE = 2,
    NEUTRAL = 3,
}
impl ::core::convert::TryFrom<i64> for GearPosition {
    type Error = ::ridl_rt::payload::Violation;
    fn try_from(value: i64) -> ::core::result::Result<Self, Self::Error> {
        match value {
            0 => ::core::result::Result::Ok(Self::PARK),
            1 => ::core::result::Result::Ok(Self::DRIVE),
            2 => ::core::result::Result::Ok(Self::REVERSE),
            3 => ::core::result::Result::Ok(Self::NEUTRAL),
            _ => {
                ::core::result::Result::Err(::ridl_rt::payload::Violation {
                    type_name: "GearPosition",
                    rule: ::ridl_rt::payload::Rule::Variant,
                })
            }
        }
    }
}
impl ::core::convert::From<GearPosition> for i64 {
    fn from(value: GearPosition) -> Self {
        value as i64
    }
}
impl Default for GearPosition {
    fn default() -> Self {
        GearPosition::PARK
    }
}
/// An accessor over FlatBuffers bytes `GearPosition`'s `verify` accepted.
///
/// The buffer's root is the box table ADR-0019 decision 8
/// gives this declaration: one required `value` field. A
/// buffer carrying no slot for it is `MissingRequired`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(deprecated)]
pub struct GearPositionFbView<'a> {
    pub(crate) buf: &'a [u8],
    pub(crate) table: usize,
}
#[allow(deprecated)]
impl<'a> GearPositionFbView<'a> {
    /// The verified bytes this view reads.
    pub fn bytes(&self) -> &'a [u8] {
        self.buf
    }
    /// The value the box carries. `GearPosition` is one value, so this decodes it rather than borrowing it, which costs one read.
    pub fn value(&self) -> GearPosition {
        __ridl_fb_decode_gear_position(self.buf, self.table)
    }
}
/// Writes `GearPosition` as its box table and returns its position (ADR-0019 decision 8).
#[allow(deprecated)]
pub(crate) fn __ridl_fb_encode_gear_position(
    value: &GearPosition,
    builder: &mut ::ridl_rt::flatbuffers::Builder<'_>,
) -> ::core::result::Result<
    ::ridl_rt::flatbuffers::Pos,
    ::ridl_rt::payload::EncodeError,
> {
    ::core::result::Result::Ok({
        let __box = [
            ::ridl_rt::flatbuffers::TableField {
                slot: 0u16,
                offset: 8u16,
                value: {
                    let __s = *value;
                    ::ridl_rt::flatbuffers::Field::I64(i64::from(__s))
                },
            },
        ];
        builder.push_table(16usize, 8usize, 1u16, &__box)?
    })
}
#[allow(deprecated)]
pub(crate) fn __ridl_fb_verify_gear_position(
    buf: &[u8],
    table: usize,
) -> ::core::result::Result<(), ::ridl_rt::payload::VerifyError> {
    match ::ridl_rt::flatbuffers::field(buf, table, 0u16, 8usize)
        .map_err(::ridl_rt::payload::VerifyError::Structure)?
    {
        ::core::option::Option::Some(__p) => {
            let __raw = ::ridl_rt::flatbuffers::read_i64(buf, __p)
                .map_err(::ridl_rt::payload::VerifyError::Structure)?;
            <GearPosition as ::core::convert::TryFrom<i64>>::try_from(__raw)
                .map_err(::ridl_rt::payload::VerifyError::Contract)?;
        }
        ::core::option::Option::None => {
            return ::core::result::Result::Err(
                ::ridl_rt::payload::VerifyError::Structure(
                    ::ridl_rt::payload::Malformed::MissingRequired,
                ),
            );
        }
    }
    ::core::result::Result::Ok(())
}
#[allow(deprecated)]
pub(crate) fn __ridl_fb_decode_gear_position(buf: &[u8], table: usize) -> GearPosition {
    {
        let __p = ::ridl_rt::flatbuffers::field(buf, table, 0u16, 8usize)
            .unwrap_or(::core::option::Option::None)
            .unwrap_or(0usize);
        <GearPosition as ::core::convert::TryFrom<
            i64,
        >>::try_from(::ridl_rt::flatbuffers::read_i64(buf, __p).unwrap_or(0i64))
            .unwrap_or(GearPosition::PARK)
    }
}
#[allow(deprecated)]
impl ::ridl_rt::payload::Payload<::ridl_rt::encoding::FlatBuffers> for GearPosition {
    /// The largest FlatBuffers buffer any legal `GearPosition` encodes to: 50 bytes.
    ///
    /// `ridl_ir::projection::flatbuffers::max_size` computed it,
    /// which is the one implementation of the bound (design note
    /// D-6): each table is charged its `soffset`, its inline
    /// fields, its vtable and one alignment event per slot; a
    /// string four bytes per declared character plus a
    /// terminator; a collection its declared maximum. It is a
    /// literal rather than an expression over the field types
    /// because that slack is not expressible in Rust's type
    /// system.
    const MAX_SIZE: usize = 50usize;
    type View<'a> = GearPositionFbView<'a>;
    fn encode<'o>(
        &self,
        out: &'o mut [u8],
    ) -> ::core::result::Result<
        ::ridl_rt::payload::Encoded<'o, Self::View<'o>>,
        ::ridl_rt::payload::EncodeError,
    > {
        let mut builder = ::ridl_rt::flatbuffers::Builder::new(out);
        let __root = __ridl_fb_encode_gear_position(self, &mut builder)?;
        let bytes = builder.finish(__root, 8usize)?;
        let table = ::ridl_rt::flatbuffers::root(bytes).unwrap_or(0usize);
        ::core::result::Result::Ok(::ridl_rt::payload::Encoded {
            bytes,
            view: GearPositionFbView {
                buf: bytes,
                table,
            },
        })
    }
    fn verify(
        buf: &[u8],
    ) -> ::core::result::Result<Self::View<'_>, ::ridl_rt::payload::VerifyError> {
        if buf.len()
            > <Self as ::ridl_rt::payload::Payload<
                ::ridl_rt::encoding::FlatBuffers,
            >>::MAX_SIZE
        {
            return ::core::result::Result::Err(
                ::ridl_rt::payload::VerifyError::Structure(
                    ::ridl_rt::payload::Malformed::TooLarge,
                ),
            );
        }
        let table = ::ridl_rt::flatbuffers::root(buf)
            .map_err(::ridl_rt::payload::VerifyError::Structure)?;
        __ridl_fb_verify_gear_position(buf, table)?;
        ::core::result::Result::Ok(GearPositionFbView { buf, table })
    }
    fn decode(
        r: ::ridl_rt::payload::Ref<'_, Self, ::ridl_rt::encoding::FlatBuffers>,
    ) -> Self {
        let __view = r.view();
        __ridl_fb_decode_gear_position(__view.buf, __view.table)
    }
}