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
use crate::{
	connection::Buffer,
	models::{BaseMessage, Value},
	protocol::json_protocol,
};

use std::{
	collections::HashMap,
	time::{SystemTime, UNIX_EPOCH},
};

pub enum BaseProtocol {
	JsonProtocol { module_id: String },
	MsgPackProtocol { module_id: String },
}

impl BaseProtocol {
	pub fn default() -> Self {
		json_protocol::default()
	}

	pub fn from(other: &Self) -> Self {
		match other {
			BaseProtocol::JsonProtocol { .. } => json_protocol::from(other),
			_ => panic!("Currently, only JsonProtocol is supported"),
		}
	}

	pub fn generate_request_id(&self) -> String {
		format!(
			"{}-{}",
			self.get_module_id(),
			SystemTime::now()
				.duration_since(UNIX_EPOCH)
				.expect("Time went backwards. Wtf?")
				.as_nanos()
		)
	}

	pub fn get_module_id(&self) -> &String {
		match self {
			BaseProtocol::JsonProtocol { module_id } => module_id,
			_ => panic!("Currently, only JsonProtocol is supported"),
		}
	}

	pub fn set_module_id(&mut self, new_module_id: String) {
		match self {
			BaseProtocol::JsonProtocol { ref mut module_id } => {
				*module_id = new_module_id;
			}
			_ => panic!("Currently, only JsonProtocol is supported"),
		}
	}

	pub fn initialize(
		&mut self,
		module_id: String,
		version: String,
		dependencies: HashMap<String, String>,
	) -> BaseMessage {
		self.set_module_id(module_id);
		BaseMessage::RegisterModuleRequest {
			request_id: self.generate_request_id(),
			module_id: self.get_module_id().clone(),
			version,
			dependencies,
		}
	}

	pub fn register_hook(&self, hook: String) -> BaseMessage {
		BaseMessage::RegisterHookRequest {
			request_id: self.generate_request_id(),
			hook,
		}
	}

	pub fn trigger_hook(&self, hook: String) -> BaseMessage {
		BaseMessage::TriggerHookRequest {
			request_id: self.generate_request_id(),
			hook,
		}
	}

	pub fn declare_function(&self, function: String) -> BaseMessage {
		BaseMessage::DeclareFunctionRequest {
			request_id: self.generate_request_id(),
			function,
		}
	}

	pub fn call_function(
		&self,
		function: String,
		arguments: HashMap<String, Value>,
	) -> BaseMessage {
		BaseMessage::FunctionCallRequest {
			request_id: self.generate_request_id(),
			function,
			arguments,
		}
	}

	pub fn encode(&self, req: BaseMessage) -> Buffer {
		match self {
			BaseProtocol::JsonProtocol { .. } => json_protocol::encode(&self, req),
			_ => panic!("Currently, only JsonProtocol is supported"),
		}
	}

	pub fn decode(&self, data: &[u8]) -> BaseMessage {
		match self {
			BaseProtocol::JsonProtocol { .. } => json_protocol::decode(&self, data),
			_ => panic!("Currently, only JsonProtocol is supported"),
		}
	}
}