Lines
0 %
Functions
use serde::{Deserialize, Serialize};
use crate::{
commands::{Command, Request, RequestParserError, RequestParserResult, ResponseParserError},
common::{StickerType, Uri},
request_tokenizer::RequestTokenizer,
response_tokenizer::{ResponseAttributes, get_next_property},
};
pub struct StickerGet;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StickerGetRequest {
pub sticker_type: StickerType,
pub uri: Uri,
pub name: String,
}
pub type StickerGetResponse = String;
impl Command for StickerGet {
type Request = StickerGetRequest;
type Response = StickerGetResponse;
const COMMAND: &'static str = "sticker get";
fn serialize_request(&self, request: Self::Request) -> String {
format!(
"{} {} {} {}",
Self::COMMAND,
request.sticker_type,
request.uri,
request.name
)
fn parse_request(mut parts: RequestTokenizer<'_>) -> RequestParserResult<'_> {
let sticker_type = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let sticker_type = sticker_type
.parse()
.map_err(|_| RequestParserError::SyntaxError(1, sticker_type.to_owned()))?;
let uri = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let uri = uri
.map_err(|_| RequestParserError::SyntaxError(1, uri.to_owned()))?;
let name = parts.next().ok_or(RequestParserError::UnexpectedEOF)?;
let name = name
.map_err(|_| RequestParserError::SyntaxError(1, name.to_owned()))?;
debug_assert!(parts.next().is_none());
Ok((Request::StickerGet(sticker_type, uri, name), ""))
fn parse_response(
parts: ResponseAttributes<'_>,
) -> Result<Self::Response, ResponseParserError<'_>> {
let parts: Vec<_> = parts.into();
let mut parts = parts.into_iter().peekable();
let (key, sticker) = get_next_property!(parts, Text);
debug_assert!(parts.peek().is_none());
debug_assert!(key == "sticker");
Ok(sticker.to_string())