blob: 4e78bf9c94ca99eb31c68c3755afbf40326fcc25 [file] [log] [blame] [edit]
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Helper definitions used used by the generated Rust backend.
use bytes::Bytes;
use thiserror::Error;
/// Type of parsing errors.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum Error {
#[error("Packet parsing failed")]
InvalidPacketError,
#[error("{field} was {value:x}, which is not known")]
ConstraintOutOfBounds { field: String, value: u64 },
#[error("Got {actual:x}, expected {expected:x}")]
InvalidFixedValue { expected: u64, actual: u64 },
#[error("when parsing {obj} needed length of {wanted} but got {got}")]
InvalidLengthError { obj: String, wanted: usize, got: usize },
#[error("array size ({array} bytes) is not a multiple of the element size ({element} bytes)")]
InvalidArraySize { array: usize, element: usize },
#[error("Due to size restrictions a struct could not be parsed.")]
ImpossibleStructError,
#[error("when parsing field {obj}.{field}, {value} is not a valid {type_} value")]
InvalidEnumValueError { obj: String, field: String, value: u64, type_: String },
#[error("expected child {expected}, got {actual}")]
InvalidChildError { expected: &'static str, actual: String },
}
/// Trait implemented for all toplevel packet declarations.
pub trait Packet {
fn to_bytes(self) -> Bytes;
fn to_vec(self) -> Vec<u8>;
}