just-a-tag

Crates.iojust-a-tag
lib.rsjust-a-tag
version0.1.1
sourcesrc
created_at2023-07-18 21:04:10.198594
updated_at2023-07-18 21:08:05.470268
descriptionA #tag-type, RFC 1035 DNS label compatible
homepagehttps://github.com/sunsided/just-a-tag
repositoryhttps://github.com/sunsided/just-a-tag
max_upload_size
id919758
size37,967
Markus Mayer (sunsided)

documentation

README

Just a tag.

This crate contains the Tag type, an RFC 1035 DNS label compatible string, with parsing FromStr and optional serde support.

Tag examples

use justatag::Tag;

fn tags() {
    assert_eq!(Tag::new("some-tag"), "some-tag");
    assert_eq!(Tag::from_str("some-tag").unwrap(), "some-tag");
    assert!(Tag::from_str("invalid-").is_err());
}

Unions of tags

A bit untrue to the crate's name, it also provides the [TagUnion] type, which represents (unsurprisingly, this time) a union of tags.

use std::collections::HashSet;
use justatag::{MatchesAnyTagUnion, Tag, TagUnion};

fn tag_unions() {
    let union = TagUnion::from_str("foo").unwrap();
    assert!(union.contains(&Tag::new("foo")));
    assert_eq!(union.len(), 1);

    let union = TagUnion::from_str("foo+bar").unwrap();
    assert!(union.contains(&Tag::new("foo")));
    assert!(union.contains(&Tag::new("bar")));
    assert_eq!(union.len(), 2);

    // TagUnions are particularly interesting when bundled up.
    let unions = vec![
        TagUnion::from_str("bar+baz").unwrap(),
        TagUnion::from_str("foo").unwrap()
    ];

    // foo matches
    let set_1 = HashSet::from_iter([Tag::new("foo"), Tag::new("bar")]);
    assert!(unions.matches_set(&set_1));

    // bar+baz matches
    let set_2 = HashSet::from_iter([Tag::new("fubar"), Tag::new("bar"), Tag::new("baz")]);
    assert!(unions.matches_set(&set_2));

    // none match
    let set_3 = HashSet::from_iter([Tag::new("fubar"), Tag::new("bar")]);
    assert!(!unions.matches_set(&set_3));
}
Commit count: 9

cargo fmt