use std::slice::Iter;
use crate::SharedString;
#[derive(PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
pub struct Label(pub(crate) SharedString, pub(crate) SharedString);
impl Label {
pub fn new<K, V>(key: K, value: V) -> Self
where
K: Into<SharedString>,
V: Into<SharedString>,
{
Label(key.into(), value.into())
}
pub const fn from_static_parts(key: &'static str, value: &'static str) -> Self {
Label(SharedString::const_str(key), SharedString::const_str(value))
}
pub fn key(&self) -> &str {
self.0.as_ref()
}
pub fn value(&self) -> &str {
self.1.as_ref()
}
pub fn into_parts(self) -> (SharedString, SharedString) {
(self.0, self.1)
}
}
impl<K, V> From<&(K, V)> for Label
where
K: Into<SharedString> + Clone,
V: Into<SharedString> + Clone,
{
fn from(pair: &(K, V)) -> Label {
Label::new(pair.0.clone(), pair.1.clone())
}
}
impl<K, V> From<(&K, &V)> for Label
where
K: Into<SharedString> + Clone,
V: Into<SharedString> + Clone,
{
fn from(pair: (&K, &V)) -> Label {
Label::new(pair.0.clone(), pair.1.clone())
}
}
pub trait IntoLabels {
fn into_labels(self) -> Vec<Label>;
}
impl IntoLabels for Vec<Label> {
fn into_labels(self) -> Vec<Label> {
self
}
}
impl IntoLabels for Iter<'_, Label> {
fn into_labels(self) -> Vec<Label> {
self.cloned().collect()
}
}
impl<T: ?Sized, L> IntoLabels for &T
where
Self: IntoIterator<Item = L>,
L: Into<Label>,
{
fn into_labels(self) -> Vec<Label> {
self.into_iter().map(|l| l.into()).collect()
}
}
#[cfg(test)]
mod label_tests {
use super::*;
#[test]
fn slice_labels() {
let labels = [("x", "a"), ("y", "b")];
fn from_slice_to_labels(labels: &[(&'static str, &'static str)]) -> Vec<Label> {
labels.into_labels()
}
let expected = vec![Label::new("x", "a"), Label::new("y", "b")];
assert_eq!(from_slice_to_labels(&labels), expected);
}
#[test]
fn btreemap_to_labels() {
use std::collections::BTreeMap;
let labels_btreemap = BTreeMap::from([("customer", "Rust Foundation")]);
let expected = vec![Label::new("customer", "Rust Foundation")];
assert_eq!(labels_btreemap.into_labels(), expected);
}
}