pub fn decode<TBuffer>(&mut self, buffer: TBuffer) -> (DecodeResult<Request<BytesMut>>, usize)
    where
        TBuffer: AsRef<[u8]>,
    {
        use self::DecodeState::*;
        use self::GenericDecodeResult::*;

        let mut buffer = buffer.as_ref();
        let buffer_size = buffer.len();

        loop {
            let decode_result = match self.state {
                Header => self.decode_header(&mut buffer),
                Method => self.decode_method(&mut buffer),
                URI => self.decode_uri(&mut buffer),
                Version => self.decode_version(&mut buffer),
                Body => self.decode_body(&mut buffer),
                End => {
                    let difference = buffer_size - buffer.len();
                    let builder = mem::replace(&mut self.builder, Request::builder());
                    let request = builder.build().expect("no request parts should be missing");
                    self.state = Method;

                    break (Complete(request), difference);
                }
                DecodeState::Error(error) => break (DecodeResult::Error(error), 0),
            };

            match decode_result {
                Complete(_) => continue,
                DecodeResult::Error(error) => {
                    self.state = DecodeState::Error(error);
                    break (DecodeResult::Error(error), buffer_size - buffer.len());
                }
                Incomplete => break (Incomplete, buffer_size - buffer.len()),
            }
        }
    }
/// Attempts to decode a request from `buffer`.
///
/// There are three different cases that can occur as the result of this function:
///
///  1. A request is successfully decoded and is returned along with the number of bytes
///     decoded. The function can be called again with the same buffer or some other data to
///     continue decoding.
///  2. A request has been partially decoded but more data is needed to finish decoding. The
///     function must be called again with the rest of the data in order to finish the decoding.
///
///     In this case, it could be possible that it has already been detected that the request is
///     invalid, but the "stream" is not corrupt and parsing can continue.
///  3. An error occured during parsing. The decoder is in a state in which decoding can no
///     longer occur. In this case, the decoder must be reset before any further decoding.
///
/// As long as the decoder is in a valid parsing state, this function can be called repeatedly
/// with more and more data. The state can be checked using the [`Decoder::state`] function. If
/// the state is invalid, the decoder can be reset to its default state using the
/// [`Decoder::reset`] function.
///
/// The number of bytes decoded is the number of bytes successfully decoded, but is likely
/// incorrect in the case of an error.
///
/// # Examples
///
/// Decoding a full request:
///
/// ```
/// # extern crate bytes;
/// # extern crate rtsp;
/// #
/// use bytes::BytesMut;
/// use std::convert::TryFrom;
///
/// use rtsp::header::name::HeaderName;
/// use rtsp::header::value::HeaderValue;
/// use rtsp::method::Method;
/// use rtsp::protocol::codec::decoder::request::Decoder;
/// use rtsp::request::Request;
/// use rtsp::uri::request::URI;
///
/// # fn main() {
/// let buffer =
///     "GET_PARAMETER rtsp://example.com RTSP/2.0\r\n\
///      Content-Length: 4\r\n\
///      Session: QKyjN8nt2WqbWw4tIYof52; timeout = 60\r\n\
///      \r\n\
///      Body";
/// let mut decoder = Decoder::new();
/// let (result, bytes_decoded) = decoder.decode(buffer);
/// let expected_request = Request::<()>::builder()
///     .with_method(Method::GetParameter)
///     .with_uri(URI::try_from("rtsp://example.com").unwrap())
///     .with_header(HeaderName::ContentLength, HeaderValue::try_from("4").unwrap())
///     .with_header(
///         HeaderName::Session,
///         HeaderValue::try_from("QKyjN8nt2WqbWw4tIYof52; timeout = 60").unwrap()
///     )
///     .with_body(BytesMut::from("Body".as_bytes()))
///     .build()
///     .unwrap();
///
/// assert_eq!(result.unwrap(), expected_request);
/// assert_eq!(bytes_decoded, buffer.len());
/// # }
/// ```
///
/// Decoding a partial request:
///
/// ```
/// # extern crate bytes;
/// # extern crate rtsp;
/// #
/// use bytes::BytesMut;
/// use std::convert::TryFrom;
///
/// use rtsp::header::name::HeaderName;
/// use rtsp::header::value::HeaderValue;
/// use rtsp::method::Method;
/// use rtsp::protocol::codec::decoder::request::Decoder;
/// use rtsp::request::Request;
/// use rtsp::uri::request::URI;
///
/// # fn main() {
/// let buffer = "SETUP * RTSP/2.0\r\n";
/// let mut decoder = Decoder::new();
/// let (result, bytes_decoded) = decoder.decode(buffer);
///
/// assert!(result.is_incomplete());
/// assert_eq!(bytes_decoded, buffer.len());
///
/// let buffer =
///     "Content-Length: 5\r\n\
///     \r\n\
///     Test!";
///
/// let (result, bytes_decoded) = decoder.decode(buffer);
///
/// let expected_request = Request::<()>::builder()
///     .with_method(Method::Setup)
///     .with_uri(URI::asterisk())
///     .with_header(HeaderName::ContentLength, HeaderValue::try_from("5").unwrap())
///     .with_body(BytesMut::from("Test!".as_bytes()))
///     .build()
///     .unwrap();
///
/// assert_eq!(result.unwrap(), expected_request);
/// assert_eq!(bytes_decoded, buffer.len());
/// # }
/// ```
Attempts to decode a request from `buffer`.
There are three different cases that can occur as the result of this function.

1. A request is successfully decoded and is returned along with the number of bytes
decoded. The function can be called again with the same buffer or some other data to
continue decoding.
2. A request has been partially decoded but more data is needed to finish decoding. The
function must be called again with the rest of the data in order to finish the decoding.

In this case, it could be possible that it has already been detected that the request is
invalid, but the "stream" is not corrupt and parsing can continue.
3. An error occured during parsing. The decoder is in a state in which decoding can no
longer occur. In this case, the decoder must be reset before any further decoding.

As long as the decoder is in a valid parsing state, this function can be called repeatedly
with more and more data. The state can be checked using the [`Decoder::state`] function. If
the state is invalid, the decoder can be reset to its default state using the
[`Decoder::reset`] function.

The number of bytes decoded is the number of bytes successfully decoded, but is likely
incorrect in the case of an error.
Attempts to decode a request from ` buffer ` . There are three different cases that can occur as the result of this function . 1 . A request is successfully decoded and is returned along with the number of bytes decoded . The function can be called again with the same buffer or some other data to continue decoding . 2 . A request has been partially decoded but more data is needed to finish decoding . The function must be called again with the rest of the data in order to finish the decoding . In this case it could be possible that it has already been detected that the request is invalid but the " stream " is not corrupt and parsing can continue . 3 . An error occured during parsing . The decoder is in a state in which decoding can no longer occur . In this case the decoder must be reset before any further decoding . As long as the decoder is in a valid parsing state this function can be called repeatedly with more and more data . The state can be checked using the [ ` Decoder :: state ` ] function . If the state is invalid the decoder can be reset to its default state using the [ ` Decoder :: reset ` ] function . The number of bytes decoded is the number of bytes successfully decoded but is likely incorrect in the case of an error .
pub fn decode<TBuffer>(&mut self, buffer: TBuffer) -> (DecodeResult<Request<BytesMut>>, usize)
    where
        TBuffer: AsRef<[u8]>,
    {
        use self::DecodeState::*;
        use self::GenericDecodeResult::*;
        let mut buffer = buffer.as_ref();
        let buffer_size = buffer.len();
        loop {
            let decode_result = match self.state {
                Header => self.decode_header(&mut buffer),
                Method => self.decode_method(&mut buffer),
                URI => self.decode_uri(&mut buffer),
                Version => self.decode_version(&mut buffer),
                Body => self.decode_body(&mut buffer),
                End => {
                    let difference = buffer_size - buffer.len();
                    let builder = mem::replace(&mut self.builder, Request::builder());
                    let request = builder.build().expect("no request parts should be missing");
                    self.state = Method;
                    break (Complete(request), difference);
                }
                DecodeState::Error(error) => break (DecodeResult::Error(error), 0),
            };
            match decode_result {
                Complete(_) => continue,
                DecodeResult::Error(error) => {
                    self.state = DecodeState::Error(error);
                    break (DecodeResult::Error(error), buffer_size - buffer.len());
                }
                Incomplete => break (Incomplete, buffer_size - buffer.len()),
            }
        }
    }
pub fn decode < TBuffer > ( & mut self , buffer : TBuffer ) -> ( DecodeResult < Request < BytesMut > > , usize ) where TBuffer : AsRef < [ u8 ] > , { use self :: DecodeState :: * ; use self :: GenericDecodeResult :: * ; let mut buffer = buffer . as_ref ( ) ; let buffer_size = buffer . len ( ) ; loop { let decode_result = match self . state { Header => self . decode_header ( & mut buffer ) , Method => self . decode_method ( & mut buffer ) , URI => self . decode_uri ( & mut buffer ) , Version => self . decode_version ( & mut buffer ) , Body => self . decode_body ( & mut buffer ) , End => { let difference = buffer_size - buffer . len ( ) ; let builder = mem :: replace ( & mut self . builder , Request :: builder ( ) ) ; let request = builder . build ( ) . expect ( " " ) ; self . state = Method ; break ( Complete ( request ) , difference ) ; } DecodeState :: Error ( error ) => break ( DecodeResult :: Error ( error ) , 0 ) , } ; match decode_result { Complete ( _ ) => continue , DecodeResult :: Error ( error ) => { self . state = DecodeState :: Error ( error ) ; break ( DecodeResult :: Error ( error ) , buffer_size - buffer . len ( ) ) ; } Incomplete => break ( Incomplete , buffer_size - buffer . len ( ) ) , } } }
Attempts to decode a request from `buffer`.
Attempts to decode a request from ` buffer ` .
&mut self
buffer
TBuffer
&mut self
buffer
TBuffer
examples
Decoding a full request.


Decoding a full request .
extern crate bytes;
None
extern crate rtsp;
use bytes::BytesMut;
use std::convert::TryFrom.

use rtsp::header::name::HeaderName;
use rtsp::header::value::HeaderValue;
use rtsp::method::Method;
use rtsp::protocol::codec::decoder::request::Decoder;
use rtsp::request::Request;
use rtsp::uri::request::URI.
use bytes :: BytesMut ; use std :: convert :: TryFrom . use rtsp :: header :: name :: HeaderName ; use rtsp :: header :: value :: HeaderValue ; use rtsp :: method :: Method ; use rtsp :: protocol :: codec :: decoder :: request :: Decoder ; use rtsp :: request :: Request ; use rtsp :: uri :: request :: URI .
fn main() {
let buffer
"GET_PARAMETER rtsp://example.com RTSP/2.0\r\n\
Content-Length: 4\r\n\
Session: QKyjN8nt2WqbWw4tIYof52; timeout = 60\r\n\
\r\n\
Body";
let mut decoder = Decoder::new();
let (result, bytes_decoded) = decoder.decode(buffer);
let expected_request = Request::<()>::builder()
.with_method(Method::GetParameter)
.with_uri(URI::try_from("rtsp://example.com").unwrap())
.with_header(HeaderName::ContentLength, HeaderValue::try_from("4").unwrap())
.with_header(
HeaderName::Session,
HeaderValue::try_from("QKyjN8nt2WqbWw4tIYof52; timeout = 60").unwrap()
)
.with_body(BytesMut::from("Body".as_bytes()))
.build()
.unwrap().

assert_eq!(result.unwrap(), expected_request);
assert_eq!(bytes_decoded, buffer.len()).
let buffer " GET_PARAMETER rtsp : // example . com RTSP / 2 . 0 \ r \ n \ Content - Length : 4 \ r \ n \ Session : QKyjN8nt2WqbWw4tIYof52 ; timeout = 60 \ r \ n \ \ r \ n \ Body " ; let mut decoder = Decoder :: new () ; let ( result bytes_decoded ) = decoder . decode ( buffer ) ; let expected_request = Request :: < () > :: builder () . with_method ( Method :: GetParameter ) . with_uri ( URI :: try_from ( " rtsp : // example . com " ) . unwrap () ) . with_header ( HeaderName :: ContentLength HeaderValue :: try_from ( " 4 " ) . unwrap () ) . with_header ( HeaderName :: Session HeaderValue :: try_from ( " QKyjN8nt2WqbWw4tIYof52 ; timeout = 60 " ) . unwrap () ) . with_body ( BytesMut :: from ( " Body " . as_bytes () )) . build () . unwrap () . assert_eq! ( result . unwrap () expected_request ) ; assert_eq! ( bytes_decoded buffer . len () ) .
}


Decoding a partial request.


Decoding a partial request .
extern crate bytes;
None
extern crate rtsp;
use bytes::BytesMut;
use std::convert::TryFrom.

use rtsp::header::name::HeaderName;
use rtsp::header::value::HeaderValue;
use rtsp::method::Method;
use rtsp::protocol::codec::decoder::request::Decoder;
use rtsp::request::Request;
use rtsp::uri::request::URI.
use bytes :: BytesMut ; use std :: convert :: TryFrom . use rtsp :: header :: name :: HeaderName ; use rtsp :: header :: value :: HeaderValue ; use rtsp :: method :: Method ; use rtsp :: protocol :: codec :: decoder :: request :: Decoder ; use rtsp :: request :: Request ; use rtsp :: uri :: request :: URI .
fn main() {
let buffer = "SETUP * RTSP/2.0\r\n";
let mut decoder = Decoder::new();
let (result, bytes_decoded) = decoder.decode(buffer).

assert!(result.is_incomplete());
assert_eq!(bytes_decoded, buffer.len()).

let buffer
"Content-Length: 5\r\n\
\r\n\
Test!".

let (result, bytes_decoded) = decoder.decode(buffer).

let expected_request = Request::<()>::builder()
.with_method(Method::Setup)
.with_uri(URI::asterisk())
.with_header(HeaderName::ContentLength, HeaderValue::try_from("5").unwrap())
.with_body(BytesMut::from("Test!".as_bytes()))
.build()
.unwrap().

assert_eq!(result.unwrap(), expected_request);
assert_eq!(bytes_decoded, buffer.len()).
let buffer = " SETUP * RTSP / 2 . 0 \ r \ n " ; let mut decoder = Decoder :: new () ; let ( result bytes_decoded ) = decoder . decode ( buffer ) . assert! ( result . is_incomplete () ) ; assert_eq! ( bytes_decoded buffer . len () ) . let buffer " Content - Length : 5 \ r \ n \ \ r \ n \ Test! " . let ( result bytes_decoded ) = decoder . decode ( buffer ) . let expected_request = Request :: < () > :: builder () . with_method ( Method :: Setup ) . with_uri ( URI :: asterisk () ) . with_header ( HeaderName :: ContentLength HeaderValue :: try_from ( " 5 " ) . unwrap () ) . with_body ( BytesMut :: from ( " Test! " . as_bytes () )) . build () . unwrap () . assert_eq! ( result . unwrap () expected_request ) ; assert_eq! ( bytes_decoded buffer . len () ) .
}
None