cinema 0.1.0

HTTP record-replay proxy for Rust tests
Documentation
use crate::prelude::*;
use std::net::SocketAddr;
use tokio::net::TcpListener;

pub struct ProxyAddress {
    ip: [u8; 4],
    port: u16,
}

impl ProxyAddress {
    pub fn ip(&self) -> [u8; 4] {
        self.ip
    }

    pub fn port(&self) -> u16 {
        self.port
    }

    pub fn pick_unused() -> Result<Self, CinemaError> {
        let ip = [127, 0, 0, 1];
        let port = portpicker::pick_unused_port().ok_or(CinemaError::FindPort)?;
        Ok(Self { ip, port })
    }

    pub fn socket(&self) -> SocketAddr {
        SocketAddr::from((self.ip, self.port))
    }

    pub async fn bind_tcp(&self) -> Result<TcpListener, CinemaError> {
        TcpListener::bind(self.socket())
            .await
            .map_err(|err| CinemaError::BindTcp(err, self.str()))
    }

    pub fn str(&self) -> String {
        format!(
            "{}:{}",
            self.ip
                .iter()
                .map(|b| b.to_string())
                .collect::<Vec<_>>()
                .join("."),
            self.port
        )
    }

    pub fn origin(&self) -> String {
        format!("http://{}", self.str())
    }
}