1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! Utilities for de-/serialization.

#[cfg(feature = "erased")]
pub mod erased;

pub(crate) mod de;
pub(crate) mod ser;


use serde;
use std;


/// A type that can be used to as a potentially temporary string-based tag.
/// 
/// This type can be constructed (and deserialiezd) from both an owned string
/// and a borroewd string. While there are similarities to `Cow<str>`, this type
/// will always be deserialized as `Borrowed` if the deserializer allows this.
/// Unlike `&'de str`, this type can, however, also be deserialized from an
/// owned string or a temporary string not fulfilling the required lifetime
/// bound.
/// 
/// The intended use of this type is as a temporary tag store/reference to be
/// passed on to a string-based `SeedFactory` implementation.
#[derive(Clone, Debug)]
pub enum TagString<'a> {
    Owned(String),
    Borrowed(&'a str),
}

impl<'a> From<&'a str> for TagString<'a> {
    fn from(source: &'a str) -> Self {
        TagString::Borrowed(source)
    }
}

impl<'a> From<String> for TagString<'a> {
    fn from(source: String) -> Self {
        TagString::Owned(source)
    }
}

impl<'a> From<std::borrow::Cow<'a, str>> for TagString<'a> {
    fn from(source: std::borrow::Cow<'a, str>) -> Self {
        match source {
            std::borrow::Cow::Owned(v) => TagString::Owned(v),
            std::borrow::Cow::Borrowed(v) => TagString::Borrowed(v),
        }
    }
}

impl<'a> From<TagString<'a>> for std::borrow::Cow<'a, str> {
    fn from(val: TagString<'a>) -> Self {
        match val {
            TagString::Owned(v) => std::borrow::Cow::Owned(v),
            TagString::Borrowed(v) => std::borrow::Cow::Borrowed(v),
        }
    }
}

impl<'a> std::ops::Deref for TagString<'a> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        match *self {
            TagString::Owned(ref v) => v,
            TagString::Borrowed(v) => v,
        }
    }
}

impl<'a, B> std::cmp::PartialEq<B> for TagString<'a>
where
    B: PartialEq<str>,
{
    fn eq(&self, other: &B) -> bool {
        other.eq(&**self)
    }
}

impl<'a, 'b> std::cmp::PartialEq<TagString<'b>> for TagString<'a> {
    fn eq(&self, other: &TagString<'b>) -> bool {
        (**self).eq(&**other)
    }
}

impl<'a> std::cmp::Eq for TagString<'a> {}

impl<'a, B> std::cmp::PartialOrd<B> for TagString<'a>
where
    B: PartialOrd<str>,
{
    fn partial_cmp(&self, other: &B) -> Option<std::cmp::Ordering> {
        other.partial_cmp(&**self).map(std::cmp::Ordering::reverse)
    }
}

impl<'a, 'b> std::cmp::PartialOrd<TagString<'b>> for TagString<'a> {
    fn partial_cmp(&self, other: &TagString) -> Option<std::cmp::Ordering> {
        (**self).partial_cmp(&**other)
    }
}

impl<'a> std::cmp::Ord for TagString<'a> {
    fn cmp(&self, other: &TagString) -> std::cmp::Ordering {
        (**self).cmp(&**other)
    }
}

impl<'a> std::fmt::Display for TagString<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        (**self).fmt(f)
    }
}

impl<'a> std::hash::Hash for TagString<'a> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        (**self).hash(state)
    }
}

impl<'a> AsRef<str> for TagString<'a> {
    fn as_ref(&self) -> &str {
        self
    }
}


impl<'de> serde::Deserialize<'de> for TagString<'de> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct Visitor;

        impl<'de> serde::de::Visitor<'de> for Visitor {
            type Value = TagString<'de>;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(formatter, "any type of string")
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(TagString::Owned(v.to_owned()))
            }

            fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(TagString::Borrowed(v))
            }

            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(TagString::Owned(v))
            }
        }

        deserializer.deserialize_str(Visitor)
    }
}

impl<'a> serde::Serialize for TagString<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer
    {
        serializer.serialize_str(self)
    }
}