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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::*;

use std::io::{
    Error as IoError,
    ErrorKind as IoErrorKind,
    Result as IoResult
};

/// Sip Response Generator. When build is called the struct
/// is consumed and produces a SipMessage::Response variant.
/// Calling the `code` method before the `build` method is
/// required.
#[derive(Default)]
pub struct ResponseGenerator {
    code: Option<u32>,
    version: Version,
    headers: Headers,
    body: Vec<u8>,
}

impl ResponseGenerator {

    /// Get a new instance of the ResponseGenerator.
    pub fn new() -> ResponseGenerator {
        ResponseGenerator {
            code: None,
            version: Version::default(),
            headers: Headers::new(),
            body: vec![],
        }
    }

    /// Set the response status code.
    pub fn code(mut self, code: u32) -> ResponseGenerator {
        self.code = Some(code);
        self
    }

    /// Add multiple headers to the response header list.
    /// This use's Vec::extend so that the current items
    /// in the header list are kept.
    pub fn headers(mut self, headers: Vec<Header>) -> ResponseGenerator {
        self.headers.extend(headers);
        self
    }

    /// Add a single header to the response header list.
    pub fn header(mut self, header: Header) -> ResponseGenerator {
        self.headers.push(header);
        self
    }

    /// Get a reference to the header list.
    pub fn header_ref(&self) -> &Headers {
        &self.headers
    }

    /// Get a mutable reference to the header list.
    pub fn headers_ref_mut(&mut self) -> &mut Headers {
        &mut self.headers
    }

    /// Set the sip response body. This completely replaces
    /// the current response body.
    pub fn body(mut self, body: Vec<u8>) -> ResponseGenerator {
        self.body = body;
        self
    }

    /// Create the Sip response.
    pub fn build(self) -> IoResult<SipMessage> {
        if let Some(code) = self.code {
            let res = SipMessage::Response {
                code,
                version: self.version,
                headers: self.headers,
                body: self.body,
            };
            Ok(res)
        } else {
            Err(IoError::new(
                IoErrorKind::InvalidInput,
                "ResponseGenerator requires `code` method be called.",
            ))
        }
    }
}