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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use crate::StrictEq;
use derive_more::{
    AsRef, Constructor, Deref, DerefMut, Display, From, Index, IndexMut, Into,
    IntoIterator,
};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, fmt, iter::FromIterator};

/// Represents a sequence of one or more tags
///
/// In vimwiki, :my-tag: would become
///
/// Tags([ Tag(my-tag) ])
///
/// Similarly, :my-tag-1:my-tag-2: would become
///
/// Tags([ Tag(my-tag-1), Tag(my-tag-2) ])
///
#[derive(
    AsRef,
    Constructor,
    Clone,
    Debug,
    Deref,
    DerefMut,
    From,
    Index,
    IndexMut,
    Into,
    IntoIterator,
    Eq,
    PartialEq,
    Hash,
    Serialize,
    Deserialize,
)]
#[as_ref(forward)]
#[into_iterator(owned, ref, ref_mut)]
pub struct Tags<'a>(
    /// Represents the tags contained within the tag set
    Vec<Tag<'a>>,
);

impl Tags<'_> {
    pub fn to_borrowed(&self) -> Tags {
        let inner = self.0.iter().map(Tag::as_borrowed).collect();

        Tags(inner)
    }

    pub fn into_owned(self) -> Tags<'static> {
        let inner = self.0.into_iter().map(Tag::into_owned).collect();

        Tags(inner)
    }
}

impl<'a> fmt::Display for Tags<'a> {
    /// Extracts a string slice containing the entire tag
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use std::borrow::Cow;
    /// # use vimwiki_core::Tag;
    /// let tag = Tag::new(Cow::Borrowed("my-tag"));
    /// assert_eq!(tag.as_str(), "my-tag");
    /// ```
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for tag in self.0.iter() {
            write!(f, ":{}", tag.0)?;
        }
        write!(f, ":")
    }
}

impl<'a> From<Tag<'a>> for Tags<'a> {
    fn from(tag: Tag<'a>) -> Self {
        std::iter::once(tag).collect()
    }
}

impl From<String> for Tags<'static> {
    fn from(s: String) -> Self {
        std::iter::once(s).collect()
    }
}

impl<'a> From<&'a str> for Tags<'a> {
    fn from(s: &'a str) -> Self {
        std::iter::once(s).collect()
    }
}

impl<'a> FromIterator<&'a str> for Tags<'a> {
    fn from_iter<I: IntoIterator<Item = &'a str>>(iter: I) -> Self {
        Self::new(iter.into_iter().map(Tag::from).collect())
    }
}

impl FromIterator<String> for Tags<'static> {
    fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
        Self::new(iter.into_iter().map(Tag::from).collect())
    }
}

impl<'a> FromIterator<Cow<'a, str>> for Tags<'a> {
    fn from_iter<I: IntoIterator<Item = Cow<'a, str>>>(iter: I) -> Self {
        Self::new(iter.into_iter().map(Tag::from).collect())
    }
}

impl<'a> FromIterator<Tag<'a>> for Tags<'a> {
    fn from_iter<I: IntoIterator<Item = Tag<'a>>>(iter: I) -> Self {
        Self::new(iter.into_iter().collect())
    }
}

impl<'a> StrictEq for Tags<'a> {
    /// Same as PartialEq
    fn strict_eq(&self, other: &Self) -> bool {
        self == other
    }
}

/// Represents a single tag
#[derive(
    AsRef,
    Constructor,
    Clone,
    Debug,
    Display,
    From,
    Into,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Hash,
    Serialize,
    Deserialize,
)]
#[as_ref(forward)]
pub struct Tag<'a>(Cow<'a, str>);

impl<'a> Tag<'a> {
    /// Extracts a string slice containing the entire tag
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use std::borrow::Cow;
    /// # use vimwiki_core::Tag;
    /// let tag = Tag::new(Cow::Borrowed("my-tag"));
    /// assert_eq!(tag.as_str(), "my-tag");
    /// ```
    pub fn as_str(&self) -> &str {
        self.0.as_ref()
    }
}

impl Tag<'_> {
    pub fn as_borrowed(&self) -> Tag {
        use self::Cow::*;

        let inner = Cow::Borrowed(match &self.0 {
            Borrowed(x) => *x,
            Owned(x) => x.as_str(),
        });

        Tag(inner)
    }

    pub fn into_owned(self) -> Tag<'static> {
        let inner = Cow::from(self.0.into_owned());

        Tag(inner)
    }
}

impl<'a> From<&'a str> for Tag<'a> {
    fn from(s: &'a str) -> Self {
        Self::new(Cow::from(s))
    }
}

impl From<String> for Tag<'static> {
    fn from(s: String) -> Self {
        Self::new(Cow::from(s))
    }
}

impl<'a> StrictEq for Tag<'a> {
    /// Same as PartialEq
    #[inline]
    fn strict_eq(&self, other: &Self) -> bool {
        self == other
    }
}