tytanic_core/test/
id.rs

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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Test identifiers.

use std::borrow::{Borrow, Cow};
use std::fmt::{self, Debug, Display};
use std::ops::Deref;
use std::path::{Component, Path, PathBuf};
use std::str::FromStr;

use ecow::EcoString;
use thiserror::Error;

/// A test id, this is the relative path from the test root directory, down to
/// the folder containing the test script.
///
/// Each part of the path must be a simple id containing only ASCII
/// alpha-numeric characters, dashes `-` or underscores `_` and start with an
/// alphabetic character. This restriction may be lifted in the future.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
pub struct Id(EcoString);

impl Id {
    /// The test component separator.
    pub const SEPARATOR: &'static str = "/";
}

impl Id {
    /// Turns this string into an id.
    ///
    /// All components must start at least one ascii alphabetic letter and
    /// contain only ascii alphanumeric characters, underscores and minuses.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// let id = Id::new("a/b/c")?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// # Errors
    /// Returns an error if a component wasn't valid.
    pub fn new<S: Into<EcoString>>(string: S) -> Result<Self, ParseIdError> {
        let id = string.into();
        Self::validate(&id)?;

        Ok(Self(id))
    }

    /// Turns this path into an id, this follows the same rules as
    /// [`Id::new`] with the additional constraint that paths must valid
    /// UTF-8.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// let id = Id::new_from_path("a/b/c")?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    ///
    /// # Errors
    /// Returns an error if a component wasn't valid.
    pub fn new_from_path<P: AsRef<Path>>(path: P) -> Result<Self, ParseIdError> {
        fn inner(path: &Path) -> Result<Id, ParseIdError> {
            let mut id = String::new();

            for component in path.components() {
                match component {
                    Component::Normal(comp) => {
                        if let Some(comp) = comp.to_str() {
                            Id::validate_component(comp)?;

                            if !id.is_empty() {
                                id.push_str(Id::SEPARATOR);
                            }

                            id.push_str(comp);
                        } else {
                            return Err(ParseIdError::InvalidFragment);
                        }
                    }
                    _ => return Err(ParseIdError::InvalidFragment),
                }
            }

            Ok(Id(id.into()))
        }

        inner(path.as_ref())
    }

    /// Turns this string into an id without validating it.
    ///
    /// # Safety
    /// The caller must ensure that the given string is a valid id.
    pub unsafe fn new_unchecked(string: EcoString) -> Self {
        debug_assert!(Self::is_valid(&string));
        Self(string)
    }
}

impl Id {
    /// Whether the given string is a valid id.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// assert!( Id::is_valid("a/b/c"));
    /// assert!( Id::is_valid("a/b"));
    /// assert!( Id::is_valid("a"));
    /// assert!(!Id::is_valid("a//b"));  // empty component
    /// assert!(!Id::is_valid("a/"));    // empty component
    /// ```
    pub fn is_valid<S: AsRef<str>>(string: S) -> bool {
        Self::validate(string).is_ok()
    }

    fn validate<S: AsRef<str>>(string: S) -> Result<(), ParseIdError> {
        for fragment in string.as_ref().split(Self::SEPARATOR) {
            Self::validate_component(fragment)?;
        }

        Ok(())
    }

    /// Whether the given string is a valid id component.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// assert!( Id::is_component_valid("a"));
    /// assert!( Id::is_component_valid("a1"));
    /// assert!(!Id::is_component_valid("1a"));  // invalid char
    /// assert!(!Id::is_component_valid("a "));  // invalid char
    /// ```
    pub fn is_component_valid<S: AsRef<str>>(component: S) -> bool {
        Self::validate_component(component).is_ok()
    }

    // TODO(tinger): this seems to be the culprit of the 100% doc tests
    fn validate_component<S: AsRef<str>>(component: S) -> Result<(), ParseIdError> {
        let component = component.as_ref();

        if component.is_empty() {
            return Err(ParseIdError::Empty);
        }

        let mut chars = component.chars().peekable();
        if !chars.next().unwrap().is_ascii_alphabetic() {
            return Err(ParseIdError::InvalidFragment);
        }

        if chars.peek().is_some()
            && !chars.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
        {
            return Err(ParseIdError::InvalidFragment);
        }

        Ok(())
    }
}

impl Id {
    /// The full id as a `str`, this string is never empty.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Clones the inner [`EcoString`].
    pub fn to_inner(&self) -> EcoString {
        self.0.clone()
    }

    /// The name of this test, the last component of this id. This string is
    /// never empty.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// let id = Id::new("a/b/c")?;
    /// assert_eq!(id.name(), "c");
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn name(&self) -> &str {
        self.components()
            .next_back()
            .expect("id is always non-empty")
    }

    /// The module containing the, all but the last component of this id. This
    /// string may be empty.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// let id = Id::new("a/b/c")?;
    /// assert_eq!(id.module(), "a/b");
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn module(&self) -> &str {
        let mut c = self.components();
        _ = c.next_back().expect("id is always non-empty");
        c.rest
    }

    /// The ancestors of this id, this corresponds to the ancestors of the
    /// test's path.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// let id = Id::new("a/b/c")?;
    /// let mut ancestors = id.ancestors();
    /// assert_eq!(ancestors.next(), Some("a/b/c"));
    /// assert_eq!(ancestors.next(), Some("a/b"));
    /// assert_eq!(ancestors.next(), Some("a"));
    /// assert_eq!(ancestors.next(), None);
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn ancestors(&self) -> Ancestors<'_> {
        Ancestors { rest: &self.0 }
    }

    /// The components of this id, this corresponds to the components of the
    /// test's path.
    ///
    /// # Examples
    /// ```
    /// # use tytanic_core::test::Id;
    /// let id = Id::new("a/b/c")?;
    /// let mut components = id.components();
    /// assert_eq!(components.next(), Some("a"));
    /// assert_eq!(components.next(), Some("b"));
    /// assert_eq!(components.next(), Some("c"));
    /// assert_eq!(components.next(), None);
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn components(&self) -> Components<'_> {
        Components { rest: &self.0 }
    }

    /// Turns this id into a path relative to the test directory root.
    pub fn to_path(&self) -> Cow<'_, Path> {
        let s = self.as_str();

        if Self::SEPARATOR == std::path::MAIN_SEPARATOR_STR {
            Cow::Borrowed(Path::new(s))
        } else {
            Cow::Owned(PathBuf::from(
                s.replace(Self::SEPARATOR, std::path::MAIN_SEPARATOR_STR),
            ))
        }
    }
}

impl Id {
    /// Adds the given component to this Id without checking if it is valid.
    ///
    /// # Safety
    /// The caller must ensure that the given component is valid.
    pub unsafe fn push_component_unchecked<S: AsRef<str>>(&mut self, component: S) {
        let comp = component.as_ref();
        self.0.push_str(Self::SEPARATOR);
        self.0.push_str(comp);
    }

    /// Tries to add the given component to this id.
    pub fn push_component<S: AsRef<str>>(&mut self, component: S) -> Result<(), ParseIdError> {
        let comp = component.as_ref();
        Self::validate_component(comp)?;

        // SAFETY: we validated above
        unsafe {
            self.push_component_unchecked(component);
        }

        Ok(())
    }

    /// Tries to add the given component to this id.
    pub fn push_path_component<P: AsRef<Path>>(
        &mut self,
        component: P,
    ) -> Result<(), ParseIdError> {
        self.push_component(
            component
                .as_ref()
                .to_str()
                .ok_or(ParseIdError::InvalidFragment)?,
        )
    }
}

impl Deref for Id {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.0.as_str()
    }
}

impl AsRef<str> for Id {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl Borrow<str> for Id {
    fn borrow(&self) -> &str {
        self.0.as_str()
    }
}

impl FromStr for Id {
    type Err = ParseIdError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::new(s)
    }
}

impl Debug for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Debug::fmt(self.as_str(), f)
    }
}

impl Display for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(self.as_str(), f)
    }
}

impl PartialEq<str> for Id {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<Id> for str {
    fn eq(&self, other: &Id) -> bool {
        self == other.as_str()
    }
}

impl PartialEq<String> for Id {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<Id> for String {
    fn eq(&self, other: &Id) -> bool {
        self == other.as_str()
    }
}

impl PartialEq<EcoString> for Id {
    fn eq(&self, other: &EcoString) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<Id> for EcoString {
    fn eq(&self, other: &Id) -> bool {
        self == other.as_str()
    }
}

/// Returned by [`Id::ancestors`].
#[derive(Debug)]
pub struct Ancestors<'id> {
    rest: &'id str,
}

impl<'id> Iterator for Ancestors<'id> {
    type Item = &'id str;

    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.rest;
        self.rest = self
            .rest
            .rsplit_once(Id::SEPARATOR)
            .map(|(rest, _)| rest)
            .unwrap_or("");

        if ret.is_empty() {
            return None;
        }

        Some(ret)
    }
}

/// Returned by [`Id::components`].
#[derive(Debug)]
pub struct Components<'id> {
    rest: &'id str,
}

impl<'id> Iterator for Components<'id> {
    type Item = &'id str;

    fn next(&mut self) -> Option<Self::Item> {
        if self.rest.is_empty() {
            return None;
        }

        let (c, rest) = self
            .rest
            .split_once(Id::SEPARATOR)
            .unwrap_or((self.rest, ""));
        self.rest = rest;

        Some(c)
    }
}

impl<'id> DoubleEndedIterator for Components<'id> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.rest.is_empty() {
            return None;
        }

        let (rest, c) = self
            .rest
            .rsplit_once(Id::SEPARATOR)
            .unwrap_or(("", self.rest));
        self.rest = rest;

        Some(c)
    }
}

/// Returned by [`Id::new`][new] and [`Id::new_from_path`][new_from_path].
///
/// [new]: super::Id::new
/// [new_from_path]: super::Id::new_from_path
#[derive(Debug, Error)]
pub enum ParseIdError {
    /// An id contained an invalid fragment.
    #[error("id contained an invalid fragment")]
    InvalidFragment,

    /// An id contained empty or no fragments.
    #[error("id contained empty or no fragments")]
    Empty,
}

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

    #[test]
    fn test_ancestors() {
        assert_eq!(
            Id::new("a/b/c").unwrap().ancestors().collect::<Vec<_>>(),
            ["a/b/c", "a/b", "a"]
        );
    }

    #[test]
    fn test_components() {
        assert_eq!(
            Id::new("a/b/c").unwrap().components().collect::<Vec<_>>(),
            ["a", "b", "c"]
        );
        assert_eq!(
            Id::new("a/b/c")
                .unwrap()
                .components()
                .rev()
                .collect::<Vec<_>>(),
            ["c", "b", "a"]
        );
    }

    #[test]
    fn test_name() {
        let tests = [("a/b/c", "c"), ("a/b", "b"), ("a", "a")];

        for (id, name) in tests {
            assert_eq!(Id(id.into()).name(), name);
        }
    }

    #[test]
    fn test_module() {
        let tests = [("a/b/c", "a/b"), ("a/b", "a"), ("a", "")];

        for (id, name) in tests {
            assert_eq!(Id(id.into()).module(), name);
        }
    }

    #[test]
    fn test_str_invalid() {
        assert!(Id::new("/a").is_err());
        assert!(Id::new("a/").is_err());
        assert!(Id::new("a//b").is_err());

        assert!(Id::new("a ").is_err());
        assert!(Id::new("1a").is_err());
        assert!(Id::new("").is_err());
    }
}