fn decode(mut self, chr: char) -> Result<DecodeBytes, DecoderError> {
        use PartialResult::*;

        // Decode or fetch the length of the bytes
        let length = match self.length {
            MatchMore(so_far)    => {
                self.length = CanvasDecoder::decode_compact_id(chr, so_far)?;

                if let &FullMatch(0) = &self.length {
                    self.byte_encoding = FullMatch(vec![]);
                }
                return Ok(self);
            }

            FullMatch(length)    => {
                self.length = PartialResult::FullMatch(length);
                length as usize
            }
        };

        // Try to decode the rest of the string
        match self.byte_encoding {
            FullMatch(_)                    => {
                // Already finished matching
                return Err(DecoderError::NotReady);
            }

            MatchMore(mut encoded_bytes)    => {
                encoded_bytes.push(chr);

                // Every 4 encoded bytes makes up 3 output bytes (and the encoding rounds up overall)
                let encoded_length = (encoded_bytes.len()/4)*3;

                if encoded_length >= length {
                    // Decode the bytes
                    let mut decoded_bytes = vec![];

                    // 4 characters decode into 3 bytes
                    for (a, b, c, d) in encoded_bytes.chars().tuples() {
                        // Decode to 6-bit values
                        let a = CanvasDecoder::decode_base64(a)?;
                        let b = CanvasDecoder::decode_base64(b)?;
                        let c = CanvasDecoder::decode_base64(c)?;
                        let d = CanvasDecoder::decode_base64(d)?;

                        // Decode to 8-bit values
                        let a = a | ((b<<6)&0xff);
                        let b = (b>>2) | ((c<<4)&0xff);
                        let c = (c>>4) | ((d<<2)&0xff);

                        // Add to the result
                        decoded_bytes.push(a);
                        decoded_bytes.push(b);
                        decoded_bytes.push(c);
                    }

                    // Trim to the actual required length
                    decoded_bytes.truncate(length);

                    // Have decoded the bytes for this object
                    self.byte_encoding = FullMatch(decoded_bytes);
                } else {
                    // Not enough bytes to make up the full string yet
                    self.byte_encoding = MatchMore(encoded_bytes);
                }

                return Ok(self);
            }
        }
    }
///
/// Decodes a single character and returns the new state of the decoder
///
Decodes a single character and returns the new state of the decoder
Decodes a single character and returns the new state of the decoder
fn decode(mut self, chr: char) -> Result<DecodeBytes, DecoderError> {
        use PartialResult::*;
        // Decode or fetch the length of the bytes
        let length = match self.length {
            MatchMore(so_far)    => {
                self.length = CanvasDecoder::decode_compact_id(chr, so_far)?;
                if let &FullMatch(0) = &self.length {
                    self.byte_encoding = FullMatch(vec![]);
                }
                return Ok(self);
            }
            FullMatch(length)    => {
                self.length = PartialResult::FullMatch(length);
                length as usize
            }
        };
        // Try to decode the rest of the string
        match self.byte_encoding {
            FullMatch(_)                    => {
                // Already finished matching
                return Err(DecoderError::NotReady);
            }
            MatchMore(mut encoded_bytes)    => {
                encoded_bytes.push(chr);
                // Every 4 encoded bytes makes up 3 output bytes (and the encoding rounds up overall)
                let encoded_length = (encoded_bytes.len()/4)*3;
                if encoded_length >= length {
                    // Decode the bytes
                    let mut decoded_bytes = vec![];
                    // 4 characters decode into 3 bytes
                    for (a, b, c, d) in encoded_bytes.chars().tuples() {
                        // Decode to 6-bit values
                        let a = CanvasDecoder::decode_base64(a)?;
                        let b = CanvasDecoder::decode_base64(b)?;
                        let c = CanvasDecoder::decode_base64(c)?;
                        let d = CanvasDecoder::decode_base64(d)?;
                        // Decode to 8-bit values
                        let a = a | ((b<<6)&0xff);
                        let b = (b>>2) | ((c<<4)&0xff);
                        let c = (c>>4) | ((d<<2)&0xff);
                        // Add to the result
                        decoded_bytes.push(a);
                        decoded_bytes.push(b);
                        decoded_bytes.push(c);
                    }
                    // Trim to the actual required length
                    decoded_bytes.truncate(length);
                    // Have decoded the bytes for this object
                    self.byte_encoding = FullMatch(decoded_bytes);
                } else {
                    // Not enough bytes to make up the full string yet
                    self.byte_encoding = MatchMore(encoded_bytes);
                }
                return Ok(self);
            }
        }
    }
fn decode ( mut self , chr : char ) -> Result < DecodeBytes , DecoderError > { use PartialResult :: * ; // Decode or fetch the length of the bytes let length = match self . length { MatchMore ( so_far ) => { self . length = CanvasDecoder :: decode_compact_id ( chr , so_far ) ? ; if let & FullMatch ( 0 ) = & self . length { self . byte_encoding = FullMatch ( vec ! [ ] ) ; } return Ok ( self ) ; } FullMatch ( length ) => { self . length = PartialResult :: FullMatch ( length ) ; length as usize } } ; // Try to decode the rest of the string match self . byte_encoding { FullMatch ( _ ) => { // Already finished matching return Err ( DecoderError :: NotReady ) ; } MatchMore ( mut encoded_bytes ) => { encoded_bytes . push ( chr ) ; // Every 4 encoded bytes makes up 3 output bytes (and the encoding rounds up overall) let encoded_length = ( encoded_bytes . len ( ) / 4 ) * 3 ; if encoded_length >= length { // Decode the bytes let mut decoded_bytes = vec ! [ ] ; // 4 characters decode into 3 bytes for ( a , b , c , d ) in encoded_bytes . chars ( ) . tuples ( ) { // Decode to 6-bit values let a = CanvasDecoder :: decode_base64 ( a ) ? ; let b = CanvasDecoder :: decode_base64 ( b ) ? ; let c = CanvasDecoder :: decode_base64 ( c ) ? ; let d = CanvasDecoder :: decode_base64 ( d ) ? ; // Decode to 8-bit values let a = a | ( ( b << 6 ) & 0xff ) ; let b = ( b >> 2 ) | ( ( c << 4 ) & 0xff ) ; let c = ( c >> 4 ) | ( ( d << 2 ) & 0xff ) ; // Add to the result decoded_bytes . push ( a ) ; decoded_bytes . push ( b ) ; decoded_bytes . push ( c ) ; } // Trim to the actual required length decoded_bytes . truncate ( length ) ; // Have decoded the bytes for this object self . byte_encoding = FullMatch ( decoded_bytes ) ; } else { // Not enough bytes to make up the full string yet self . byte_encoding = MatchMore ( encoded_bytes ) ; } return Ok ( self ) ; } } }
Decodes a single character and returns the new state of the decoder
Decodes a single character and returns the new state of the decoder
mut self
chr
char