Skip to main content

decode_position_notification

Function decode_position_notification 

Source
pub fn decode_position_notification(
    bytes: &[u8],
) -> Result<Position, DecodePositionNotificationError>
Expand description

Decodes a position notification emitted by the position characteristic.

Bytes two through thirty-three contain the packed state of all 64 squares. Any trailing bytes are transport metadata and are ignored.

§Examples

use chessnut_move::protocol::{
    Color, File, Piece, PieceKind, Rank, Square,
    decode_position_notification,
};

let mut notification = [0_u8; 34];
notification[2] = 0x21; // h8 = black queen, g8 = black king
let position = decode_position_notification(&notification)?;

assert_eq!(
    position.piece_at(Square::new(File::H, Rank::Eight)),
    Some(Piece {
        color: Color::Black,
        kind: PieceKind::Queen,
    }),
);

§Errors

Returns DecodePositionNotificationError::NotificationTooShort when fewer than 34 bytes are supplied.

Returns DecodePositionNotificationError::InvalidPiece when a square contains an unknown piece code.