p101_enc 0.11.0

Library to convert Olivetti P101 program to and from different encodings
Documentation
mod standard_encoding;
mod ascii_encoding;

pub use standard_encoding::*;
pub use ascii_encoding::*;

use p101_is::instruction::*;

pub enum DecodeResult {
    /// Wraps the decoded instruction
    Ok(Instruction),

    /// Input string contains no instruction
    None,

    /// Error decoding input string
    Err(String),
}

/// The Encoding trait defines all methods that an encoder/decoder must implement.
pub trait Encoding {
    /// Given an instruction, convert it to its string representation.
    fn encode(&self, i: &Instruction) -> String;
    
    /// Given a string, try to convert it back to an instruction according to the implemented encoding.
    ///
    /// # Errors
    ///
    /// Decoding may fail.
    /// - When the string violates the encoding, a DecodeResult::Err(String) value is returned. The string payload describes the decoding error.
    /// - When the string is empty, it decodes to DecodeResult::None.
    fn decode_instr(&self, text: &str) -> DecodeResult;

    /// Given a string, try to convert it back to a "constant as instruction" according to the implemented encoding.
    ///
    /// # Errors
    ///
    /// Decoding may fail.
    /// - When the string violates the encoding, a DecodeResult::Err(String) value is returned. The string payload describes the decoding error.
    /// - When the string is empty, it decodes to DecodeResult::None.
    fn decode_cai(&self, text: &str) -> DecodeResult;
}