rustyphoenixgenericmock 1.8.0

This library provides mock backend for rusty_phoenix_socket. This is the C++ equivalent of [PhoenixGenericMock](https://cta-lapp.pages.in2p3.fr/PHOENIX_LIBS2/serialize-io/PhoenixGenericMock/)
Documentation
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

//{We will need some Debug :
use std::fmt::Debug;
use std::borrow::ToOwned;
//{Let's use the \lib{rustyphoenixdatastream} to build our generic mock backend :
use rustyphoenixdatastream::{datastream_read, datastream_write};

// mod abstract_mock_backend;
use crate::abstract_mock_backend::AbstractMockBackend;

///Struct which handles a generic mock of vector of values
#[derive(Debug, Default, Clone)]
pub struct GenericVecMock<T>
	where T: Default + rustyphoenixdatastream::DataStream + std::cmp::PartialEq<T> + Debug, T: ToOwned<Owned = T>
{
	///Name of the file to be saved or read
	filename: String,
	///Vector of all times to be read or recorded
	vec_value: Vec<T>,
	///Index of the current value to be used
	current_index: usize,
	///True if the current mock is in record mode
	is_mock_record: bool,
}

///Implement the generic mock with vector of value
impl<T: Default + ToOwned<Owned = T> + rustyphoenixdatastream::DataStream + std::cmp::PartialEq<T> + Debug> AbstractMockBackend<T> for GenericVecMock<T>
{
	///Create a GenericVecMock
	/// # Returns
	/// constructed AbstractMockBackend for GenericVecMock
	fn new() -> Self{
		GenericVecMock::<T>{
			filename: String::from(""),
			vec_value: vec![],
			current_index: 0,
			is_mock_record: false
		}
	}
	///Get the current index of the GenericVecMock
	/// # Returns
	/// current index of the GenericVecMock
	fn get_current_index(&self) -> usize{
		return self.current_index;
	}
	///Say if the current mock is in MockRecord mode
	/// # Returns
	/// true if the current mock is in MockRecord mode, false otherwise
	fn get_is_mock_record(&self) -> bool{
		return self.is_mock_record;
	}
	///Abstract method to get the current value
	/// # Parameters
	/// - `value` : current value of the mock
	fn get_current_value(&mut self, value: &mut T){
		if self.is_mock_record {
			panic!("GenericVecMock<T>::get_current_value cannot be call in mock record mode !")
		}
		if self.vec_value.len() == 0 {
			//If the vector of value is empty we have to load the mock file
			match datastream_read(&self.filename, &mut self.vec_value) {
				Ok(file) => file,
				Err(error) => panic!("GenericVecMock<T>::get_current_value : Problem reading the file '{}': error: {:?}", self.filename, error),
			}
		}
		*value = self.vec_value[self.current_index].to_owned();
		self.current_index += 1;
		if self.current_index >= self.vec_value.len() {
			self.current_index = 0;
		}
	}
	///Abstract method to check the current value
	/// # Parameters
	/// - `value` : current value to be checked
	/// # Returns
	/// true if the current value corresponds to the given value, false otherwise
	fn check_current_value(&mut self, value: &T) -> bool{
		let mut reference_value: T = Default::default();
		self.get_current_value(&mut reference_value);
		let is_equal: bool = reference_value == *value;
		if ! is_equal {
			println!("GenericVecMock<T>::check_current_value : different values");
			println!("\t          value = '{:?}'", value);
			println!("\treference_value = '{:?}'", reference_value);
		}
		return is_equal;
	}
	///Append the current in the mock file 
	/// # Parameters
	/// - `value` - current value to be appended into the mock
	fn append(&mut self, value: &T){
		if !self.is_mock_record {
			panic!("GenericVecMock<T>::append cannot be call when record mode is disable !")
		}
		self.vec_value.push(value.to_owned());
	}
	///Set the record mode in the mock
	/// # Parameters
	/// - `is_mock_record` - true if the mock is in record mode
	fn set_is_record(&mut self, is_mock_record: bool){
		self.is_mock_record = is_mock_record;
	}
	///Set the filename of the mock
	/// # Parameters
	/// - `filename` - filename where to save/load the current mock
	fn set_filename(&mut self, filename: &String){
		self.filename = filename.to_string();
	}
	///Close the mock
	fn close(&mut self){
		if self.is_mock_record {
			match datastream_write(&self.filename, &self.vec_value) {
				Ok(is_ok) => is_ok,
				Err(error) => panic!("GenericVecMock<T>::close : Problem writing the file '{}': error: {:?}", self.filename, error),
			}
			self.is_mock_record = false;
			self.current_index = 0;
		}
	}
}

//{We also need a desctructor to save the recorded mock if the mock record is enabled :
impl<T: Default + ToOwned<Owned = T> + rustyphoenixdatastream::DataStream + std::cmp::PartialEq<T> + Debug> Drop for GenericVecMock<T> {
	///Destructor of the GenericVecMock
	fn drop(&mut self){
		self.close();
	}
}