1
use crate::commands::{
2
    Command, Request, RequestParserError, RequestParserResult, ResponseAttributes,
3
    ResponseParserError, get_next_property,
4
};
5

            
6
pub struct StickerGet;
7

            
8
pub type StickerGetResponse = String;
9

            
10
impl Command for StickerGet {
11
    type Response = StickerGetResponse;
12
    const COMMAND: &'static str = "sticker get";
13

            
14
    fn parse_request(mut parts: std::str::SplitWhitespace<'_>) -> RequestParserResult<'_> {
15
        let sticker_type = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
16
        let sticker_type = sticker_type
17
            .parse()
18
            .map_err(|_| RequestParserError::SyntaxError(1, sticker_type.to_owned()))?;
19

            
20
        let uri = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
21
        let uri = uri
22
            .parse()
23
            .map_err(|_| RequestParserError::SyntaxError(1, uri.to_owned()))?;
24

            
25
        let name = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
26
        let name = name
27
            .parse()
28
            .map_err(|_| RequestParserError::SyntaxError(1, name.to_owned()))?;
29

            
30
        debug_assert!(parts.next().is_none());
31

            
32
        Ok((Request::StickerGet(sticker_type, uri, name), ""))
33
    }
34

            
35
    fn parse_response(
36
        parts: ResponseAttributes<'_>,
37
    ) -> Result<Self::Response, ResponseParserError> {
38
        let parts: Vec<_> = parts.into();
39
        let mut parts = parts.into_iter().peekable();
40
        let (key, sticker) = get_next_property!(parts, Text);
41
        debug_assert!(parts.peek().is_none());
42
        debug_assert!(key == "sticker");
43
        Ok(sticker.to_string())
44
    }
45
}