Skip to main content

GenericParser

Struct GenericParser 

Source
pub struct GenericParser { /* private fields */ }
Expand description

Parser générique pour protocoles UART.

Accumule les octets reçus, valide l’en-tête et retourne une référence vers la trame complète dès qu’elle est disponible.

Note : La vérification du CRC n’est pas effectuée par ce parser. Elle doit être réalisée par le code appelant après réception d’une trame complète, car chaque protocole définit son propre algorithme.

§Exemple

use uart_proto_detector::{GenericParser, ParserConfig, PacketLengthStrategy};

let config = ParserConfig {
    header: &[0xAA, 0x55],
    length_strategy: PacketLengthStrategy::Fixed(6),
};
let mut parser = GenericParser::new(config);

// Simuler un flux d'octets
let bytes: &[u8] = &[0xAA, 0x55, 0x01, 0x02, 0x03, 0x04];
for &b in bytes {
    if let Ok(Some(packet)) = parser.parse_byte(b) {
        assert_eq!(packet.len(), 6);
    }
}

Implementations§

Source§

impl GenericParser

Source

pub fn new(config: ParserConfig) -> Self

Crée un nouveau parser à partir d’une ParserConfig.

Source

pub fn reset(&mut self)

Réinitialise l’état interne du parser.

Appelé automatiquement après chaque trame complète ou en cas d’erreur. Peut aussi être appelé manuellement en cas de timeout ou de flush du bus.

Source

pub fn parse_byte(&mut self, byte: u8) -> Result<Option<&[u8]>, ParserError>

Soumet un octet au parser.

§Valeur de retour
RésultatSignification
Ok(None)Trame en cours d’accumulation, aucune action requise.
Ok(Some(…))Trame complète disponible dans la slice retournée.
Err(…)Erreur de parsing ; le parser a été réinitialisé.

La slice retournée par Ok(Some(…)) pointe sur le buffer interne et n’est valide que jusqu’au prochain appel à parse_byte ou reset.

§Erreurs

Retourne ParserError::BufferOverflow si le buffer est plein avant qu’une trame valide ne soit reçue, ou ParserError::PacketTooLarge si la longueur calculée dépasse BUFFER_SIZE.

Source

pub fn bytes_accumulated(&self) -> usize

Retourne le nombre d’octets actuellement accumulés dans le buffer.

Source

pub fn expected_length(&self) -> Option<usize>

Retourne la longueur de trame attendue, si elle a déjà été déterminée.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.