pop_chains/try_runtime/
parse.rs1use sp_version::StateVersion;
19
20use crate::Error;
21
22pub fn hash(block_hash: &str) -> Result<String, Error> {
24 let (block_hash, offset) = if let Some(block_hash) = block_hash.strip_prefix("0x") {
25 (block_hash, 2)
26 } else {
27 (block_hash, 0)
28 };
29
30 if let Some(pos) = block_hash.chars().position(|c| !c.is_ascii_hexdigit()) {
31 Err(Error::ParamParsingError(format!(
32 "Expected block hash, found illegal hex character at position: {}",
33 offset + pos,
34 )))
35 } else {
36 Ok(block_hash.into())
37 }
38}
39
40pub fn url(s: &str) -> Result<String, Error> {
42 if s.starts_with("ws://") || s.starts_with("wss://") {
43 Ok(s.to_string())
44 } else {
45 Err(Error::ParamParsingError(
46 "not a valid WS(S) url: must start with 'ws://' or 'wss://'".to_string(),
47 ))
48 }
49}
50
51pub fn state_version(s: &str) -> Result<StateVersion, Error> {
53 s.parse::<u8>()
54 .map_err(|_| ())
55 .and_then(StateVersion::try_from)
56 .map_err(|_| Error::ParamParsingError("Invalid state version.".to_string()))
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn parse_hash_works() {
65 assert!(hash("0x1234567890abcdef").is_ok());
66 assert!(hash("1234567890abcdef").is_ok());
67 assert!(hash("0x1234567890abcdefg").is_err());
68 assert!(hash("1234567890abcdefg").is_err());
69 }
70
71 #[test]
72 fn parse_url_works() {
73 assert!(url("ws://localhost:9944").is_ok());
74 assert!(url("wss://localhost:9944").is_ok());
75 assert!(url("http://localhost:9944").is_err());
76 assert!(url("https://localhost:9944").is_err());
77 }
78
79 #[test]
80 fn parse_state_version_works() {
81 assert!(state_version("0").is_ok());
82 assert!(state_version("1").is_ok());
83 assert!(state_version("100").is_err());
84 assert!(state_version("200").is_err());
85 }
86}