1use framing::{FrameType, Frame, FramePayload, MethodFrame};
2use error::Result;
3
4#[derive(Debug, PartialEq, Eq, Clone)]
5pub struct EncodedMethod(Vec<u8>);
6
7impl EncodedMethod {
8 pub fn new(data: Vec<u8>) -> Self {
9 EncodedMethod(data)
10 }
11
12 pub fn into_inner(self) -> Vec<u8> {
13 self.0
14 }
15
16 pub fn inner(&self) -> &[u8] {
17 &self.0
18 }
19}
20
21pub trait Method {
22 fn decode(method_frame: MethodFrame) -> Result<Self> where Self: Sized;
23 fn encode(&self) -> Result<EncodedMethod>;
24 fn name(&self) -> &'static str;
25 fn id(&self) -> u16;
26 fn class_id(&self) -> u16;
27
28 fn encode_method_frame(&self) -> Result<FramePayload> {
29 let frame = MethodFrame { class_id: self.class_id(), method_id: self.id(), arguments: self.encode()? };
30 frame.encode()
31 }
32
33 fn to_frame(&self, channel: u16) -> Result<Frame> {
34 Ok(Frame {
35 frame_type: FrameType::METHOD,
36 channel: channel,
37 payload: self.encode_method_frame()?,
38 })
39 }
40}