1use std::{collections::HashSet, fmt, str::FromStr};
2
3use serde::{Deserialize, Serialize, Serializer};
4use strum::{ParseError, VariantNames};
5
6#[derive(Debug, Default, Clone, Eq, PartialEq)]
17pub enum RpcModuleSelection {
18 All,
20 #[default]
22 Standard,
23 Selection(HashSet<RethRpcModule>),
25}
26
27impl RpcModuleSelection {
30 pub const STANDARD_MODULES: [RethRpcModule; 3] =
32 [RethRpcModule::Eth, RethRpcModule::Net, RethRpcModule::Web3];
33
34 pub fn all_modules() -> HashSet<RethRpcModule> {
36 Self::all_modules_iter().collect()
37 }
38
39 pub fn standard_modules() -> HashSet<RethRpcModule> {
41 HashSet::from(Self::STANDARD_MODULES)
42 }
43
44 pub fn default_ipc_modules() -> HashSet<RethRpcModule> {
46 Self::all_modules()
47 }
48
49 pub fn try_from_selection<I, T>(selection: I) -> Result<Self, T::Error>
75 where
76 I: IntoIterator<Item = T>,
77 T: TryInto<RethRpcModule>,
78 {
79 selection.into_iter().map(TryInto::try_into).collect()
80 }
81
82 pub fn len(&self) -> usize {
84 match self {
85 Self::All => Self::all_modules_iter().count(),
86 Self::Standard => Self::STANDARD_MODULES.len(),
87 Self::Selection(s) => s.len(),
88 }
89 }
90
91 pub fn is_empty(&self) -> bool {
93 match self {
94 Self::Selection(sel) => sel.is_empty(),
95 _ => false,
96 }
97 }
98
99 pub const fn is_all(&self) -> bool {
101 matches!(self, Self::All)
102 }
103
104 pub fn iter_selection(&self) -> Box<dyn Iterator<Item = RethRpcModule> + '_> {
106 match self {
107 Self::All => Box::new(Self::all_modules_iter()),
108 Self::Standard => Box::new(Self::STANDARD_MODULES.iter().cloned()),
109 Self::Selection(s) => Box::new(s.iter().cloned()),
110 }
111 }
112
113 pub fn to_selection(&self) -> HashSet<RethRpcModule> {
115 match self {
116 Self::All => Self::all_modules(),
117 Self::Standard => Self::standard_modules(),
118 Self::Selection(s) => s.clone(),
119 }
120 }
121
122 pub fn into_selection(self) -> HashSet<RethRpcModule> {
124 match self {
125 Self::All => Self::all_modules(),
126 Self::Standard => Self::standard_modules(),
127 Self::Selection(s) => s,
128 }
129 }
130
131 pub fn are_identical(http: Option<&Self>, ws: Option<&Self>) -> bool {
133 match (http, ws) {
134 (Some(some), None) | (None, Some(some)) => some.is_empty(),
136
137 (Some(http), Some(ws)) => http.to_selection() == ws.to_selection(),
138 (None, None) => true,
139 }
140 }
141
142 pub fn contains(&self, module: &RethRpcModule) -> bool {
144 match self {
145 Self::All => !matches!(module, RethRpcModule::Testing),
146 Self::Standard => Self::STANDARD_MODULES.contains(module),
147 Self::Selection(s) => s.contains(module),
148 }
149 }
150
151 pub fn push(&mut self, module: RethRpcModule) {
156 if !self.is_all() {
157 let mut modules = self.to_selection();
158 modules.insert(module);
159 *self = Self::Selection(modules);
160 }
161 }
162
163 pub fn append(self, module: RethRpcModule) -> Self {
168 if self.is_all() {
169 Self::All
170 } else {
171 let mut modules = self.into_selection();
172 modules.insert(module);
173 Self::Selection(modules)
174 }
175 }
176
177 pub fn extend<I>(&mut self, iter: I)
182 where
183 I: IntoIterator<Item = RethRpcModule>,
184 {
185 if !self.is_all() {
186 let mut modules = self.to_selection();
187 modules.extend(iter);
188 *self = Self::Selection(modules);
189 }
190 }
191
192 pub fn extended<I>(self, iter: I) -> Self
197 where
198 I: IntoIterator<Item = RethRpcModule>,
199 {
200 if self.is_all() {
201 Self::All
202 } else {
203 let mut modules = self.into_selection();
204 modules.extend(iter);
205 Self::Selection(modules)
206 }
207 }
208
209 fn all_modules_iter() -> impl Iterator<Item = RethRpcModule> {
210 RethRpcModule::modules().into_iter().filter(|module| *module != RethRpcModule::Testing)
211 }
212}
213
214impl From<&HashSet<RethRpcModule>> for RpcModuleSelection {
215 fn from(s: &HashSet<RethRpcModule>) -> Self {
216 Self::from(s.clone())
217 }
218}
219
220impl From<HashSet<RethRpcModule>> for RpcModuleSelection {
221 fn from(s: HashSet<RethRpcModule>) -> Self {
222 Self::Selection(s)
223 }
224}
225
226impl From<&[RethRpcModule]> for RpcModuleSelection {
227 fn from(s: &[RethRpcModule]) -> Self {
228 Self::Selection(s.iter().cloned().collect())
229 }
230}
231
232impl From<Vec<RethRpcModule>> for RpcModuleSelection {
233 fn from(s: Vec<RethRpcModule>) -> Self {
234 Self::Selection(s.into_iter().collect())
235 }
236}
237
238impl<const N: usize> From<[RethRpcModule; N]> for RpcModuleSelection {
239 fn from(s: [RethRpcModule; N]) -> Self {
240 Self::Selection(s.into_iter().collect())
241 }
242}
243
244impl<'a> FromIterator<&'a RethRpcModule> for RpcModuleSelection {
245 fn from_iter<I>(iter: I) -> Self
246 where
247 I: IntoIterator<Item = &'a RethRpcModule>,
248 {
249 iter.into_iter().cloned().collect()
250 }
251}
252
253impl FromIterator<RethRpcModule> for RpcModuleSelection {
254 fn from_iter<I>(iter: I) -> Self
255 where
256 I: IntoIterator<Item = RethRpcModule>,
257 {
258 Self::Selection(iter.into_iter().collect())
259 }
260}
261
262impl FromStr for RpcModuleSelection {
263 type Err = ParseError;
264
265 fn from_str(s: &str) -> Result<Self, Self::Err> {
266 if s.is_empty() {
267 return Ok(Self::Selection(Default::default()))
268 }
269 let modules = s.split(',').map(str::trim).filter(|m| !m.is_empty());
271 let is_keyword = |m: &str| m.eq_ignore_ascii_case("all") || m.eq_ignore_ascii_case("none");
277 if modules.clone().count() > 1 && modules.clone().any(is_keyword) {
278 return Err(ParseError::VariantNotFound)
279 }
280 let single = modules.clone().next().unwrap_or_default();
281 if single.eq_ignore_ascii_case("all") {
282 Ok(Self::All)
283 } else if single.eq_ignore_ascii_case("none") {
284 Ok(Self::Selection(Default::default()))
285 } else {
286 Self::try_from_selection(modules)
287 }
288 }
289}
290
291impl fmt::Display for RpcModuleSelection {
292 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
293 write!(
294 f,
295 "[{}]",
296 self.iter_selection().map(|s| s.to_string()).collect::<Vec<_>>().join(", ")
297 )
298 }
299}
300
301#[derive(Debug, Clone, Eq, PartialEq, Hash, VariantNames, Deserialize)]
303#[serde(rename_all = "snake_case")]
304#[strum(serialize_all = "kebab-case")]
305pub enum RethRpcModule {
306 Admin,
308 Debug,
310 Eth,
312 Net,
314 Trace,
316 Txpool,
318 Web3,
320 Rpc,
322 Reth,
324 Ots,
326 Flashbots,
328 Miner,
330 Mev,
332 Testing,
334 #[strum(default)]
336 #[serde(untagged)]
337 Other(String),
338}
339
340impl RethRpcModule {
343 const STANDARD_VARIANTS: &'static [Self] = &[
345 Self::Admin,
346 Self::Debug,
347 Self::Eth,
348 Self::Net,
349 Self::Trace,
350 Self::Txpool,
351 Self::Web3,
352 Self::Rpc,
353 Self::Reth,
354 Self::Ots,
355 Self::Flashbots,
356 Self::Miner,
357 Self::Mev,
358 Self::Testing,
359 ];
360
361 pub const fn variant_count() -> usize {
363 Self::STANDARD_VARIANTS.len()
364 }
365
366 pub const fn all_variant_names() -> &'static [&'static str] {
368 <Self as VariantNames>::VARIANTS
369 }
370
371 pub fn standard_variant_names() -> impl Iterator<Item = &'static str> {
373 <Self as VariantNames>::VARIANTS.iter().copied().filter(|&name| name != "other")
374 }
375
376 pub const fn all_variants() -> &'static [Self] {
378 Self::STANDARD_VARIANTS
379 }
380
381 pub fn modules() -> impl IntoIterator<Item = Self> + Clone {
383 Self::STANDARD_VARIANTS.iter().cloned()
384 }
385
386 pub fn as_str(&self) -> &str {
388 match self {
389 Self::Other(s) => s.as_str(),
390 _ => self.as_ref(), }
392 }
393
394 pub const fn is_other(&self) -> bool {
396 matches!(self, Self::Other(_))
397 }
398}
399
400impl AsRef<str> for RethRpcModule {
401 fn as_ref(&self) -> &str {
402 match self {
403 Self::Other(s) => s.as_str(),
404 Self::Admin => "admin",
406 Self::Debug => "debug",
407 Self::Eth => "eth",
408 Self::Net => "net",
409 Self::Trace => "trace",
410 Self::Txpool => "txpool",
411 Self::Web3 => "web3",
412 Self::Rpc => "rpc",
413 Self::Reth => "reth",
414 Self::Ots => "ots",
415 Self::Flashbots => "flashbots",
416 Self::Miner => "miner",
417 Self::Mev => "mev",
418 Self::Testing => "testing",
419 }
420 }
421}
422
423impl FromStr for RethRpcModule {
424 type Err = ParseError;
425
426 fn from_str(s: &str) -> Result<Self, Self::Err> {
427 Ok(match s {
428 "admin" => Self::Admin,
429 "debug" => Self::Debug,
430 "eth" => Self::Eth,
431 "net" => Self::Net,
432 "trace" => Self::Trace,
433 "txpool" => Self::Txpool,
434 "web3" => Self::Web3,
435 "rpc" => Self::Rpc,
436 "reth" => Self::Reth,
437 "ots" => Self::Ots,
438 "flashbots" => Self::Flashbots,
439 "miner" => Self::Miner,
440 "mev" => Self::Mev,
441 "testing" => Self::Testing,
442 other => Self::Other(other.to_string()),
444 })
445 }
446}
447
448impl TryFrom<&str> for RethRpcModule {
449 type Error = ParseError;
450 fn try_from(s: &str) -> Result<Self, <Self as TryFrom<&str>>::Error> {
451 FromStr::from_str(s)
452 }
453}
454
455impl fmt::Display for RethRpcModule {
456 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
457 f.pad(self.as_ref())
458 }
459}
460
461impl Serialize for RethRpcModule {
462 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
463 where
464 S: Serializer,
465 {
466 s.serialize_str(self.as_str())
467 }
468}
469
470pub trait RpcModuleValidator: Clone + Send + Sync + 'static {
475 fn parse_selection(s: &str) -> Result<RpcModuleSelection, String>;
477
478 fn validate_selection(modules: &RpcModuleSelection, arg_name: &str) -> Result<(), String> {
483 let RpcModuleSelection::Selection(module_set) = modules else {
487 return Ok(());
489 };
490
491 for module in module_set {
492 let RethRpcModule::Other(name) = module else {
493 continue;
495 };
496
497 let selection = Self::parse_selection(name)
502 .map_err(|e| format!("Invalid RPC module '{name}' in {arg_name}: {e}"))?;
503 if !matches!(&selection, RpcModuleSelection::Selection(s) if s.len() == 1 && s.contains(module))
504 {
505 return Err(format!("Invalid RPC module '{name}' in {arg_name}: not a module name"))
506 }
507 }
508
509 Ok(())
510 }
511}
512
513#[derive(Debug, Clone, Copy)]
517pub struct DefaultRpcModuleValidator;
518
519impl RpcModuleValidator for DefaultRpcModuleValidator {
520 fn parse_selection(s: &str) -> Result<RpcModuleSelection, String> {
521 let selection = RpcModuleSelection::from_str(s)
523 .map_err(|e| format!("Failed to parse RPC modules: {}", e))?;
524
525 if let RpcModuleSelection::Selection(modules) = &selection {
527 for module in modules {
528 if let RethRpcModule::Other(name) = module {
529 return Err(format!("Unknown RPC module: '{}'", name));
530 }
531 }
532 }
533
534 Ok(selection)
535 }
536}
537
538#[derive(Debug, Clone, Copy)]
542pub struct LenientRpcModuleValidator;
543
544impl RpcModuleValidator for LenientRpcModuleValidator {
545 fn parse_selection(s: &str) -> Result<RpcModuleSelection, String> {
546 RpcModuleSelection::from_str(s).map_err(|e| format!("Failed to parse RPC modules: {}", e))
547 }
548}
549
550#[cfg(test)]
551mod test {
552 use super::*;
553
554 #[test]
555 fn test_all_modules() {
556 let all_modules = RpcModuleSelection::all_modules();
557 assert_eq!(all_modules.len(), RethRpcModule::variant_count() - 1);
558 assert!(!all_modules.contains(&RethRpcModule::Testing));
559 }
560
561 #[test]
562 fn test_testing_requires_explicit_selection() {
563 let all = RpcModuleSelection::All;
564 let explicit = RpcModuleSelection::from([RethRpcModule::Testing]);
565
566 assert!(!all.contains(&RethRpcModule::Testing));
567 assert!(!all.to_selection().contains(&RethRpcModule::Testing));
568 assert!(!all.into_selection().contains(&RethRpcModule::Testing));
569 assert!(explicit.contains(&RethRpcModule::Testing));
570 assert!(explicit.iter_selection().any(|module| module == RethRpcModule::Testing));
571 }
572
573 #[test]
574 fn test_standard_modules() {
575 let standard_modules = RpcModuleSelection::standard_modules();
576 let expected_modules: HashSet<RethRpcModule> =
577 HashSet::from([RethRpcModule::Eth, RethRpcModule::Net, RethRpcModule::Web3]);
578 assert_eq!(standard_modules, expected_modules);
579 }
580
581 #[test]
582 fn test_default_ipc_modules() {
583 let default_ipc_modules = RpcModuleSelection::default_ipc_modules();
584 assert_eq!(default_ipc_modules, RpcModuleSelection::all_modules());
585 assert!(!default_ipc_modules.contains(&RethRpcModule::Testing));
586 }
587
588 #[test]
589 fn test_try_from_selection_success() {
590 let selection = vec!["eth", "admin"];
591 let config = RpcModuleSelection::try_from_selection(selection).unwrap();
592 assert_eq!(config, RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Admin]));
593 }
594
595 #[test]
596 fn test_rpc_module_selection_len() {
597 let all_modules = RpcModuleSelection::All;
598 let standard = RpcModuleSelection::Standard;
599 let selection = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Admin]);
600
601 assert_eq!(all_modules.len(), RethRpcModule::variant_count() - 1);
602 assert_eq!(standard.len(), 3);
603 assert_eq!(selection.len(), 2);
604 }
605
606 #[test]
607 fn test_rpc_module_selection_is_empty() {
608 let empty_selection = RpcModuleSelection::from(HashSet::new());
609 assert!(empty_selection.is_empty());
610
611 let non_empty_selection = RpcModuleSelection::from([RethRpcModule::Eth]);
612 assert!(!non_empty_selection.is_empty());
613 }
614
615 #[test]
616 fn test_rpc_module_selection_iter_selection() {
617 let all_modules = RpcModuleSelection::All;
618 let standard = RpcModuleSelection::Standard;
619 let selection = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Admin]);
620
621 assert_eq!(all_modules.iter_selection().count(), RethRpcModule::variant_count() - 1);
622 assert!(!all_modules.iter_selection().any(|module| module == RethRpcModule::Testing));
623 assert_eq!(standard.iter_selection().count(), 3);
624 assert_eq!(selection.iter_selection().count(), 2);
625 }
626
627 #[test]
628 fn test_rpc_module_selection_to_selection() {
629 let all_modules = RpcModuleSelection::All;
630 let standard = RpcModuleSelection::Standard;
631 let selection = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Admin]);
632
633 assert_eq!(all_modules.to_selection(), RpcModuleSelection::all_modules());
634 assert_eq!(standard.to_selection(), RpcModuleSelection::standard_modules());
635 assert_eq!(
636 selection.to_selection(),
637 HashSet::from([RethRpcModule::Eth, RethRpcModule::Admin])
638 );
639 }
640
641 #[test]
642 fn test_rpc_module_selection_are_identical() {
643 let all_modules = RpcModuleSelection::All;
647 assert!(RpcModuleSelection::are_identical(Some(&all_modules), Some(&all_modules)));
648
649 assert!(RpcModuleSelection::are_identical(None, None));
654
655 let selection1 = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Admin]);
660 let selection2 = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Admin]);
661 assert!(RpcModuleSelection::are_identical(Some(&selection1), Some(&selection2)));
662
663 let standard = RpcModuleSelection::Standard;
669 assert!(!RpcModuleSelection::are_identical(Some(&all_modules), Some(&standard)));
670
671 let empty_selection = RpcModuleSelection::Selection(HashSet::new());
676 assert!(RpcModuleSelection::are_identical(None, Some(&empty_selection)));
677 assert!(RpcModuleSelection::are_identical(Some(&empty_selection), None));
678
679 let non_empty_selection = RpcModuleSelection::from([RethRpcModule::Eth]);
685 assert!(!RpcModuleSelection::are_identical(None, Some(&non_empty_selection)));
686 assert!(!RpcModuleSelection::are_identical(Some(&non_empty_selection), None));
687
688 let partial_selection = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Net]);
693 assert!(!RpcModuleSelection::are_identical(Some(&all_modules), Some(&partial_selection)));
694
695 let full_selection = RpcModuleSelection::from(RpcModuleSelection::all_modules());
699 assert!(RpcModuleSelection::are_identical(Some(&all_modules), Some(&full_selection)));
700
701 let mut testing_selection = RpcModuleSelection::all_modules();
703 testing_selection.insert(RethRpcModule::Testing);
704 let testing_selection = RpcModuleSelection::from(testing_selection);
705 assert!(!RpcModuleSelection::are_identical(Some(&all_modules), Some(&testing_selection),));
706
707 let selection3 = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Net]);
712 let selection4 = RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Web3]);
713 assert!(!RpcModuleSelection::are_identical(Some(&selection3), Some(&selection4)));
714
715 let matching_standard =
719 RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Net, RethRpcModule::Web3]);
720 assert!(RpcModuleSelection::are_identical(Some(&standard), Some(&matching_standard)));
721
722 let non_matching_standard =
727 RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Net]);
728 assert!(!RpcModuleSelection::are_identical(Some(&standard), Some(&non_matching_standard)));
729 }
730
731 #[test]
732 fn test_rpc_module_selection_append() {
733 let selection = RpcModuleSelection::Standard;
735 let new_selection = selection.append(RethRpcModule::Admin);
736 assert!(new_selection.contains(&RethRpcModule::Eth));
737 assert!(new_selection.contains(&RethRpcModule::Net));
738 assert!(new_selection.contains(&RethRpcModule::Web3));
739 assert!(new_selection.contains(&RethRpcModule::Admin));
740
741 let selection = RpcModuleSelection::Selection(HashSet::new());
743 let new_selection = selection.append(RethRpcModule::Eth);
744 assert!(new_selection.contains(&RethRpcModule::Eth));
745 assert_eq!(new_selection.len(), 1);
746
747 let selection = RpcModuleSelection::All;
749 let new_selection = selection.append(RethRpcModule::Eth);
750 assert_eq!(new_selection, RpcModuleSelection::All);
751 }
752
753 #[test]
754 fn test_rpc_module_selection_extend() {
755 let mut selection = RpcModuleSelection::Standard;
757 selection.extend(vec![RethRpcModule::Admin, RethRpcModule::Debug]);
758 assert!(selection.contains(&RethRpcModule::Eth));
759 assert!(selection.contains(&RethRpcModule::Net));
760 assert!(selection.contains(&RethRpcModule::Web3));
761 assert!(selection.contains(&RethRpcModule::Admin));
762 assert!(selection.contains(&RethRpcModule::Debug));
763
764 let mut selection = RpcModuleSelection::Selection(HashSet::new());
766 selection.extend(vec![RethRpcModule::Eth, RethRpcModule::Admin]);
767 assert!(selection.contains(&RethRpcModule::Eth));
768 assert!(selection.contains(&RethRpcModule::Admin));
769 assert_eq!(selection.len(), 2);
770
771 let mut selection = RpcModuleSelection::All;
773 selection.extend(vec![RethRpcModule::Eth, RethRpcModule::Admin]);
774 assert_eq!(selection, RpcModuleSelection::All);
775 }
776
777 #[test]
778 fn test_rpc_module_selection_from_str() {
779 let result = RpcModuleSelection::from_str("");
781 assert!(result.is_ok());
782 assert_eq!(result.unwrap(), RpcModuleSelection::Selection(Default::default()));
783
784 let result = RpcModuleSelection::from_str("all");
786 assert!(result.is_ok());
787 assert_eq!(result.unwrap(), RpcModuleSelection::All);
788
789 let result = RpcModuleSelection::from_str("All");
790 assert!(result.is_ok());
791 assert_eq!(result.unwrap(), RpcModuleSelection::All);
792
793 let result = RpcModuleSelection::from_str("ALL");
794 assert!(result.is_ok());
795 assert_eq!(result.unwrap(), RpcModuleSelection::All);
796
797 let result = RpcModuleSelection::from_str("none");
799 assert!(result.is_ok());
800 assert_eq!(result.unwrap(), RpcModuleSelection::Selection(Default::default()));
801
802 let result = RpcModuleSelection::from_str("None");
803 assert!(result.is_ok());
804 assert_eq!(result.unwrap(), RpcModuleSelection::Selection(Default::default()));
805
806 let result = RpcModuleSelection::from_str("NONE");
807 assert!(result.is_ok());
808 assert_eq!(result.unwrap(), RpcModuleSelection::Selection(Default::default()));
809
810 for s in ["all,testing", "none,eth", "eth,all", "eth, NONE", "all,all", "eth,all,admin"] {
812 assert!(RpcModuleSelection::from_str(s).is_err(), "{s}");
813 assert!(DefaultRpcModuleValidator::parse_selection(s).is_err(), "{s}");
814 }
815 assert_eq!(RpcModuleSelection::from_str("all,").unwrap(), RpcModuleSelection::All);
817 assert!(RpcModuleSelection::from_str(",none").unwrap().is_empty());
818 for s in ["eth,", "eth,,admin"] {
819 let selection = DefaultRpcModuleValidator::parse_selection(s).unwrap();
820 assert!(DefaultRpcModuleValidator::validate_selection(&selection, "http.api").is_ok());
821 }
822
823 let bracketed = RpcModuleSelection::from([RethRpcModule::Other("[eth".to_string())]);
825 assert!(DefaultRpcModuleValidator::validate_selection(&bracketed, "http.api").is_err());
826
827 for name in ["all", "NONE", "eth,eth", " eth ", "eth\t"] {
828 let selection = RpcModuleSelection::from([
829 RethRpcModule::Eth,
830 RethRpcModule::Other(name.to_string()),
831 ]);
832 assert!(DefaultRpcModuleValidator::validate_selection(&selection, "http.api").is_err());
833 assert!(LenientRpcModuleValidator::validate_selection(&selection, "http.api").is_err());
834 }
835
836 let result = RpcModuleSelection::from_str("eth,admin");
838 assert!(result.is_ok());
839 let expected_selection =
840 RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Admin]);
841 assert_eq!(result.unwrap(), expected_selection);
842
843 let result = RpcModuleSelection::from_str(" eth , admin ");
845 assert!(result.is_ok());
846 assert_eq!(result.unwrap(), expected_selection);
847
848 let result = RpcModuleSelection::from_str("invalid,unknown");
850 assert!(result.is_ok());
851 let selection = result.unwrap();
852 assert!(selection.contains(&RethRpcModule::Other("invalid".to_string())));
853 assert!(selection.contains(&RethRpcModule::Other("unknown".to_string())));
854
855 let result = RpcModuleSelection::from_str("eth");
857 assert!(result.is_ok());
858 let expected_selection = RpcModuleSelection::from([RethRpcModule::Eth]);
859 assert_eq!(result.unwrap(), expected_selection);
860
861 let result = RpcModuleSelection::from_str("unknown");
863 assert!(result.is_ok());
864 let expected_selection =
865 RpcModuleSelection::from([RethRpcModule::Other("unknown".to_string())]);
866 assert_eq!(result.unwrap(), expected_selection);
867 }
868
869 #[test]
870 fn test_rpc_module_other_variant() {
871 let custom_module = RethRpcModule::from_str("myCustomModule").unwrap();
873 assert_eq!(custom_module, RethRpcModule::Other("myCustomModule".to_string()));
874
875 assert_eq!(custom_module.as_str(), "myCustomModule");
877
878 assert_eq!(custom_module.as_ref(), "myCustomModule");
880
881 assert_eq!(custom_module.to_string(), "myCustomModule");
883 }
884
885 #[test]
886 fn test_rpc_module_selection_with_mixed_modules() {
887 let result = RpcModuleSelection::from_str("eth,admin,myCustomModule,anotherCustom");
889 assert!(result.is_ok());
890
891 let selection = result.unwrap();
892 assert!(selection.contains(&RethRpcModule::Eth));
893 assert!(selection.contains(&RethRpcModule::Admin));
894 assert!(selection.contains(&RethRpcModule::Other("myCustomModule".to_string())));
895 assert!(selection.contains(&RethRpcModule::Other("anotherCustom".to_string())));
896 }
897
898 #[test]
899 fn test_rpc_module_all_excludes_custom() {
900 let all_selection = RpcModuleSelection::All;
902
903 assert!(all_selection.contains(&RethRpcModule::Eth));
905 assert!(all_selection.contains(&RethRpcModule::Admin));
906
907 assert_eq!(all_selection.len(), RethRpcModule::variant_count() - 1);
910 }
911
912 #[test]
913 fn test_rpc_module_equality_with_other() {
914 let other1 = RethRpcModule::Other("custom".to_string());
915 let other2 = RethRpcModule::Other("custom".to_string());
916 let other3 = RethRpcModule::Other("different".to_string());
917
918 assert_eq!(other1, other2);
919 assert_ne!(other1, other3);
920 assert_ne!(other1, RethRpcModule::Eth);
921 }
922
923 #[test]
924 fn test_rpc_module_is_other() {
925 assert!(!RethRpcModule::Eth.is_other());
927 assert!(!RethRpcModule::Admin.is_other());
928 assert!(!RethRpcModule::Debug.is_other());
929
930 assert!(RethRpcModule::Other("custom".to_string()).is_other());
932 assert!(RethRpcModule::Other("mycustomrpc".to_string()).is_other());
933 }
934
935 #[test]
936 fn test_standard_variant_names_excludes_other() {
937 let standard_names: Vec<_> = RethRpcModule::standard_variant_names().collect();
938
939 assert!(!standard_names.contains(&"other"));
941
942 assert_eq!(standard_names.len(), RethRpcModule::STANDARD_VARIANTS.len());
944
945 for variant in RethRpcModule::STANDARD_VARIANTS {
947 assert!(standard_names.contains(&variant.as_ref()));
948 }
949 }
950
951 #[test]
952 fn test_default_validator_accepts_standard_modules() {
953 let result = DefaultRpcModuleValidator::parse_selection("eth,admin,debug");
955 assert!(result.is_ok());
956
957 let selection = result.unwrap();
958 assert!(matches!(selection, RpcModuleSelection::Selection(_)));
959 }
960
961 #[test]
962 fn test_default_validator_rejects_unknown_modules() {
963 let result = DefaultRpcModuleValidator::parse_selection("eth,mycustom");
965 assert!(result.is_err());
966 assert!(result.unwrap_err().contains("Unknown RPC module: 'mycustom'"));
967
968 let result = DefaultRpcModuleValidator::parse_selection("unknownmodule");
969 assert!(result.is_err());
970 assert!(result.unwrap_err().contains("Unknown RPC module: 'unknownmodule'"));
971
972 let result = DefaultRpcModuleValidator::parse_selection("eth,admin,xyz123");
973 assert!(result.is_err());
974 assert!(result.unwrap_err().contains("Unknown RPC module: 'xyz123'"));
975 }
976
977 #[test]
978 fn test_default_validator_all_selection() {
979 let result = DefaultRpcModuleValidator::parse_selection("all");
981 assert!(result.is_ok());
982 assert_eq!(result.unwrap(), RpcModuleSelection::All);
983 }
984
985 #[test]
986 fn test_default_validator_none_selection() {
987 let result = DefaultRpcModuleValidator::parse_selection("none");
989 assert!(result.is_ok());
990 assert_eq!(result.unwrap(), RpcModuleSelection::Selection(Default::default()));
991 }
992
993 #[test]
994 fn test_lenient_validator_accepts_unknown_modules() {
995 let result = LenientRpcModuleValidator::parse_selection("eht,adimn,xyz123,customrpc");
997 assert!(result.is_ok());
998
999 let selection = result.unwrap();
1000 if let RpcModuleSelection::Selection(modules) = selection {
1001 assert!(modules.contains(&RethRpcModule::Other("eht".to_string())));
1002 assert!(modules.contains(&RethRpcModule::Other("adimn".to_string())));
1003 assert!(modules.contains(&RethRpcModule::Other("xyz123".to_string())));
1004 assert!(modules.contains(&RethRpcModule::Other("customrpc".to_string())));
1005 } else {
1006 panic!("Expected Selection variant");
1007 }
1008 }
1009
1010 #[test]
1011 fn test_default_validator_mixed_standard_and_custom() {
1012 let result = DefaultRpcModuleValidator::parse_selection("eth,admin,mycustom,debug");
1014 assert!(result.is_err());
1015 assert!(result.unwrap_err().contains("Unknown RPC module: 'mycustom'"));
1016 }
1017}