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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use client;
use serde_json;
use std::fmt;



#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Errcode {
	// UNKNOWN_ERROR
	UnknownError,

	// 1xxx errors
	ApiVersionNotFound,
	ApiVersionDeprecated,
	ApiClassNotFound,
	ApiFunctionNotFound,
	NoApiPermissions,
	InvalidToken,
	ApiFunctionDisabled,
	ProxerMaintenance,
	ApiMaintenance,

	// 2xxx errors
	IpBlocked,
	NewsError,

	// 3xxx errors
	MissingLoginCredentials,
	InvalidLoginCredentials,
	InvalidId,
	InvalidUid,
	UserNotLoggedIn,
	UserNotFound,
	UserAlreadyLoggedIn,
}


impl Errcode {
	pub fn from_code(code: i64) -> Self
	{

		match code
		{
			1000 => Errcode::ApiVersionNotFound,
			1001 => Errcode::ApiVersionDeprecated,
			1002 => Errcode::ApiClassNotFound,
			1003 => Errcode::ApiFunctionNotFound,
			1004 => Errcode::NoApiPermissions,
			1005 => Errcode::InvalidToken,
			1006 => Errcode::ApiFunctionDisabled,
			1007 => Errcode::ProxerMaintenance,
			1008 => Errcode::ApiMaintenance,

			2000 => Errcode::IpBlocked,
			2001 => Errcode::NewsError,

			3000 => Errcode::MissingLoginCredentials,
			3001 => Errcode::InvalidLoginCredentials,
			3002 => Errcode::UserNotLoggedIn,
			3003 => Errcode::UserNotFound,
			3004 => Errcode::UserNotLoggedIn,
			3007 => Errcode::InvalidId,
			3009 => Errcode::UserNotLoggedIn,
			3012 => Errcode::UserAlreadyLoggedIn,
			3023 => Errcode::UserNotLoggedIn,
			3034 => Errcode::UserNotLoggedIn,

			_ => Errcode::UnknownError,
		}
	}
}





impl From<i64> for Errcode {
	fn from(code: i64) -> Errcode
	{
		Errcode::from_code(code)
	}
}



impl fmt::Display for Errcode {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
	{
		write!(f, "{:?}", self)
	}
}










#[derive(Debug, Clone)]
pub struct Api {
	code: i64,
	message: String,
	error: Errcode,
}

impl Api {
	pub fn new(code: i64, msg: String) -> Self
	{
		Api {
			code: code,
			message: msg,
			error: Errcode::from(code),
		}
	}

	pub fn error(&self) -> Errcode
	{
		self.error
	}
}


impl From<serde_json::Value> for Api {
	fn from(json: serde_json::Value) -> Self
	{

		let error = json.get("code").unwrap().as_i64().unwrap();
		let msg = json.get("message").unwrap().as_str().unwrap();


		Self {
			error: Errcode::from_code(error),
			code: error,
			message: msg.to_string(),
		}
	}
}


impl<T> From<client::ApiResponse<T>> for Api {
	fn from(res: client::ApiResponse<T>) -> Self
	{
		Self {
			code: res.code.unwrap(),
			error: Errcode::from_code(res.code.unwrap()),
			message: res.message.to_string(),
		}
	}
}



impl fmt::Display for Api {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
	{
		write!(f, "{}", Errcode::from(self.code))
	}
}