use crate::{CompactIri, Keyword};
use iri_rs::Iri;
use rdfx::BlankId;
use smol_str::SmolStr;
use std::{borrow::Borrow, fmt, hash::Hash};
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Key(SmolStr);
impl Key {
#[must_use]
pub fn as_iri(&self) -> Option<Iri<&str>> {
Iri::parse(self.0.as_str()).ok()
}
#[must_use]
pub fn as_compact_iri(&self) -> Option<&CompactIri> {
CompactIri::new(self.0.as_str()).ok()
}
#[must_use]
pub fn as_blank_id(&self) -> Option<&BlankId> {
BlankId::new(self.0.as_str()).ok()
}
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn into_string(self) -> String {
String::from(self.0)
}
#[must_use]
pub fn is_keyword_like(&self) -> bool {
crate::is_keyword_like(self.0.as_str())
}
}
impl From<jstrict::object::Key> for Key {
fn from(k: jstrict::object::Key) -> Self {
Self::from(k.into_string())
}
}
impl From<String> for Key {
fn from(k: String) -> Self {
Self(SmolStr::from(k))
}
}
impl<'a> From<&'a str> for Key {
fn from(value: &'a str) -> Self {
Self(SmolStr::new(value))
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl Borrow<str> for Key {
fn borrow(&self) -> &str {
self.as_str()
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for Key {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Key {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor;
impl serde::de::Visitor<'_> for Visitor {
type Value = Key;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("a string")
}
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Key, E> {
Ok(Key::from(v))
}
}
deserializer.deserialize_str(Visitor)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct KeyRef<'a>(&'a str);
impl<'a> KeyRef<'a> {
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn is_keyword_like(&self) -> bool {
crate::is_keyword_like(self.as_str())
}
#[must_use]
pub fn as_str(&self) -> &'a str {
self.0
}
#[must_use]
pub fn to_owned(self) -> Key {
Key::from(self.0)
}
}
impl<'a> From<&'a str> for KeyRef<'a> {
fn from(s: &'a str) -> Self {
Self(s)
}
}
impl<'a> From<&'a Key> for KeyRef<'a> {
fn from(k: &'a Key) -> Self {
Self(k.as_str())
}
}
impl fmt::Display for KeyRef<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum KeyOrKeyword {
Keyword(Keyword),
Key(Key),
}
impl KeyOrKeyword {
#[must_use]
pub fn is_empty(&self) -> bool {
match self {
Self::Keyword(_) => false,
Self::Key(k) => k.is_empty(),
}
}
#[must_use]
pub fn into_keyword(self) -> Option<Keyword> {
match self {
Self::Keyword(k) => Some(k),
Self::Key(_) => None,
}
}
#[must_use]
pub fn into_key(self) -> Option<Key> {
match self {
Self::Keyword(_) => None,
Self::Key(k) => Some(k),
}
}
#[must_use]
pub fn as_keyword(&self) -> Option<Keyword> {
match self {
Self::Keyword(k) => Some(*k),
Self::Key(_) => None,
}
}
#[must_use]
pub fn as_key(&self) -> Option<&Key> {
match self {
Self::Keyword(_) => None,
Self::Key(k) => Some(k),
}
}
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::Keyword(k) => k.into_str(),
Self::Key(k) => k.as_str(),
}
}
}
#[allow(clippy::derived_hash_with_manual_eq)]
impl Hash for KeyOrKeyword {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
impl fmt::Display for KeyOrKeyword {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Key(k) => k.fmt(f),
Self::Keyword(k) => k.fmt(f),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum KeyOrKeywordRef<'a> {
Keyword(Keyword),
Key(KeyRef<'a>),
}
impl<'a> KeyOrKeywordRef<'a> {
#[must_use]
pub fn to_owned(self) -> KeyOrKeyword {
match self {
Self::Keyword(k) => KeyOrKeyword::Keyword(k),
Self::Key(k) => KeyOrKeyword::Key(k.to_owned()),
}
}
#[must_use]
pub fn as_str(&self) -> &'a str {
match self {
Self::Keyword(k) => k.into_str(),
Self::Key(k) => k.as_str(),
}
}
}
impl<'a> From<&'a str> for KeyOrKeywordRef<'a> {
fn from(s: &'a str) -> Self {
match Keyword::try_from(s) {
Ok(k) => Self::Keyword(k),
Err(_) => Self::Key(s.into()),
}
}
}
impl<'a> From<&'a KeyOrKeyword> for KeyOrKeywordRef<'a> {
fn from(k: &'a KeyOrKeyword) -> Self {
match k {
KeyOrKeyword::Keyword(k) => Self::Keyword(*k),
KeyOrKeyword::Key(k) => Self::Key(k.into()),
}
}
}
impl<'a> From<KeyRef<'a>> for KeyOrKeywordRef<'a> {
fn from(k: KeyRef<'a>) -> Self {
Self::Key(k)
}
}
impl<'a> From<&'a Key> for KeyOrKeywordRef<'a> {
fn from(k: &'a Key) -> Self {
Self::Key(k.into())
}
}
pub enum KeyOrType {
Key(Key),
Type,
}
impl KeyOrType {
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::Key(k) => k.as_str(),
Self::Type => "@type",
}
}
}
impl fmt::Display for KeyOrType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_str().fmt(f)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
use crate::{TryFromJson, context::Context};
use jstrict::Parse;
use std::{
collections::HashMap,
hash::{BuildHasher, RandomState},
};
fn parse_definition(text: &str) -> crate::context::Definition {
let (value, _) = jstrict::Value::parse_str(text).unwrap();
match Context::try_from_json(&value).unwrap() {
Context::One(crate::ContextEntry::Definition(definition)) => Some(definition),
Context::One(_) | Context::Many(_) => None,
}
.unwrap()
}
#[test]
fn keys_from_borrowed_and_owned_strings_agree() {
let borrowed = Key::from("speed");
let owned = Key::from(String::from("speed"));
assert_eq!(borrowed, owned);
let hasher = RandomState::new();
assert_eq!(hasher.hash_one(&borrowed), hasher.hash_one(&owned));
assert_eq!(hasher.hash_one(&borrowed), hasher.hash_one("speed"));
let map = HashMap::from([(borrowed, 7_u8)]);
assert_eq!(map.get("speed"), Some(&7));
}
#[test]
fn keys_of_separately_parsed_contexts_are_equal() {
let first = parse_definition(r#"{"speed": "https://example.org/speed"}"#);
let second = parse_definition(r#"{"speed": "https://example.org/velocity"}"#);
let first_key = first.bindings.get_key("speed").unwrap();
let second_key = second.bindings.get_key("speed").unwrap();
assert_eq!(first_key, second_key);
assert_eq!(first_key.cmp(second_key), std::cmp::Ordering::Equal);
assert!(!first_key.0.is_heap_allocated());
assert_eq!(*first_key, first_key.clone());
}
#[cfg(feature = "serde")]
#[test]
fn key_deserializes_through_serde_content_buffer() {
let (value, _) = jstrict::Value::parse_str(r#"{"@vocab": "http://example.com/", "term": "http://example.com/term"}"#).unwrap();
let definition: crate::context::Definition = crate::from_value(value).unwrap();
assert!(definition.bindings.get(&Key::from("term")).is_some());
}
#[cfg(feature = "serde")]
#[test]
fn key_deserializes_from_owned_string() {
let (value, _) = jstrict::Value::parse_str(r#""api""#).unwrap();
let key: Key = crate::from_value(value).unwrap();
assert_eq!(key.as_str(), "api");
}
}