try_runtime_core/common/
parse.rs1use sp_version::StateVersion;
19
20pub(crate) fn hash(block_hash: &str) -> Result<String, String> {
21 let (block_hash, offset) = if let Some(block_hash) = block_hash.strip_prefix("0x") {
22 (block_hash, 2)
23 } else {
24 (block_hash, 0)
25 };
26
27 if let Some(pos) = block_hash.chars().position(|c| !c.is_ascii_hexdigit()) {
28 Err(format!(
29 "Expected block hash, found illegal hex character at position: {}",
30 offset + pos,
31 ))
32 } else {
33 Ok(block_hash.into())
34 }
35}
36
37pub(crate) fn url(s: &str) -> Result<String, &'static str> {
38 if s.starts_with("ws://") || s.starts_with("wss://") {
39 Ok(s.to_string())
41 } else {
42 Err("not a valid WS(S) url: must start with 'ws://' or 'wss://'")
43 }
44}
45
46pub(crate) fn state_version(s: &str) -> Result<StateVersion, &'static str> {
47 s.parse::<u8>()
48 .map_err(|_| ())
49 .and_then(StateVersion::try_from)
50 .map_err(|_| "Invalid state version.")
51}