steamy_vdf/entry/
value.rs1use std::ops::Deref;
2use super::Entry;
3
4#[derive(Clone, PartialEq, Eq, Debug)]
5pub struct Value(String);
6
7impl From<String> for Value {
8 fn from(value: String) -> Value {
9 Value(value)
10 }
11}
12
13impl Into<Entry> for Value {
14 fn into(self) -> Entry {
15 Entry::Value(self)
16 }
17}
18
19impl Deref for Value {
20 type Target = str;
21
22 fn deref(&self) -> &Self::Target {
23 &self.0
24 }
25}
26
27impl Value {
28 pub fn to<T: Parse>(&self) -> Option<T> {
30 T::parse(&self.0)
31 }
32}
33
34pub trait Parse: Sized {
36 fn parse(string: &str) -> Option<Self>;
38}
39
40macro_rules! from_str {
41 (for) => ();
42
43 (for $ty:ident $($rest:tt)*) => (
44 from_str!($ty);
45 from_str!(for $($rest)*);
46 );
47
48 ($ty:ident) => (
49 impl Parse for $ty {
50 fn parse(string: &str) -> Option<Self> {
51 string.parse::<$ty>().ok()
52 }
53 }
54 );
55}
56
57use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
58from_str!(for IpAddr Ipv4Addr Ipv6Addr SocketAddr SocketAddrV4 SocketAddrV6);
59from_str!(for i8 i16 i32 i64 isize u8 u16 u32 u64 usize f32 f64);
60
61impl Parse for bool {
62 fn parse(string: &str) -> Option<Self> {
63 match string {
64 "0" => Some(false),
65 "1" => Some(true),
66 v => v.parse::<bool>().ok()
67 }
68 }
69}