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


use crate::prelude::*;




pub type ServerError = io::Error;
pub type ServerResult<V = ()> = Result<V, ServerError>;




pub(crate) trait ResultExtPanic<V, E : Error> : Sized {
	
	fn result (self) -> Result<V, E>;
	
	fn or_panic (self, _code : u32) -> V {
		match self.result () {
			Ok (_value) =>
				_value,
			Err (_error) =>
				_error.panic (_code),
		}
	}
}


impl <V, E : Error> ResultExtPanic<V, E> for Result<V, E> {
	
	fn result (self) -> Self {
		self
	}
}


impl <V> ResultExtPanic<V, io::Error> for Result<V, ()> {
	
	fn result (self) -> Result<V, io::Error> {
		self.map_err (|_| io::Error::new (io::ErrorKind::Other, "unspecified error"))
	}
}




pub(crate) trait ErrorExtPanic<E : Error> : Sized {
	
	fn error (self) -> E;
	
	fn panic (self, _code : u32) -> ! {
		panic! ("[{:08x}]  unexpected error encountered!  //  {}", _code, self.error ());
	}
}


impl <E : Error> ErrorExtPanic<E> for E {
	
	fn error (self) -> Self {
		self
	}
}




pub(crate) trait ResultExtWrap<V> : Sized {
	
	fn or_wrap (self, _code : u32) -> ServerResult<V>;
}


impl <V, E : Error> ResultExtWrap<V> for Result<V, E> {
	
	fn or_wrap (self, _code : u32) -> ServerResult<V> {
		match self {
			Ok (_value) =>
				Ok (_value),
			Err (_error) =>
				Err (_error.wrap (_code)),
		}
	}
}


pub(crate) trait ErrorExtWrap : Sized {
	
	fn wrap (self, _code : u32) -> ServerError;
}

impl <E : Error> ErrorExtWrap for E {
	
	fn wrap (self, _code : u32) -> ServerError {
		io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  {}", _code, self))
	}
}




#[ allow (dead_code) ]
pub(crate) fn error_with_format (_code : u32, _message : fmt::Arguments<'_>) -> ServerError {
	io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  {}", _code, _message))
}

#[ allow (dead_code) ]
pub(crate) fn error_with_message (_code : u32, _message : &str) -> ServerError {
	if ! _message.is_empty () {
		io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  {}", _code, _message))
	} else {
		error_with_code (_code)
	}
}

#[ allow (dead_code) ]
pub(crate) fn error_with_code (_code : u32) -> ServerError {
	io::Error::new (io::ErrorKind::Other, format! ("[{:08x}]  unexpected error encountered!", _code))
}




#[ allow (dead_code) ]
pub(crate) fn panic_with_format (_code : u32, _message : fmt::Arguments<'_>) -> ! {
	panic! (format! ("[{:08x}]  {}", _code, _message))
}

#[ allow (dead_code) ]
pub(crate) fn panic_with_message (_code : u32, _message : &str) -> ! {
	if ! _message.is_empty () {
		panic! (format! ("[{:08x}]  {}", _code, _message))
	} else {
		panic_with_code (_code)
	}
}

#[ allow (dead_code) ]
pub(crate) fn panic_with_code (_code : u32) -> ! {
	panic! (format! ("[{:08x}]  unexpected error encountered!", _code))
}