ss58_registry/
address_format.rs

1// Copyright (C) 2021-2022 Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18/// A custom address format. See also [`Ss58AddressFormatRegistry`]
19#[derive(Copy, Clone, PartialEq, Eq, Debug)]
20pub struct Ss58AddressFormat {
21	prefix: u16,
22}
23
24/// An enumeration of unique networks.
25/// Some are reserved.
26impl Ss58AddressFormat {
27	/// Custom constructor
28	#[inline]
29	pub fn custom(prefix: u16) -> Self {
30		Ss58AddressFormat { prefix }
31	}
32
33	/// Address prefix used on the network
34	pub const fn prefix(&self) -> u16 {
35		self.prefix
36	}
37
38	/// names of all address formats
39	pub fn all_names() -> &'static [&'static str] {
40		&ALL_SS58_ADDRESS_FORMAT_NAMES
41	}
42
43	/// All known address formats.
44	pub fn all() -> &'static [Ss58AddressFormatRegistry] {
45		&ALL_SS58_ADDRESS_FORMATS
46	}
47}
48
49/// Display the name of the address format (not the description).
50#[cfg(feature = "std")]
51impl std::fmt::Display for Ss58AddressFormat {
52	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
53		if let Ok(lookup) =
54			PREFIX_TO_INDEX.binary_search_by_key(&self.prefix, |(prefix, _)| *prefix)
55		{
56			let (_, idx) = PREFIX_TO_INDEX[lookup];
57			write!(f, "{}", ALL_SS58_ADDRESS_FORMAT_NAMES[idx])
58		} else {
59			write!(f, "{}", self.prefix)
60		}
61	}
62}
63
64/// const function to convert [`Ss58AddressFormat`] to u16
65pub const fn from_address_format(x: Ss58AddressFormat) -> u16 {
66	x.prefix
67}
68
69impl From<Ss58AddressFormatRegistry> for Ss58AddressFormat {
70	fn from(x: Ss58AddressFormatRegistry) -> Ss58AddressFormat {
71		Ss58AddressFormat { prefix: x as u16 }
72	}
73}
74
75impl From<u8> for Ss58AddressFormat {
76	#[inline]
77	fn from(x: u8) -> Ss58AddressFormat {
78		Ss58AddressFormat::from(u16::from(x))
79	}
80}
81
82impl From<Ss58AddressFormat> for u16 {
83	#[inline]
84	fn from(x: Ss58AddressFormat) -> u16 {
85		from_address_format(x)
86	}
87}
88
89impl From<u16> for Ss58AddressFormat {
90	#[inline]
91	fn from(prefix: u16) -> Ss58AddressFormat {
92		Ss58AddressFormat { prefix }
93	}
94}
95
96impl<'a> TryFrom<&'a str> for Ss58AddressFormat {
97	type Error = ParseError;
98
99	fn try_from(x: &'a str) -> Result<Ss58AddressFormat, Self::Error> {
100		Ss58AddressFormatRegistry::try_from(x).map(|a| a.into())
101	}
102}
103
104impl<'a> TryFrom<&'a str> for Ss58AddressFormatRegistry {
105	type Error = ParseError;
106
107	fn try_from(x: &'a str) -> Result<Ss58AddressFormatRegistry, Self::Error> {
108		ALL_SS58_ADDRESS_FORMAT_NAMES
109			.into_iter()
110			.position(|n| n.eq_ignore_ascii_case(x))
111			.map(|lookup| ALL_SS58_ADDRESS_FORMATS[lookup])
112			.ok_or(ParseError)
113	}
114}
115
116#[cfg(feature = "std")]
117impl From<Ss58AddressFormat> for String {
118	fn from(x: Ss58AddressFormat) -> String {
119		x.to_string()
120	}
121}
122
123#[cfg(feature = "std")]
124impl std::str::FromStr for Ss58AddressFormatRegistry {
125	type Err = ParseError;
126
127	fn from_str(data: &str) -> Result<Self, Self::Err> {
128		TryFrom::try_from(data)
129	}
130}