jsxbin-parser 1.0.0

Binary parser for Adobe JSX .jsxbin files with comprehensive AST representation
Documentation
use crate::error::{Error, Result};
use crate::types::Token;

/// Decodes tokens from binary format
pub struct TokenDecoder;

impl TokenDecoder {
    pub fn new() -> Self {
        TokenDecoder
    }

    /// Decode a token from opcode
    pub fn decode_token(&self, opcode: u8) -> Result<Token> {
        match opcode {
            0x00 => Ok(Token::EndStatement),
            0x01 => Ok(Token::Number(0.0)),
            0x02 => Ok(Token::String(String::new())),
            0x03 => Ok(Token::Identifier(String::new())),
            0x04 => Ok(Token::Function),
            0x05 => Ok(Token::If),
            0x06 => Ok(Token::Else),
            0x07 => Ok(Token::For),
            0x08 => Ok(Token::While),
            0x09 => Ok(Token::Return),
            0x0A => Ok(Token::Var),
            0x0B => Ok(Token::Assignment),
            _ => Err(Error::InvalidToken(format!("Unknown opcode: 0x{:02X}", opcode))),
        }
    }

    /// Decode multiple tokens
    pub fn decode_tokens(&self, opcodes: Vec<u8>) -> Result<Vec<Token>> {
        opcodes
            .iter()
            .map(|&opcode| self.decode_token(opcode))
            .collect()
    }
}

impl Default for TokenDecoder {
    fn default() -> Self {
        Self::new()
    }
}