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
use std::fmt::Display;
use std::fmt::Formatter;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/*

*** Action Type Options ***

swap
addLiquidity
withdraw
donate
refund
switch

*/

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum ActionType {
	#[serde(alias = "swap", alias = "SWAP")]
	Swap,

	#[serde(alias = "addLiquidity", alias = "ADDLIQUIDITY")]
	AddLiquidity,

	#[serde(alias = "withdraw", alias = "WITHDRAW")]
	Withdraw,

	#[serde(alias = "donate", alias = "DONATE")]
	Donate,

	#[serde(alias = "refund", alias = "REFUND")]
	Refund,

	#[serde(alias = "switch", alias = "SWITCH")]
	Switch,
}

impl Display for ActionType {
	fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
		match *self {
			Self::Swap => write!(f, "swap"),
			Self::AddLiquidity => write!(f, "addLiquidity"),
			Self::Withdraw => write!(f, "withdraw"),
			Self::Donate => write!(f, "donate"),
			Self::Refund => write!(f, "refund"),
			Self::Switch => write!(f, "switch"),
		}
	}
}

impl FromStr for ActionType {
	type Err = ();

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		match s {
			"swap" => Ok(Self::Swap),
			"addLiquidity" => Ok(Self::AddLiquidity),
			"withdraw" => Ok(Self::Withdraw),
			"donate" => Ok(Self::Donate),
			"refund" => Ok(Self::Refund),
			"switch" => Ok(Self::Switch),
			_ => Err(()),
		}
	}
}

impl Default for ActionType {
	fn default() -> Self {
		Self::Swap
	}
}

#[cfg(test)]
mod tests {
	use std::str::FromStr;

	use serde_json::json;

	use super::*;

	#[test]
	fn test_serialize_action_type() {
		let json = json!(ActionType::Swap);
		assert_eq!(json.to_string(), "\"swap\"");
	}
}