op_primitives/components/
rollup.rs1use std::fmt::Display;
2
3use enum_variants_strings::EnumVariantsStrings;
4use serde::{Deserialize, Serialize};
5use strum::EnumIter;
6
7#[derive(
12 Default, Copy, Clone, PartialEq, EnumVariantsStrings, Deserialize, Serialize, EnumIter,
13)]
14#[serde(rename_all = "kebab-case")]
15#[enum_variants_strings_transform(transform = "kebab_case")]
16pub enum RollupClient {
17 #[default]
19 OpNode,
20 Magi,
22}
23
24impl std::fmt::Debug for RollupClient {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 write!(f, "{}", self.to_str())
27 }
28}
29
30impl std::str::FromStr for RollupClient {
31 type Err = eyre::Report;
32
33 fn from_str(s: &str) -> Result<Self, Self::Err> {
34 if s == RollupClient::OpNode.to_str() {
35 return Ok(RollupClient::OpNode);
36 }
37 if s == RollupClient::Magi.to_str() {
38 return Ok(RollupClient::Magi);
39 }
40 eyre::bail!("Invalid rollup client: {}", s)
41 }
42}
43
44impl Display for RollupClient {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 write!(f, "{}", self.to_str())
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_deserialize_string() {
56 assert_eq!(
57 serde_json::from_str::<RollupClient>(r#""op-node""#).unwrap(),
58 RollupClient::OpNode
59 );
60 assert_eq!(
61 serde_json::from_str::<RollupClient>(r#""magi""#).unwrap(),
62 RollupClient::Magi
63 );
64 }
65
66 #[test]
67 fn test_debug_string() {
68 assert_eq!(format!("{:?}", RollupClient::OpNode), "op-node");
69 assert_eq!(format!("{:?}", RollupClient::Magi), "magi");
70 }
71
72 #[test]
73 fn test_rollup_client_from_str() {
74 assert_eq!(
75 RollupClient::from_str("op-node").unwrap(),
76 RollupClient::OpNode
77 );
78 assert_eq!(RollupClient::from_str("magi").unwrap(), RollupClient::Magi);
79 assert!(RollupClient::from_str("invalid").is_err());
80 }
81
82 #[test]
83 fn test_rollup_client_to_str() {
84 assert_eq!(RollupClient::OpNode.to_str(), "op-node");
85 assert_eq!(RollupClient::Magi.to_str(), "magi");
86 }
87}