You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: tlv.rs
    path of file: ./repos/bee/bee-protocol/bee-protocol/src/packets
    
    the code of the file until where you have to start completion: // Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Type-length-value encoding on top of the packets.

use crate::packets::{HeaderPacket, Packet, HEADER_SIZE};

#[allow(clippy::enum_variant_names)]
#[allow(dead_code)] // TODO
#[derive(Debug)]
pub(crate) enum Error {
    InvalidAdvertisedType {
        found: u8,
        advertised: u8,
    },
    InvalidAdvertisedLength {
        type_id: u8,
        advertised: usize,
        found: usize,
    },
    InvalidLength {
        type_id: u8,
     
    
pub(crate) fn tlv_from_bytes<P: Packet>(header: &HeaderPacket, bytes: &[u8]) -> Result<P, Error> {
    if header.packet_type != P::ID {
        return Err(Error::InvalidAdvertisedType {
            found: P::ID,
            advertised: header.packet_type,
        });
    }

    if header.packet_length as usize != bytes.len() {
        return Err(Error::InvalidAdvertisedLength {
            type_id: header.packet_type,
            advertised: header.packet_length as usize,
            found: bytes.len(),
        });
    }

    if !P::size_range().contains(&bytes.len()) {
        return Err(Error::InvalidLength {
            type_id: header.packet_type,
            len: bytes.len(),
        });
    }

    Ok(P::from_bytes(bytes))
}