From 5d0e9d769713d54d4444096061879c038c159e41 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 10 Jan 2024 03:46:17 +0200 Subject: [PATCH] Move `Cors` into `arg_enums` module to the rest of CLI data structures --- substrate/client/cli/src/arg_enums.rs | 45 +++++++++++++++++ substrate/client/cli/src/commands/run_cmd.rs | 51 +------------------- 2 files changed, 47 insertions(+), 49 deletions(-) diff --git a/substrate/client/cli/src/arg_enums.rs b/substrate/client/cli/src/arg_enums.rs index d4a4b7cfdf6d1..d436673cb9de7 100644 --- a/substrate/client/cli/src/arg_enums.rs +++ b/substrate/client/cli/src/arg_enums.rs @@ -19,6 +19,7 @@ //! Definitions of [`ValueEnum`] types. use clap::ValueEnum; +use std::str::FromStr; /// The instantiation strategy to use in compiled mode. #[derive(Debug, Clone, Copy, ValueEnum)] @@ -177,6 +178,50 @@ impl Into for RpcMethods { } } +/// CORS setting +/// +/// The type is introduced to overcome `Option>` handling of `clap`. +#[derive(Clone, Debug)] +pub enum Cors { + /// All hosts allowed. + All, + /// Only hosts on the list are allowed. + List(Vec), +} + +impl From for Option> { + fn from(cors: Cors) -> Self { + match cors { + Cors::All => None, + Cors::List(list) => Some(list), + } + } +} + +impl FromStr for Cors { + type Err = crate::Error; + + fn from_str(s: &str) -> Result { + let mut is_all = false; + let mut origins = Vec::new(); + for part in s.split(',') { + match part { + "all" | "*" => { + is_all = true; + break + }, + other => origins.push(other.to_owned()), + } + } + + if is_all { + Ok(Cors::All) + } else { + Ok(Cors::List(origins)) + } + } +} + /// Database backend #[derive(Debug, Clone, PartialEq, Copy, clap::ValueEnum)] #[value(rename_all = "lower")] diff --git a/substrate/client/cli/src/commands/run_cmd.rs b/substrate/client/cli/src/commands/run_cmd.rs index 5114d149323a4..4ac10a03cd446 100644 --- a/substrate/client/cli/src/commands/run_cmd.rs +++ b/substrate/client/cli/src/commands/run_cmd.rs @@ -17,7 +17,7 @@ // along with this program. If not, see . use crate::{ - arg_enums::RpcMethods, + arg_enums::{Cors, RpcMethods}, error::{Error, Result}, params::{ ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams, @@ -34,10 +34,7 @@ use sc_service::{ ChainSpec, Role, }; use sc_telemetry::TelemetryEndpoints; -use std::{ - net::{IpAddr, Ipv4Addr, SocketAddr}, - str::FromStr, -}; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; /// The `run` command used to run a node. #[derive(Debug, Clone, Parser)] @@ -473,50 +470,6 @@ fn rpc_interface( } } -/// CORS setting -/// -/// The type is introduced to overcome `Option>` handling of `clap`. -#[derive(Clone, Debug)] -pub enum Cors { - /// All hosts allowed. - All, - /// Only hosts on the list are allowed. - List(Vec), -} - -impl From for Option> { - fn from(cors: Cors) -> Self { - match cors { - Cors::All => None, - Cors::List(list) => Some(list), - } - } -} - -impl FromStr for Cors { - type Err = Error; - - fn from_str(s: &str) -> Result { - let mut is_all = false; - let mut origins = Vec::new(); - for part in s.split(',') { - match part { - "all" | "*" => { - is_all = true; - break - }, - other => origins.push(other.to_owned()), - } - } - - if is_all { - Ok(Cors::All) - } else { - Ok(Cors::List(origins)) - } - } -} - #[cfg(test)] mod tests { use super::*;