|
1 |
| -use core::fmt::{Debug, Display}; |
2 |
| - |
3 | 1 | use alloc::vec::Vec;
|
4 | 2 | use byte_string_derive::byte_string;
|
5 | 3 | use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
|
6 | 4 | use scale_info::TypeInfo;
|
7 |
| -use serde::de::Error as DeError; |
8 |
| -use serde::ser::Error as SerError; |
9 |
| -use serde::{Deserialize, Serialize}; |
10 |
| -use sp_core::bounded::BoundedVec; |
11 |
| -use sp_core::ConstU32; |
12 | 5 |
|
13 | 6 | /// Wrapper for bytes that is serialized as hex string
|
14 | 7 | /// To be used for binary data that we want to display nicely but
|
@@ -52,54 +45,3 @@ impl<const N: usize> Default for SizedByteString<N> {
|
52 | 45 | Self([0; N])
|
53 | 46 | }
|
54 | 47 | }
|
55 |
| - |
56 |
| -/// Byte-encoded text string with bounded length |
57 |
| -#[derive(Eq, Clone, PartialEq, TypeInfo, Default, Encode, Decode, MaxEncodedLen)] |
58 |
| -pub struct BoundedString<const N: u32>(pub BoundedVec<u8, ConstU32<N>>); |
59 |
| - |
60 |
| -impl<const N: u32> TryFrom<Vec<u8>> for BoundedString<N> { |
61 |
| - type Error = <BoundedVec<u8, ConstU32<N>> as TryFrom<Vec<u8>>>::Error; |
62 |
| - |
63 |
| - fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> { |
64 |
| - Ok(Self(value.try_into()?)) |
65 |
| - } |
66 |
| -} |
67 |
| - |
68 |
| -impl<'a, const N: u32> Deserialize<'a> for BoundedString<N> { |
69 |
| - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
70 |
| - where |
71 |
| - D: serde::Deserializer<'a>, |
72 |
| - { |
73 |
| - Ok(Self( |
74 |
| - BoundedVec::try_from( |
75 |
| - alloc::string::String::deserialize(deserializer)?.as_bytes().to_vec(), |
76 |
| - ) |
77 |
| - .map_err(|_| D::Error::custom("Size limit exceeded"))?, |
78 |
| - )) |
79 |
| - } |
80 |
| -} |
81 |
| - |
82 |
| -impl<const N: u32> Serialize for BoundedString<N> { |
83 |
| - fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
84 |
| - where |
85 |
| - S: serde::Serializer, |
86 |
| - { |
87 |
| - let str = alloc::string::String::from_utf8(self.0.to_vec()) |
88 |
| - .map_err(|_| S::Error::custom("String is not valid UTF-8"))?; |
89 |
| - serializer.serialize_str(&str) |
90 |
| - } |
91 |
| -} |
92 |
| - |
93 |
| -impl<const N: u32> Display for BoundedString<N> { |
94 |
| - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
95 |
| - f.write_str( |
96 |
| - &alloc::string::String::from_utf8(self.0.to_vec()).map_err(|_| core::fmt::Error)?, |
97 |
| - ) |
98 |
| - } |
99 |
| -} |
100 |
| - |
101 |
| -impl<const N: u32> Debug for BoundedString<N> { |
102 |
| - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
103 |
| - f.write_str(&alloc::format!("BoundedString<{}>({:?})", N, self.0)) |
104 |
| - } |
105 |
| -} |
0 commit comments