try_runtime_core/common/
parse.rs

1// This file is part of try-runtime-cli.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use 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        // could use Url crate as well, but lets keep it simple for now.
40        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}