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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

/*! Methods of recording and reading the protocol */


pub mod iter;
pub mod build;
pub mod sized;
pub mod array;
pub mod buffer;

use lamansh::iter::cluLamanshIntoIter;
use std::convert::TryFrom;
use std::marker::PhantomData;
use self::sized::LamanshSized;
use lamansh::iter::cluLamanshIter;

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[allow(non_camel_case_types)]
pub struct cluLamansh<'a, NC: LamanshSized + 'static, N: LamanshSized + 'static> {
	array: &'a [u8],
	
	value_head_array: &'a [u8],
	value_array: &'a [u8],
	
	_n_count_phantom: PhantomData<NC>,
	_n_phantom: PhantomData<N>,
}


impl<'a, NC: LamanshSized + 'static, N: LamanshSized + 'static> cluLamansh<'a, NC, N> {
	#[inline]
	pub fn new(array: &'a [u8]) -> Result< Self, cluLamanshErr > {
		Self::array(array)
	}
	
	pub fn array(array: &'a [u8]) -> Result< Self, cluLamanshErr > {
		
		let ( value_head_array, value_array ) = {			
			let byted_count = NC::byted();

			let count_header = {
				match array.get( .. byted_count) {
					Some(a) => NC::read_usize(a)+1,
					_ => return Err( cluLamanshErr::ErrGetSizeArray ),
				}
			};
			

			let size_value = N::byted();
			let n = byted_count + (size_value * count_header);

			let header_array = match array.get(byted_count .. n) {
				Some(a) => a,
				_ => return Err( cluLamanshErr::ErrGetValueHead ),
			};

			let value_array = match array.get(n .. ) {
				Some(a) => a,
				_ => return Err( cluLamanshErr::ErrGetValue ),
			};
			
			( header_array, value_array )
		};
		
		
		
		Ok( 
			Self {
				array: array,
				
				value_head_array: value_head_array,
				value_array: value_array,
				
				_n_count_phantom: PhantomData,
				_n_phantom: PhantomData,
			}
		 )
	}
	
	#[inline]
	pub fn iter<'b>(&'a self) -> cluLamanshIter<'a, 'b, NC, N> {
		cluLamanshIter::new(self)
	}
	#[inline]
	pub fn into_iter(self) -> cluLamanshIntoIter<'a, NC, N> {
		cluLamanshIntoIter::new(self)
	}
}


#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum cluLamanshErr {
	///Empty data is not possible to convert
	ErrEmptyLamansh,
	
	///It is not possible to get the number of elements
	ErrGetSizeArray,

	///Going beyond the header
	ErrGetValueHead,

	///Going beyond the value
	ErrGetValue,

	///Going beyond the value
	ErrGetNextValue,
}



/*
impl<'a, N: LamanshSized + 'a, V: LamanshSized + 'a> Into<&'a [u8]> for cluLamansh<'a, N, V> {
	#[inline]
	fn into(self) -> &'a [u8] {
		self.array
	}
}*/

impl<'a, NC: LamanshSized + 'static, N: LamanshSized + 'static> TryFrom<&'a [u8]> for cluLamansh<'a, NC, N> {
	type Error = cluLamanshErr;
	#[inline]
	fn try_from(array: &'a [u8]) -> Result< cluLamansh<'a, NC, N>, Self::Error > {
		cluLamansh::array(array)
	}
}