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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! A drop lookup in the form `(<scope>/)?<drop>(@<version>)?`.

use std::{
    cmp::Ordering,
    convert::TryInto,
    fmt,
};
use url::Url;
use super::ScopedName;

/// A drop lookup in the form `(<scope>/)?<name>(@<version>)?`.
///
/// By default, [`String`] is used for the generic `Name` and `Version` types.
///
/// When performing [zero-copy] parsing, it is recommended to use `Query<&str>`.
///
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
/// [zero-copy]: https://en.wikipedia.org/wiki/Zero-copy
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord, Hash)]
pub struct Query<Name = String, Version = Name> {
    /// The drop's owner.
    pub scope: Option<Name>,
    /// The drop's name.
    pub name: Name,
    /// The drop's version requirement.
    pub version: Option<Version>,
}

impl<N, V> From<ScopedName<N>> for Query<N, V> {
    #[inline]
    fn from(n: ScopedName<N>) -> Self {
        Self { scope: Some(n.scope), name: n.name, version: None }
    }
}

impl<A, B, X, Y> PartialEq<Query<X, Y>> for Query<A, B>
where
    A: PartialEq<X>,
    B: PartialEq<Y>,
{
    fn eq(&self, other: &Query<X, Y>) -> bool {
        // Needed because `Option<T>` only implements `PartialEq` over itself.
        fn eq_option<A, B>(a: &Option<A>, b: &Option<B>) -> bool
            where A: PartialEq<B>
        {
            match (a, b) {
                (Some(a), Some(b)) => a == b,
                (None, None) => true,
                _ => false,
            }
        }
        eq_option(&self.scope, &other.scope) &&
            self.name == other.name &&
            eq_option(&self.version, &other.version)
    }
}

impl<N: fmt::Display, V: fmt::Display> fmt::Display for Query<N, V> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(scope) = self.scope.as_ref() {
            write!(f, "{}/", scope)?;
        }

        write!(f, "{}", self.name)?;

        if let Some(version) = self.version.as_ref() {
            write!(f, "@{}", version)?;
        }

        Ok(())
    }
}

impl<'a> Query<&'a str> {
    // Monomorphized form of `parse_liberal` to slightly reduce the instruction
    // count of the binary.
    fn _parse_liberal(query: &'a str) -> Self {
        let mut scope_iter = query.splitn(2, '/');
        let (scope, rest) = match (scope_iter.next(), scope_iter.next()) {
            (None, _) => unreachable!(),
            (Some(rest), None) => (None, rest),
            (scope, Some(rest)) => (scope, rest),
        };

        let mut version_iter = rest.splitn(2, '@');
        let (name, version) = match (version_iter.next(), version_iter.next()) {
            (None, _) => unreachable!(),
            (Some(name), version) => (name, version),
        };

        Self { scope, name, version }
    }
}

impl<N, V> Query<N, V> {
    /// Creates a new `Query` instance with `scope`, `name`, and `version`.
    ///
    /// # Examples
    ///
    /// This serves as a convenience to not need to deal with explicitly
    /// wrapping types with `Some`.
    ///
    /// ```
    /// use oceanpkg::drop::name::Query;
    ///
    /// let query = Query::<&str>::new("core", "wget", "1.20");
    ///
    /// assert_eq!(query.to_string(), "core/wget@1.20");
    /// ```
    #[inline]
    pub fn new<A, B, C>(scope: A, name: B, version: C) -> Self
    where
        A: Into<N>,
        B: Into<N>,
        C: Into<V>,
    {
        Self {
            scope:   Some(scope.into()),
            name:    name.into(),
            version: Some(version.into()),
        }
    }

    /// Attempts to create a new instance by strictly parsing `name`.
    #[inline]
    pub fn parse<Q, NE, VE>(query: Q) -> Result<Self, ParseError<NE, VE>>
        where Q: TryInto<Self, Error = ParseError<NE, VE>>
    {
        query.try_into()
    }

    /// Creates a new instance by parsing `query` in a non-strict manner.
    ///
    /// # Examples
    ///
    /// ```
    /// use oceanpkg::drop::name::Query;
    ///
    /// let scope   = "core";
    /// let name    = "ruby";
    /// let version = "2.6";
    ///
    /// let query_string = format!("{}/{}@{}", scope, name, version);
    /// let query = Query::<&str>::parse_liberal(&query_string);
    ///
    /// assert_eq!(query.scope,   Some(scope));
    /// assert_eq!(query.name,    name);
    /// assert_eq!(query.version, Some(version));
    ///
    /// assert_eq!(query.to_string(), query_string);
    /// ```
    #[inline]
    pub fn parse_liberal<'a>(query: &'a str) -> Self
    where
        &'a str: Into<N>,
        &'a str: Into<V>,
    {
        Query::_parse_liberal(query).cast()
    }

    /// Converts `self` into a new `Query` by performing an [`Into`] conversion
    /// over all fields.
    ///
    /// [`Into`]: https://doc.rust-lang.org/std/convert/trait.Into.html
    #[inline]
    pub fn cast<A, B>(self) -> Query<A, B>
    where
        N: Into<A>,
        V: Into<B>,
    {
        Query {
            scope:   self.scope.map(Into::into),
            name:    self.name.into(),
            version: self.version.map(Into::into),
        }
    }

    /// Converts `self` into a new `Query` by performing an [`Into`] conversion
    /// over all fields.
    ///
    /// [`Into`]: https://doc.rust-lang.org/std/convert/trait.Into.html
    pub fn try_cast<A, B>(self) -> Result<Query<A, B>, ParseError<N::Error, V::Error>>
    where
        N: TryInto<A>,
        V: TryInto<B>,
    {
        let scope = match self.scope.map(TryInto::try_into) {
            Some(Err(error)) => return Err(ParseError::Scope(error)),
            Some(Ok(error)) => Some(error),
            None => None,
        };
        let name = match self.name.try_into() {
            Err(error) => return Err(ParseError::Name(error)),
            Ok(name) => name,
        };
        let version = match self.version.map(TryInto::try_into) {
            Some(Err(error)) => return Err(ParseError::Version(error)),
            Some(Ok(error)) => Some(error),
            None => None,
        };
        Ok(Query { scope, name, version })
    }

    /// Takes a shared reference to the fields of this query.
    ///
    /// See [`Query::as_mut`](#method.as_mut) for the mutable equivalent.
    #[inline]
    pub fn as_ref(&self) -> Query<&N, &V> {
        Query {
            scope:   self.scope.as_ref(),
            name:    &self.name,
            version: self.version.as_ref(),
        }
    }

    /// Takes a shared reference to the fields of this query as type `A`.
    ///
    /// See [`Query::to_mut`](#method.to_mut) for the mutable equivalent.
    ///
    /// # Examples
    ///
    /// ```
    /// use oceanpkg::drop::name::Query;
    ///
    /// let query: Query<String> = //
    /// # Query::new("", "", "");
    /// let by_ref: Query<&str> = query.to_ref();
    ///
    /// assert_eq!(query, by_ref);
    /// ```
    #[inline]
    pub fn to_ref<A>(&self) -> Query<&A>
    where
        N: AsRef<A>,
        V: AsRef<A>,
        A: ?Sized,
    {
        Query {
            scope:   self.scope.as_ref().map(AsRef::as_ref),
            name:    self.name.as_ref(),
            version: self.version.as_ref().map(AsRef::as_ref),
        }
    }

    /// Takes a mutable reference to the fields of this query.
    ///
    /// See [`Query::as_ref`](#method.as_ref) for the immutable equivalent.
    #[inline]
    pub fn as_mut(&mut self) -> Query<&mut N, &mut V> {
        Query {
            scope:   self.scope.as_mut(),
            name:    &mut self.name,
            version: self.version.as_mut(),
        }
    }

    /// Takes a mutable reference to the fields of this query as type `A`.
    ///
    /// See [`Query::to_ref`](#method.to_ref) for the immutable equivalent.
    #[inline]
    pub fn to_mut<A>(&mut self) -> Query<&mut A>
    where
        N: AsMut<A>,
        V: AsMut<A>,
        A: ?Sized,
    {
        Query {
            scope:   self.scope.as_mut().map(AsMut::as_mut),
            name:    self.name.as_mut(),
            version: self.version.as_mut().map(AsMut::as_mut),
        }
    }

    /// Returns the scoped name for `self` if `scope` exists.
    #[inline]
    pub fn scoped_name(&self) -> Option<ScopedName<&N>> {
        self.scope.as_ref().map(|scope| ScopedName { scope, name: &self.name })
    }

    /// Performs a partial version comparison between `self` and `other`.
    ///
    /// Returns `None` if:
    /// - One version is `Some` and the other is `Some`.
    /// - `<V as PartialOrd<B>>::partial_cmp` returns `None`.
    #[inline]
    pub fn cmp_version<A, B>(&self, other: &Query<A, B>) -> Option<Ordering>
        where V: PartialOrd<B>
    {
        match (&self.version, &other.version) {
            (Some(this), Some(other)) => this.partial_cmp(other),
            (None, None) => Some(Ordering::Equal),
            _ => None,
        }
    }

    /// Returns the name of `self` formatted for the filesystem.
    pub fn file_name(&self) -> String
    where
        N: fmt::Display,
        V: fmt::Display,
    {
        if let Some(version) = &self.version {
            format!("{}@{}", self.name, version)
        } else {
            self.name.to_string()
        }
    }

    /// Returns the name of `self` packaged as a tarball.
    pub fn tarball_name(&self) -> String
    where
        N: fmt::Display,
        V: fmt::Display,
    {
        let mut name = self.file_name();
        name.push_str(".tar.gz");
        name
    }

    /// Returns a new `Url` with the fields of `self` appended.
    ///
    /// # Examples
    ///
    /// If `scope` is empty, it is replaced with `core`.
    ///
    /// ```
    /// use oceanpkg::drop::name::Query;
    /// use url::Url;
    ///
    /// let query = "wget@1.20";
    /// let query = Query::<&str>::parse_liberal(query);
    ///
    /// let url = Url::parse("https://www.oceanpkg.org").unwrap();
    /// let url = query.join_to_url(&url).unwrap();
    ///
    /// assert_eq!(url.as_str(), "https://www.oceanpkg.org/u/core/p/wget?version=1.20");
    /// ```
    pub fn join_to_url(&self, url: &Url) -> Result<Url, url::ParseError>
    where
        N: fmt::Display,
        V: AsRef<str>,
    {
        let suffix = if let Some(scope) = &self.scope {
            format!("u/{}/p/{}", scope, self.name)
        } else {
            format!("u/core/p/{}", self.name)
        };
        let mut url = url.join(&suffix)?;
        if let Some(version) = &self.version {
            url.query_pairs_mut().append_pair("version", version.as_ref());
        }
        Ok(url)
    }
}

impl<'n, 'v, N: ?Sized, V: ?Sized> Query<&'n N, &'v V> {
    /// Returns the result of calling [`ToOwned::to_owned`] on the fields of
    /// `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oceanpkg::drop::name::Query;
    ///
    /// let query: Query<&str> = Query::new("core", "wget", "*");
    /// let owned: Query<String> = query.to_owned();
    ///
    /// assert_eq!(query, owned);
    /// ```
    ///
    /// [`ToOwned::to_owned`]: https://doc.rust-lang.org/std/borrow/trait.ToOwned.html#tymethod.to_owned
    pub fn to_owned(&self) -> Query<N::Owned, V::Owned>
    where
        N: ToOwned,
        V: ToOwned,
    {
        Query {
            scope:   self.scope.map(ToOwned::to_owned),
            name:    self.name.to_owned(),
            version: self.version.map(ToOwned::to_owned),
        }
    }
}

/// An error returned when parsing a `Query`.
pub enum ParseError<NameError, VersionError> {
    /// An error occurred when parsing the `scope` field.
    Scope(NameError),
    /// An error occurred when parsing the `name` field.
    Name(NameError),
    /// An error occurred when parsing the `version` field.
    Version(VersionError),
}

impl<N: fmt::Display, V: fmt::Display> fmt::Display for ParseError<N, V> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "failed to parse query ")?;
        match self {
            Self::Scope(error)   => write!(f, "scope: {}", error),
            Self::Name(error)    => write!(f, "name: {}", error),
            Self::Version(error) => write!(f, "version: {}", error),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_liberal() {
        let cases: &[(&str, (Option<&str>, &str, Option<&str>))] = &[
            ("ocean",           (None,          "ocean",  None)),
            ("ocean@1",         (None,          "ocean",  Some("1"))),
            ("ocean/ocean@1",   (Some("ocean"), "ocean",  Some("1"))),
            ("ocean//ocean@1",  (Some("ocean"), "/ocean", Some("1"))),
            ("ocean//ocean@@1", (Some("ocean"), "/ocean", Some("@1"))),
        ];
        for &(query_string, (scope, name, version)) in cases {
            let query = Query::<&str>::parse_liberal(query_string);

            assert_eq!(query, Query { scope, name, version });
            assert_eq!(query_string, query.to_string());
        }
    }
}