intrepid_core/extract/
message_uri.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::{Context, Frame};

use super::{Extractor, Message, MessageFrameError};

/// Extract the URI of a message from a frame.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MessageUri(pub String);

impl<State> Extractor<State> for MessageUri {
    type Error = MessageFrameError;

    fn extract(frame: Frame, context: &Context<State>) -> Result<Self, Self::Error>
    where
        Self: Sized,
    {
        let Message(message_frame) = Message::extract(frame, context)?;

        Ok(Self(message_frame.uri))
    }
}

#[test]
fn message_uri() -> Result<(), MessageFrameError> {
    let frame = Frame::message("/test", (), ());
    let context = Context::<()>::default();
    let MessageUri(message_uri) = MessageUri::extract(frame, &context)?;

    assert_eq!(message_uri, "/test");

    Ok(())
}

#[test]
fn failure() -> Result<(), MessageFrameError> {
    use crate::{Error, ExtractorError};

    let context = Context::<()>::default();
    let result = MessageUri::extract_from_frame_and_state(Frame::default(), &context);

    assert!(matches!(
        result.unwrap_err(),
        Error::ExtractorError(ExtractorError::MessageFrameError(
            MessageFrameError::WrongFrame(_)
        ))
    ),);

    Ok(())
}