From f270b08a486d8e4ec85434ce13a060408d66e761 Mon Sep 17 00:00:00 2001
From: Nazar Mokrynskyi <nazar@mokrynskyi.com>
Date: Thu, 11 Jan 2024 13:41:59 +0200
Subject: [PATCH] Move Cors data structure into reachable place (#2898)

Trivial refactoring, fixes
https://github.com/paritytech/polkadot-sdk/issues/2885
---
 substrate/client/cli/src/arg_enums.rs        | 45 ++++++++++++++++++++
 substrate/client/cli/src/commands/run_cmd.rs | 45 +-------------------
 2 files changed, 47 insertions(+), 43 deletions(-)

diff --git a/substrate/client/cli/src/arg_enums.rs b/substrate/client/cli/src/arg_enums.rs
index d4a4b7cfdf6..d436673cb9d 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<sc_service::config::RpcMethods> for RpcMethods {
 	}
 }
 
+/// CORS setting
+///
+/// The type is introduced to overcome `Option<Option<T>>` handling of `clap`.
+#[derive(Clone, Debug)]
+pub enum Cors {
+	/// All hosts allowed.
+	All,
+	/// Only hosts on the list are allowed.
+	List(Vec<String>),
+}
+
+impl From<Cors> for Option<Vec<String>> {
+	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<Self, Self::Err> {
+		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 bc62dc3324e..4ac10a03cd4 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 <https://www.gnu.org/licenses/>.
 
 use crate::{
-	arg_enums::RpcMethods,
+	arg_enums::{Cors, RpcMethods},
 	error::{Error, Result},
 	params::{
 		ImportParams, KeystoreParams, NetworkParams, OffchainWorkerParams, SharedParams,
@@ -108,7 +108,7 @@ pub struct RunCmd {
 	/// value). Value of `all` will disable origin validation. Default is to
 	/// allow localhost and <https://polkadot.js.org> origins. When running in
 	/// `--dev` mode the default is to allow all origins.
-	#[arg(long, value_name = "ORIGINS", value_parser = parse_cors)]
+	#[arg(long, value_name = "ORIGINS")]
 	pub rpc_cors: Option<Cors>,
 
 	/// The human-readable name for this node.
@@ -470,47 +470,6 @@ fn rpc_interface(
 	}
 }
 
-/// CORS setting
-///
-/// The type is introduced to overcome `Option<Option<T>>` handling of `clap`.
-#[derive(Clone, Debug)]
-pub enum Cors {
-	/// All hosts allowed.
-	All,
-	/// Only hosts on the list are allowed.
-	List(Vec<String>),
-}
-
-impl From<Cors> for Option<Vec<String>> {
-	fn from(cors: Cors) -> Self {
-		match cors {
-			Cors::All => None,
-			Cors::List(list) => Some(list),
-		}
-	}
-}
-
-/// Parse cors origins.
-fn parse_cors(s: &str) -> Result<Cors> {
-	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::*;
-- 
GitLab