1
use serde::{Deserialize, Serialize};
2

            
3
use crate::{
4
    commands::{Command, Request, RequestParserError, RequestParserResult, ResponseParserError},
5
    common::{StickerType, Uri},
6
    request_tokenizer::RequestTokenizer,
7
    response_tokenizer::{ResponseAttributes, get_next_property},
8
};
9

            
10
pub struct StickerGet;
11

            
12
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13
pub struct StickerGetRequest {
14
    pub sticker_type: StickerType,
15
    pub uri: Uri,
16
    pub name: String,
17
}
18

            
19
pub type StickerGetResponse = String;
20

            
21
impl Command for StickerGet {
22
    type Request = StickerGetRequest;
23
    type Response = StickerGetResponse;
24
    const COMMAND: &'static str = "sticker get";
25

            
26
    fn serialize_request(&self, request: Self::Request) -> String {
27
        format!(
28
            "{} {} {} {}",
29
            Self::COMMAND,
30
            request.sticker_type,
31
            request.uri,
32
            request.name
33
        )
34
    }
35

            
36
    fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
37
        let sticker_type = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
38
        let sticker_type = sticker_type
39
            .parse()
40
            .map_err(|_| RequestParserError::SyntaxError(1, sticker_type.to_owned()))?;
41

            
42
        let uri = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
43
        let uri = uri
44
            .parse()
45
            .map_err(|_| RequestParserError::SyntaxError(1, uri.to_owned()))?;
46

            
47
        let name = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
48
        let name = name
49
            .parse()
50
            .map_err(|_| RequestParserError::SyntaxError(1, name.to_owned()))?;
51

            
52
        debug_assert!(parts.next().is_none());
53

            
54
        Ok((Request::StickerGet(sticker_type, uri, name), ""))
55
    }
56

            
57
    fn parse_response(
58
        parts: ResponseAttributes<'_>,
59
    ) -> Result<Self::Response, ResponseParserError<'_>> {
60
        let parts: Vec<_> = parts.into();
61
        let mut parts = parts.into_iter().peekable();
62
        let (key, sticker) = get_next_property!(parts, Text);
63
        debug_assert!(parts.peek().is_none());
64
        debug_assert!(key == "sticker");
65
        Ok(sticker.to_string())
66
    }
67
}