rustyphoenixrequest 1.6.1

This library provides methods to mock request API. This is the Rust equivalent of https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS2/network/PhoenixRequest
Documentation
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

use crate::phoenix_request::{
	PRequestParam,
	PResponseParam,
	PhoenixRequestStream,
	PhoenixRequestMockStream,
	PhoenixRequestServer,
	PhoenixRequestMockServer
};

///Mode of the request server
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum PRequestMode {
	NoMock,
	Mock,
	MockRecord,
}


///Switcher between two backends
pub struct PGenericRequestStream<TBackend, TMockBackend> {
	///Mode of the server to choose between two abstact classes
	mode: PRequestMode,
	///Principal backend
	backend: TBackend,
	///Mock backend
	mock_backend: TMockBackend,
}


//We need to restrict TBackend and TMockBackend to types which implement the PhoenixRequestStream trait
impl<TBackend: PhoenixRequestStream, TMockBackend: PhoenixRequestStream + PhoenixRequestMockStream> PGenericRequestStream<TBackend, TMockBackend> {
	///Set the mode of the PGenericRequestStream
	/// # Parameters
	/// - `mode` - mode of the PGenericRequestStream
	pub fn set_mode(&mut self, mode: PRequestMode){
		self.mode = mode;
		self.mock_backend.set_is_record(mode == PRequestMode::MockRecord);
	}
	///Get the mode of the PGenericRequestStream
	/// # Returns
	/// mode of the PGenericRequestStream
	pub fn get_mode(&self) -> PRequestMode{
		return self.mode;
	}
	///Set the prefix of the current mock
	/// # Parameters
	/// - `prefix` - prefix of the current mock
	pub fn set_prefix(&mut self, prefix: &String){
		self.mock_backend.set_prefix(prefix);
	}
	///Recieved a request from a client
	/// # Parameters
	/// - `request` : PRequestParam recievred from the client
	/// # Returns
	/// true on success, false otherwise
	pub fn recv_request(&mut self, request: &mut PRequestParam) -> bool{
		if self.mode == PRequestMode::NoMock {
			return self.backend.recv_request(request);
		}else if self.mode == PRequestMode::Mock {
			return self.mock_backend.recv_request(request);
		}else{
			let status = self.backend.recv_request(request);
			self.mock_backend.recv_request(request);
			return status;
		}
	}
	
	///Send response to the client
	/// # Returns
	/// corresponding request
	pub fn send_response(&mut self, response: &PResponseParam){
		if self.mode == PRequestMode::NoMock {
			self.backend.send_response(response);
		}else if self.mode == PRequestMode::Mock {
			self.mock_backend.send_response(response);
		}else{
			self.backend.send_response(response);
			self.mock_backend.send_response(response);
		}
	}
}


///Server Switcher between two backends
pub struct PGenericRequestServer<TBackend, TMockBackend>
	where TBackend: PhoenixRequestServer,
		TMockBackend: PhoenixRequestServer + PhoenixRequestMockServer,
		<TBackend as PhoenixRequestServer>::Stream: PhoenixRequestStream,
		<TMockBackend>::Stream: PhoenixRequestStream + PhoenixRequestMockStream
{
	///Mode of the server to choose between two abstact classes
	mode: PRequestMode,
	///Principal backend
	backend: TBackend,
	///Mock backend
	mock_backend: TMockBackend,
	///Prefix of the mock files to be used
	mock_prefix: String,
	///Number of realised query 
	nb_query: u64,
}


//We need to restrict TBackend and TMockBackend to types which implement the PGenericRequestServer trait
impl<TBackend: PhoenixRequestServer, TMockBackend: PhoenixRequestServer + PhoenixRequestMockServer> PGenericRequestServer<TBackend, TMockBackend>
	where TBackend: PhoenixRequestServer,
		TMockBackend: PhoenixRequestServer + PhoenixRequestMockServer,
		<TBackend as PhoenixRequestServer>::Stream: PhoenixRequestStream,
		<TMockBackend>::Stream: PhoenixRequestStream + PhoenixRequestMockStream
{
	///Create a PGenericRequestServer
	/// # Parameters
	/// - `mode` - mode of the PGenericRequestServer
	/// # Returns
	/// constructed PGenericRequestServer
	pub fn new(hostname: &String, port: u16, mode: PRequestMode) -> Self {
		let mut other = PGenericRequestServer{
			mode: mode,
			backend: TBackend::new(hostname, port),
			mock_backend: TMockBackend::new(hostname, port),
			mock_prefix: String::from(""),
			nb_query: 0
		};
		other.mock_backend.set_is_record(mode == PRequestMode::MockRecord);
		return other;
	}
	///Set the mode of the PGenericRequestStream
	/// # Parameters
	/// - `mode` - mode of the PGenericRequestStream
	pub fn set_mode(&mut self, mode: PRequestMode){
		self.mode = mode;
		self.mock_backend.set_is_record(mode == PRequestMode::MockRecord);
	}
	///Get the mode of the PGenericRequestStream
	/// # Returns
	/// mode of the PGenericRequestStream
	pub fn get_mode(&self) -> PRequestMode{
		return self.mode;
	}
	///Set the prefix of the current mock
	/// # Parameters
	/// - `prefix` - prefix of the current mock
	pub fn set_prefix(&mut self, prefix: &String){
		self.mock_backend.set_prefix(prefix);
	}
	
	///Accept a connection to a client
	/// # Returns
	/// Stream to get the answer and response to the client
	pub fn accept(&mut self) -> PGenericRequestStream<TBackend::Stream, TMockBackend::Stream>{
		let mut other = PGenericRequestStream{
			mode: self.mode,
			backend: self.backend.accept(),
			mock_backend: self.mock_backend.accept(),
		};
		let full_prefix = format!("{}_{}", self.mock_prefix, self.nb_query);
		other.set_prefix(&full_prefix);
		self.nb_query += 1;
		return other;
	}
	
}