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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.

/// Sizes for the header part of a packet.
pub mod header {
	/// Minimum size of a packet header.
	pub trait Min {
		/// Get the minimum size.
		fn min() -> usize;
	}

	/// Maximum size of a packet header.
	pub trait Max {
		/// Get the maximum size.
		fn max() -> usize;
	}

	/// Actual size of the packet header.
	pub trait Size {
		/// Get the actual size.
		fn size(&self) -> usize;
	}
}

/// Sizes for the payload part of a packet.
pub mod payload {
	/// Minimum size of a packet payload.
	pub trait Min {
		/// Get the minimum size.
		fn min() -> usize;
	}

	/// Maximum size of a packet payload.
	pub trait Max {
		/// Get the maximum size.
		fn max() -> usize;
	}

	/// Actual size of the packet payload.
	pub trait Size {
		/// Get the actual size.
		fn size(&self) -> usize;
	}
}

/// Minimum size of a packet.
pub trait Min {
	/// Get the minimum size.
	fn min() -> usize;
}

/// Maximum size of a packet.
pub trait Max {
	/// Get the maximum size.
	fn max() -> usize;
}

/// Actual size of the packet.
pub trait Size {
	/// Get the actual size.
	fn size(&self) -> usize;
}

impl<T: header::Min + payload::Min> Min for T {
	fn min() -> usize {
		<Self as header::Min>::min() + <Self as payload::Min>::min()
	}
}

impl<T: header::Max + payload::Max> Max for T {
	fn max() -> usize {
		<Self as header::Max>::max() + <Self as payload::Max>::max()
	}
}

impl<T: header::Size + payload::Size> Size for T {
	fn size(&self) -> usize {
		<Self as header::Size>::size(self) + <Self as payload::Size>::size(self)
	}
}

macro_rules! sized {
	()                             => ();
	($name:ident, )                => ();
	($name:ident => $part:ident; ) => ();

	($name:ident, $part:ident { $($defs:tt)* } $($rest:tt)*) => (
		sized!($name => $part; $($defs)*);
		sized!($name, $($rest)*);
	);

	($name:ident => $part:ident; min: $value:expr, $($rest:tt)*) => (
		impl<B> crate::size::$part::Min for $name<B> {
			fn min() -> usize {
				$value
			}
		}

		sized!($name => $part; $($rest)*);
	);

	($name:ident => $part:ident; max: $value:expr, $($rest:tt)*) => (
		impl<B> crate::size::$part::Max for $name<B> {
			fn max() -> usize {
				$value
			}
		}

		sized!($name => $part; $($rest)*);
	);

	($name:ident => $part:ident; size: $value:expr, $($rest:tt)*) => (
		impl<B: AsRef<[u8]>> crate::size::$part::Size for $name<B> {
			fn size(&self) -> usize {
				$value
			}
		}

		sized!($name => $part; $($rest)*);
	);

	($name:ident => $part:ident; size: $packet:ident => $value:expr, $($rest:tt)*) => (
		impl<B: AsRef<[u8]>> crate::size::$part::Size for $name<B> {
			fn size(&self) -> usize {
				let $packet = self;
				$value
			}
		}

		sized!($name => $part; $($rest)*);
	);
}