Skip to main content

domain_key/
key.rs

1//! Core Key implementation for domain-key
2//!
3//! This module contains the main `Key<T>` structure and its implementation,
4//! providing high-performance, type-safe key handling with extensive optimizations.
5
6use core::borrow::Borrow;
7use core::fmt;
8use core::hash::{Hash, Hasher};
9use core::marker::PhantomData;
10use core::ops::Deref;
11use core::str::FromStr;
12
13#[cfg(not(feature = "std"))]
14use alloc::borrow::{Cow, ToOwned};
15#[cfg(not(feature = "std"))]
16use alloc::string::String;
17
18#[cfg(feature = "std")]
19use std::borrow::Cow;
20
21#[cfg(feature = "serde")]
22use serde::{Deserialize, Serialize};
23
24use smartstring::alias::String as SmartString;
25
26use crate::domain::KeyDomain;
27use crate::error::KeyParseError;
28use crate::utils;
29
30// ============================================================================
31// CONSTANTS
32// ============================================================================
33
34/// Default maximum allowed length for any key
35///
36/// This is a reasonable default that balances usability with performance.
37/// Keys up to this length can benefit from stack allocation optimizations.
38/// Domains can override this with their own limits.
39pub const DEFAULT_MAX_KEY_LENGTH: usize = 64;
40
41// ============================================================================
42// SPLIT ITERATOR TYPES
43// ============================================================================
44
45/// Split cache type for consistent API
46pub type SplitCache<'a> = core::str::Split<'a, char>;
47
48/// Split iterator with consistent API
49#[derive(Debug)]
50pub struct SplitIterator<'a>(SplitCache<'a>);
51
52impl<'a> Iterator for SplitIterator<'a> {
53    type Item = &'a str;
54
55    #[inline]
56    fn next(&mut self) -> Option<Self::Item> {
57        self.0.next()
58    }
59}
60
61// ============================================================================
62// CORE KEY IMPLEMENTATION
63// ============================================================================
64
65/// High-performance generic key type with advanced optimizations
66///
67/// This is the core key type that provides type safety through the domain
68/// marker `T`. Keys are immutable after creation and use `SmartString` for
69/// optimal memory usage (stack allocation for short keys, heap for longer ones).
70///
71/// # Performance Characteristics
72///
73/// - **Memory Layout**: 32 bytes total (fits in single cache line)
74/// - **Hash Access**: O(1) via pre-computed hash (`.hash() -> u64`)
75/// - **Length Access**: O(1) via `SmartString` (inline length)
76/// - **String Access**: Direct reference to internal storage
77/// - **`HashMap` Lookup**: by `&str` via `Borrow<str>` — no temporary key needed
78/// - **Clone**: Efficient via `SmartString`'s copy-on-write semantics
79///
80/// # Type Parameters
81///
82/// * `T` - A domain marker type that implements `KeyDomain`
83///
84/// # Memory Layout
85///
86/// ```text
87/// Key<T> struct (32 bytes, cache-line friendly):
88/// ┌─────────────────────┬──────────┬─────────────┐
89/// │ SmartString (24B)   │ hash (8B)│ marker (0B) │
90/// └─────────────────────┴──────────┴─────────────┘
91/// ```
92///
93/// Keys use `SmartString` which stores strings up to 23 bytes inline on the stack,
94/// only allocating on the heap for longer strings. The pre-computed hash (feature-
95/// selected algorithm) is accessible via `.hash() -> u64`. The `Hash` trait
96/// delegates to `str` so that `Borrow<str>` works correctly with `HashMap`.
97///
98/// # Examples
99///
100/// ```rust
101/// use domain_key::{Key, Domain, KeyDomain};
102///
103/// #[derive(Debug)]
104/// struct UserDomain;
105///
106/// impl Domain for UserDomain {
107///     const DOMAIN_NAME: &'static str = "user";
108/// }
109///
110/// impl KeyDomain for UserDomain {
111///     const MAX_LENGTH: usize = 32;
112/// }
113///
114/// type UserKey = Key<UserDomain>;
115///
116/// let key = UserKey::new("john_doe")?;
117/// assert_eq!(key.as_str(), "john_doe");
118/// assert_eq!(key.domain(), "user");
119/// assert_eq!(key.len(), 8);
120/// # Ok::<(), domain_key::KeyParseError>(())
121/// ```
122#[derive(Debug)]
123pub struct Key<T: KeyDomain> {
124    /// Internal string storage using `SmartString` for optimal memory usage
125    inner: SmartString,
126
127    /// Pre-computed hash value accessible via [`Key::hash()`]
128    ///
129    /// This hash is computed once during key creation using the
130    /// feature-selected algorithm (gxhash / ahash / blake3 / fnv-1a).
131    /// It is **not** used by the [`Hash`] trait implementation — that
132    /// one delegates to `self.inner` so that the `Borrow<str>` contract
133    /// (`hash(key) == hash(key.borrow())`) is upheld.
134    hash: u64,
135
136    /// Zero-sized type marker for compile-time type safety
137    ///
138    /// This field provides compile-time type safety without any runtime
139    /// overhead. Different domain types cannot be mixed or compared.
140    _marker: PhantomData<T>,
141}
142
143// Manual Clone implementation to ensure optimal performance
144impl<T: KeyDomain> Clone for Key<T> {
145    /// Efficient clone implementation
146    ///
147    /// Cloning a key is efficient due to `SmartString`'s optimizations:
148    /// - For inline strings (≤23 chars): Simple memory copy
149    /// - For heap strings: Reference counting or copy-on-write
150    #[inline]
151    fn clone(&self) -> Self {
152        Self {
153            inner: self.inner.clone(),
154            hash: self.hash,
155            _marker: PhantomData,
156        }
157    }
158}
159
160// Manual PartialEq/Eq — compare only the key string, not cached fields
161impl<T: KeyDomain> PartialEq for Key<T> {
162    #[inline]
163    fn eq(&self, other: &Self) -> bool {
164        self.inner == other.inner
165    }
166}
167
168impl<T: KeyDomain> Eq for Key<T> {}
169
170// Manual PartialOrd/Ord — compare only the key string
171impl<T: KeyDomain> PartialOrd for Key<T> {
172    #[inline]
173    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
174        Some(self.cmp(other))
175    }
176}
177
178impl<T: KeyDomain> Ord for Key<T> {
179    #[inline]
180    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
181        self.inner.cmp(&other.inner)
182    }
183}
184
185// Hash implementation delegates to the inner string so that the
186// Borrow<str> contract is satisfied: hash(key) == hash(key.borrow()).
187// This allows HashMap<Key<T>, V>::get("some_str") to work correctly.
188impl<T: KeyDomain> Hash for Key<T> {
189    #[inline]
190    fn hash<H: Hasher>(&self, state: &mut H) {
191        self.inner.hash(state);
192    }
193}
194
195// Conditional Serde support for serialization/deserialization
196#[cfg(feature = "serde")]
197impl<T: KeyDomain> Serialize for Key<T> {
198    /// Serialize the key as its string representation
199    ///
200    /// Keys are serialized as their string content, not including
201    /// the cached hash or length for efficiency and compatibility.
202    #[inline]
203    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
204    where
205        S: serde::Serializer,
206    {
207        self.inner.serialize(serializer)
208    }
209}
210
211#[cfg(feature = "serde")]
212impl<'de, T: KeyDomain> Deserialize<'de> for Key<T> {
213    /// Deserialize and validate a key from its string representation
214    ///
215    /// This implementation chooses the optimal deserialization strategy
216    /// based on the format (human-readable vs binary) for best performance.
217    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
218    where
219        D: serde::Deserializer<'de>,
220    {
221        if deserializer.is_human_readable() {
222            // For human-readable formats (JSON, YAML), use zero-copy when possible
223            let s = <&str>::deserialize(deserializer)?;
224            Key::new(s).map_err(serde::de::Error::custom)
225        } else {
226            // For binary formats, deserialize as owned string
227            let s = String::deserialize(deserializer)?;
228            Key::from_string(s).map_err(serde::de::Error::custom)
229        }
230    }
231}
232
233// ============================================================================
234// KEY IMPLEMENTATION - CORE METHODS
235// ============================================================================
236
237impl<T: KeyDomain> Key<T> {
238    /// Creates a new key with comprehensive validation and optimization
239    ///
240    /// This method performs both common validation (length, characters) and
241    /// domain-specific validation according to the key's domain type. It
242    /// automatically chooses the optimal creation path based on the input
243    /// characteristics and domain configuration.
244    ///
245    /// # Arguments
246    ///
247    /// * `key` - String-like input that will be normalized and validated
248    ///
249    /// # Returns
250    ///
251    /// * `Ok(Key<T>)` if the key is valid
252    /// * `Err(KeyParseError)` with the specific validation failure
253    ///
254    /// # Errors
255    ///
256    /// Returns `KeyParseError` if the key fails common validation (empty, too
257    /// long, invalid characters) or domain-specific validation rules
258    ///
259    /// # Examples
260    ///
261    /// ```rust
262    /// use domain_key::{Key, Domain, KeyDomain};
263    ///
264    /// #[derive(Debug)]
265    /// struct TestDomain;
266    /// impl Domain for TestDomain {
267    ///     const DOMAIN_NAME: &'static str = "test";
268    /// }
269    /// impl KeyDomain for TestDomain {}
270    /// type TestKey = Key<TestDomain>;
271    ///
272    /// let key = TestKey::new("valid_key")?;
273    /// assert_eq!(key.as_str(), "valid_key");
274    ///
275    /// // Invalid keys return descriptive errors
276    /// let error = TestKey::new("").unwrap_err();
277    /// assert!(matches!(error, domain_key::KeyParseError::Empty));
278    /// # Ok::<(), domain_key::KeyParseError>(())
279    /// ```
280    #[inline]
281    pub fn new(key: impl AsRef<str>) -> Result<Self, KeyParseError> {
282        let key_str = key.as_ref();
283        Self::new_optimized(key_str)
284    }
285
286    /// Optimized implementation for key creation
287    ///
288    /// This method uses performance optimizations when available:
289    /// - Stack allocation for short keys
290    /// - Fast validation paths
291    /// - Cached operations
292    ///
293    /// # Errors
294    ///
295    /// Returns `KeyParseError` if the constructed key fails validation
296    fn new_optimized(key: &str) -> Result<Self, KeyParseError> {
297        // Step 1: Reject truly empty input before normalization; whitespace-only
298        // strings are handled by normalize() + validate_common() below.
299        if key.is_empty() {
300            return Err(KeyParseError::Empty);
301        }
302
303        // Step 2: Normalization (trimming, lowercasing, domain-specific)
304        let normalized = Self::normalize(key);
305
306        // Step 3: Common validation on the normalized result
307        Self::validate_common(&normalized)?;
308
309        // Step 4: Domain-specific validation
310        T::validate_domain_rules(&normalized).map_err(Self::fix_domain_error)?;
311
312        // Step 5: Hash computation and storage
313        let hash = Self::compute_hash(&normalized);
314
315        Ok(Self {
316            inner: SmartString::from(normalized.as_ref()),
317            hash,
318            _marker: PhantomData,
319        })
320    }
321
322    /// Creates a new key from an owned String with optimized handling
323    ///
324    /// This method is more efficient when you already have a `String` as it
325    /// can reuse the allocation when possible.
326    ///
327    /// # Arguments
328    ///
329    /// * `key` - Owned string that will be normalized and validated
330    ///
331    /// # Errors
332    ///
333    /// Returns `KeyParseError` if the key fails validation
334    ///
335    /// # Examples
336    ///
337    /// ```rust
338    /// use domain_key::{Key, Domain, KeyDomain};
339    ///
340    /// #[derive(Debug)]
341    /// struct TestDomain;
342    /// impl Domain for TestDomain {
343    ///     const DOMAIN_NAME: &'static str = "test";
344    /// }
345    /// impl KeyDomain for TestDomain {}
346    /// type TestKey = Key<TestDomain>;
347    ///
348    /// let key_string = "test_key".to_string();
349    /// let key = TestKey::from_string(key_string)?;
350    /// assert_eq!(key.as_str(), "test_key");
351    /// # Ok::<(), domain_key::KeyParseError>(())
352    /// ```
353    pub fn from_string(key: String) -> Result<Self, KeyParseError> {
354        // Reject obviously empty input before normalization
355        if key.trim().is_empty() {
356            return Err(KeyParseError::Empty);
357        }
358
359        // Normalize efficiently, reusing allocation when possible
360        let normalized = Self::normalize_owned(key);
361
362        // Validate the normalized result
363        Self::validate_common(&normalized)?;
364
365        // Domain validation
366        T::validate_domain_rules(&normalized).map_err(Self::fix_domain_error)?;
367
368        let hash = Self::compute_hash(&normalized);
369
370        Ok(Self {
371            inner: SmartString::from(normalized),
372            hash,
373            _marker: PhantomData,
374        })
375    }
376
377    /// Create a key from multiple parts separated by a delimiter
378    ///
379    /// This method efficiently constructs a key from multiple string parts,
380    /// using pre-calculated sizing to minimize allocations.
381    ///
382    /// # Arguments
383    ///
384    /// * `parts` - Array of string parts to join
385    /// * `delimiter` - String to use as separator between parts
386    ///
387    /// # Returns
388    ///
389    /// * `Ok(Key<T>)` if the constructed key is valid
390    /// * `Err(KeyParseError)` if validation fails
391    ///
392    /// # Examples
393    ///
394    /// ```rust
395    /// use domain_key::{Key, Domain, KeyDomain};
396    ///
397    /// #[derive(Debug)]
398    /// struct TestDomain;
399    /// impl Domain for TestDomain {
400    ///     const DOMAIN_NAME: &'static str = "test";
401    /// }
402    /// impl KeyDomain for TestDomain {}
403    /// type TestKey = Key<TestDomain>;
404    ///
405    /// let key = TestKey::from_parts(&["user", "123", "profile"], "_")?;
406    /// assert_eq!(key.as_str(), "user_123_profile");
407    /// # Ok::<(), domain_key::KeyParseError>(())
408    /// ```
409    /// # Errors
410    ///
411    /// Returns `KeyParseError` if the constructed key fails validation
412    pub fn from_parts(parts: &[&str], delimiter: &str) -> Result<Self, KeyParseError> {
413        if parts.is_empty() {
414            return Err(KeyParseError::Empty);
415        }
416
417        if parts.iter().any(|part| part.is_empty()) {
418            return Err(KeyParseError::InvalidStructure {
419                reason: "Parts cannot contain empty strings",
420            });
421        }
422
423        let joined = parts.join(delimiter);
424
425        if joined.is_empty() {
426            return Err(KeyParseError::Empty);
427        }
428
429        Self::from_string(joined)
430    }
431
432    /// Try to create a key from multiple parts, returning None on failure
433    ///
434    /// This is a convenience method for when you want to handle validation
435    /// failures by ignoring invalid keys rather than handling errors.
436    ///
437    /// # Examples
438    ///
439    /// ```rust
440    /// use domain_key::{Key, Domain, KeyDomain};
441    ///
442    /// #[derive(Debug)]
443    /// struct TestDomain;
444    /// impl Domain for TestDomain {
445    ///     const DOMAIN_NAME: &'static str = "test";
446    /// }
447    /// impl KeyDomain for TestDomain {}
448    /// type TestKey = Key<TestDomain>;
449    ///
450    /// let valid = TestKey::try_from_parts(&["user", "123"], "_").unwrap();
451    /// let invalid = TestKey::try_from_parts(&["", ""], "_"); // Returns None
452    /// assert!(invalid.is_none());
453    /// ```
454    #[must_use]
455    pub fn try_from_parts(parts: &[&str], delimiter: &str) -> Option<Self> {
456        Self::from_parts(parts, delimiter).ok()
457    }
458
459    /// Creates a key from a static string without runtime validation
460    ///
461    /// # Warning
462    ///
463    /// The caller must ensure that the static string follows all validation
464    /// rules for the domain (allowed characters, length limits, normalization,
465    /// domain-specific rules). Invalid keys created this way will violate
466    /// internal invariants and may cause unexpected behavior.
467    ///
468    /// Prefer [`try_from_static`](Self::try_from_static) or the [`static_key!`](macro@crate::static_key) macro
469    /// for safe creation of static keys.
470    ///
471    /// # Panics
472    ///
473    /// Panics in debug builds if the key is empty or exceeds `T::MAX_LENGTH`.
474    ///
475    /// # Arguments
476    ///
477    /// * `key` - A static string literal that represents a valid key
478    ///
479    /// # Examples
480    ///
481    /// ```rust
482    /// use domain_key::{Key, Domain, KeyDomain};
483    ///
484    /// #[derive(Debug)]
485    /// struct TestDomain;
486    /// impl Domain for TestDomain {
487    ///     const DOMAIN_NAME: &'static str = "test";
488    /// }
489    /// impl KeyDomain for TestDomain {}
490    /// type TestKey = Key<TestDomain>;
491    ///
492    /// let key = TestKey::from_static_unchecked("static_key");
493    /// assert_eq!(key.as_str(), "static_key");
494    /// ```
495    #[must_use]
496    pub fn from_static_unchecked(key: &'static str) -> Self {
497        debug_assert!(
498            !key.is_empty(),
499            "from_static_unchecked: key must not be empty"
500        );
501        debug_assert!(
502            key.len() <= T::MAX_LENGTH,
503            "from_static_unchecked: key length {} exceeds domain max {}",
504            key.len(),
505            T::MAX_LENGTH
506        );
507
508        let hash = Self::compute_hash(key);
509
510        Self {
511            inner: SmartString::from(key),
512            hash,
513            _marker: PhantomData,
514        }
515    }
516
517    /// Creates a key from a static string with validation
518    ///
519    /// This is a safer alternative to `from_static_unchecked` that validates
520    /// the key at runtime. The validation cost is paid once, and subsequent
521    /// uses of the key are as fast as the unchecked version.
522    ///
523    /// # Arguments
524    ///
525    /// * `key` - A static string literal to validate and convert
526    ///
527    /// # Errors
528    ///
529    /// Returns `KeyParseError` if the static key fails validation
530    ///
531    /// # Examples
532    ///
533    /// ```rust
534    /// use domain_key::{Key, Domain, KeyDomain};
535    ///
536    /// #[derive(Debug)]
537    /// struct TestDomain;
538    /// impl Domain for TestDomain {
539    ///     const DOMAIN_NAME: &'static str = "test";
540    /// }
541    /// impl KeyDomain for TestDomain {}
542    /// type TestKey = Key<TestDomain>;
543    ///
544    /// let key = TestKey::try_from_static("static_key")?;
545    /// assert_eq!(key.as_str(), "static_key");
546    ///
547    /// let invalid = TestKey::try_from_static("");
548    /// assert!(invalid.is_err());
549    /// # Ok::<(), domain_key::KeyParseError>(())
550    /// ```
551    /// # Errors
552    ///
553    /// Returns `KeyParseError` if the constructed key fails validation
554    pub fn try_from_static(key: &'static str) -> Result<Self, KeyParseError> {
555        // Validate and create via the normal path
556        Self::new(key)
557    }
558
559    /// Try to create a key, returning None on validation failure
560    ///
561    /// This is a convenience method for when you want to handle validation
562    /// failures by ignoring invalid keys rather than handling errors.
563    ///
564    /// # Examples
565    ///
566    /// ```rust
567    /// use domain_key::{Key, Domain, KeyDomain};
568    ///
569    /// #[derive(Debug)]
570    /// struct TestDomain;
571    /// impl Domain for TestDomain {
572    ///     const DOMAIN_NAME: &'static str = "test";
573    /// }
574    /// impl KeyDomain for TestDomain {}
575    /// type TestKey = Key<TestDomain>;
576    ///
577    /// let valid = TestKey::try_new("valid_key").unwrap();
578    /// let invalid = TestKey::try_new(""); // Returns None
579    /// assert!(invalid.is_none());
580    /// ```
581    #[inline]
582    pub fn try_new(key: impl AsRef<str>) -> Option<Self> {
583        Self::new(key).ok()
584    }
585}
586
587// ============================================================================
588// KEY IMPLEMENTATION - ACCESSOR METHODS
589// ============================================================================
590
591impl<T: KeyDomain> Key<T> {
592    /// Returns the key as a string slice
593    ///
594    /// This is the primary way to access the string content of a key.
595    /// The returned reference is valid for the lifetime of the key.
596    ///
597    /// # Examples
598    ///
599    /// ```rust
600    /// use domain_key::{Key, Domain, KeyDomain};
601    ///
602    /// #[derive(Debug)]
603    /// struct TestDomain;
604    /// impl Domain for TestDomain {
605    ///     const DOMAIN_NAME: &'static str = "test";
606    /// }
607    /// impl KeyDomain for TestDomain {}
608    /// type TestKey = Key<TestDomain>;
609    ///
610    /// let key = TestKey::new("example")?;
611    /// assert_eq!(key.as_str(), "example");
612    /// # Ok::<(), domain_key::KeyParseError>(())
613    /// ```
614    #[inline]
615    #[must_use]
616    pub fn as_str(&self) -> &str {
617        &self.inner
618    }
619
620    /// Returns the domain name for this key type
621    ///
622    /// This is a compile-time constant that identifies which domain
623    /// this key belongs to.
624    ///
625    /// # Examples
626    ///
627    /// ```rust
628    /// use domain_key::{Key, Domain, KeyDomain};
629    ///
630    /// #[derive(Debug)]
631    /// struct UserDomain;
632    /// impl Domain for UserDomain {
633    ///     const DOMAIN_NAME: &'static str = "user";
634    /// }
635    /// impl KeyDomain for UserDomain {}
636    /// type UserKey = Key<UserDomain>;
637    ///
638    /// let key = UserKey::new("john")?;
639    /// assert_eq!(key.domain(), "user");
640    /// # Ok::<(), domain_key::KeyParseError>(())
641    /// ```
642    #[inline]
643    #[must_use]
644    pub const fn domain(&self) -> &'static str {
645        T::DOMAIN_NAME
646    }
647
648    /// Returns the length of the key string in bytes
649    ///
650    /// This is an O(1) operation — `SmartString` stores the length inline.
651    ///
652    /// # Examples
653    ///
654    /// ```rust
655    /// use domain_key::{Key, Domain, KeyDomain};
656    ///
657    /// #[derive(Debug)]
658    /// struct TestDomain;
659    /// impl Domain for TestDomain {
660    ///     const DOMAIN_NAME: &'static str = "test";
661    /// }
662    /// impl KeyDomain for TestDomain {}
663    /// type TestKey = Key<TestDomain>;
664    ///
665    /// let key = TestKey::new("example")?;
666    /// assert_eq!(key.len(), 7);
667    /// # Ok::<(), domain_key::KeyParseError>(())
668    /// ```
669    #[inline]
670    #[must_use]
671    pub fn len(&self) -> usize {
672        self.inner.len()
673    }
674
675    /// Returns true if the key is empty (this should never happen for valid keys)
676    ///
677    /// Since empty keys are rejected during validation, this method should
678    /// always return `false` for properly constructed keys. It's provided
679    /// for completeness and debugging purposes.
680    ///
681    /// # Examples
682    ///
683    /// ```rust
684    /// use domain_key::{Key, Domain, KeyDomain};
685    ///
686    /// #[derive(Debug)]
687    /// struct TestDomain;
688    /// impl Domain for TestDomain {
689    ///     const DOMAIN_NAME: &'static str = "test";
690    /// }
691    /// impl KeyDomain for TestDomain {}
692    /// type TestKey = Key<TestDomain>;
693    ///
694    /// let key = TestKey::new("example")?;
695    /// assert!(!key.is_empty());
696    /// # Ok::<(), domain_key::KeyParseError>(())
697    /// ```
698    #[inline]
699    #[must_use]
700    pub fn is_empty(&self) -> bool {
701        self.inner.is_empty()
702    }
703
704    /// Returns the pre-computed hash value
705    ///
706    /// This hash is computed once during key creation using the
707    /// feature-selected algorithm (gxhash / ahash / blake3 / fnv-1a)
708    /// and cached for the lifetime of the key.
709    ///
710    /// **Important:** This is *not* the hash used by [`Hash`] trait /
711    /// `HashMap`.  The `Hash` trait delegates to `str`'s implementation
712    /// so that `Borrow<str>` works correctly.  Use this method when you
713    /// need a deterministic, feature-dependent hash for your own data
714    /// structures or protocols.
715    ///
716    /// **Note:** The hash algorithm depends on the active feature flags
717    /// (`fast`, `secure`, `crypto`, or the default hasher). Keys created
718    /// with different feature configurations will produce different hash
719    /// values. Do not persist or compare hash values across builds with
720    /// different features.
721    ///
722    /// # Examples
723    ///
724    /// ```rust
725    /// use domain_key::{Key, Domain, KeyDomain};
726    ///
727    /// #[derive(Debug)]
728    /// struct TestDomain;
729    /// impl Domain for TestDomain {
730    ///     const DOMAIN_NAME: &'static str = "test";
731    /// }
732    /// impl KeyDomain for TestDomain {}
733    /// type TestKey = Key<TestDomain>;
734    ///
735    /// let key1 = TestKey::new("example")?;
736    /// let key2 = TestKey::new("example")?;
737    /// let key3 = TestKey::new("different")?;
738    ///
739    /// // Same keys have same hash
740    /// assert_eq!(key1.hash(), key2.hash());
741    /// // Different keys have different hashes (with high probability)
742    /// assert_ne!(key1.hash(), key3.hash());
743    /// # Ok::<(), domain_key::KeyParseError>(())
744    /// ```
745    #[inline]
746    #[must_use]
747    pub const fn hash(&self) -> u64 {
748        self.hash
749    }
750
751    /// Checks if this key starts with the given prefix
752    ///
753    /// This is a simple string prefix check that can be useful for
754    /// categorizing or filtering keys.
755    ///
756    /// # Arguments
757    ///
758    /// * `prefix` - The prefix string to check for
759    ///
760    /// # Examples
761    ///
762    /// ```rust
763    /// use domain_key::{Key, Domain, KeyDomain};
764    ///
765    /// #[derive(Debug)]
766    /// struct TestDomain;
767    /// impl Domain for TestDomain {
768    ///     const DOMAIN_NAME: &'static str = "test";
769    /// }
770    /// impl KeyDomain for TestDomain {}
771    /// type TestKey = Key<TestDomain>;
772    ///
773    /// let key = TestKey::new("user_profile")?;
774    /// assert!(key.starts_with("user_"));
775    /// assert!(!key.starts_with("admin_"));
776    /// # Ok::<(), domain_key::KeyParseError>(())
777    /// ```
778    #[inline]
779    #[must_use]
780    pub fn starts_with(&self, prefix: &str) -> bool {
781        self.inner.starts_with(prefix)
782    }
783
784    /// Checks if this key ends with the given suffix
785    ///
786    /// This is a simple string suffix check that can be useful for
787    /// categorizing or filtering keys.
788    ///
789    /// # Arguments
790    ///
791    /// * `suffix` - The suffix string to check for
792    ///
793    /// # Examples
794    ///
795    /// ```rust
796    /// use domain_key::{Key, Domain, KeyDomain};
797    ///
798    /// #[derive(Debug)]
799    /// struct TestDomain;
800    /// impl Domain for TestDomain {
801    ///     const DOMAIN_NAME: &'static str = "test";
802    /// }
803    /// impl KeyDomain for TestDomain {}
804    /// type TestKey = Key<TestDomain>;
805    ///
806    /// let key = TestKey::new("user_profile")?;
807    /// assert!(key.ends_with("_profile"));
808    /// assert!(!key.ends_with("_settings"));
809    /// # Ok::<(), domain_key::KeyParseError>(())
810    /// ```
811    #[inline]
812    #[must_use]
813    pub fn ends_with(&self, suffix: &str) -> bool {
814        self.inner.ends_with(suffix)
815    }
816
817    /// Checks if this key contains the given substring
818    ///
819    /// This performs a substring search within the key.
820    ///
821    /// # Arguments
822    ///
823    /// * `pattern` - The substring to search for
824    ///
825    /// # Examples
826    ///
827    /// ```rust
828    /// use domain_key::{Key, Domain, KeyDomain};
829    ///
830    /// #[derive(Debug)]
831    /// struct TestDomain;
832    /// impl Domain for TestDomain {
833    ///     const DOMAIN_NAME: &'static str = "test";
834    /// }
835    /// impl KeyDomain for TestDomain {}
836    /// type TestKey = Key<TestDomain>;
837    ///
838    /// let key = TestKey::new("user_profile_settings")?;
839    /// assert!(key.contains("profile"));
840    /// assert!(!key.contains("admin"));
841    /// # Ok::<(), domain_key::KeyParseError>(())
842    /// ```
843    #[inline]
844    #[must_use]
845    pub fn contains(&self, pattern: &str) -> bool {
846        self.inner.contains(pattern)
847    }
848
849    /// Returns an iterator over the characters of the key
850    ///
851    /// This provides access to individual characters in the key string.
852    ///
853    /// # Examples
854    ///
855    /// ```rust
856    /// use domain_key::{Key, Domain, KeyDomain};
857    ///
858    /// #[derive(Debug)]
859    /// struct TestDomain;
860    /// impl Domain for TestDomain {
861    ///     const DOMAIN_NAME: &'static str = "test";
862    /// }
863    /// impl KeyDomain for TestDomain {}
864    /// type TestKey = Key<TestDomain>;
865    ///
866    /// let key = TestKey::new("abc")?;
867    /// let chars: Vec<char> = key.chars().collect();
868    /// assert_eq!(chars, vec!['a', 'b', 'c']);
869    /// # Ok::<(), domain_key::KeyParseError>(())
870    /// ```
871    #[inline]
872    pub fn chars(&self) -> core::str::Chars<'_> {
873        self.inner.chars()
874    }
875
876    /// Splits the key by a delimiter and returns an iterator
877    ///
878    /// This method provides consistent split functionality.
879    ///
880    /// # Arguments
881    ///
882    /// * `delimiter` - Character to split on
883    ///
884    /// # Examples
885    ///
886    /// ```rust
887    /// use domain_key::{Key, Domain, KeyDomain};
888    ///
889    /// #[derive(Debug)]
890    /// struct TestDomain;
891    /// impl Domain for TestDomain {
892    ///     const DOMAIN_NAME: &'static str = "test";
893    /// }
894    /// impl KeyDomain for TestDomain {}
895    /// type TestKey = Key<TestDomain>;
896    ///
897    /// let key = TestKey::new("user_profile_settings")?;
898    /// let parts: Vec<&str> = key.split('_').collect();
899    /// assert_eq!(parts, vec!["user", "profile", "settings"]);
900    /// # Ok::<(), domain_key::KeyParseError>(())
901    /// ```
902    #[must_use]
903    pub fn split(&self, delimiter: char) -> SplitIterator<'_> {
904        SplitIterator(utils::new_split_cache(&self.inner, delimiter))
905    }
906
907    /// Split operation for consistent API
908    ///
909    /// This method provides the same functionality as `split()` but with explicit naming
910    /// for cases where caching behavior needs to be clear.
911    #[must_use]
912    pub fn split_cached(&self, delimiter: char) -> SplitCache<'_> {
913        utils::new_split_cache(&self.inner, delimiter)
914    }
915
916    /// Splits the key by a string delimiter and returns an iterator
917    ///
918    /// This method splits the key using a string pattern rather than a single character.
919    ///
920    /// # Examples
921    ///
922    /// ```rust
923    /// use domain_key::{Key, Domain, KeyDomain};
924    ///
925    /// #[derive(Debug)]
926    /// struct TestDomain;
927    /// impl Domain for TestDomain {
928    ///     const DOMAIN_NAME: &'static str = "test";
929    /// }
930    /// impl KeyDomain for TestDomain {}
931    /// type TestKey = Key<TestDomain>;
932    ///
933    /// let key = TestKey::new("user-and-profile-and-settings")?;
934    /// let parts: Vec<&str> = key.split_str("-and-").collect();
935    /// assert_eq!(parts, vec!["user", "profile", "settings"]);
936    /// # Ok::<(), domain_key::KeyParseError>(())
937    /// ```
938    #[must_use]
939    pub fn split_str<'a>(&'a self, delimiter: &'a str) -> core::str::Split<'a, &'a str> {
940        self.inner.split(delimiter)
941    }
942
943    /// Returns the key with a prefix if it doesn't already have it
944    ///
945    /// This method efficiently adds a prefix to a key if it doesn't already
946    /// start with that prefix.
947    ///
948    /// # Arguments
949    ///
950    /// * `prefix` - The prefix to ensure is present
951    ///
952    /// # Examples
953    ///
954    /// ```rust
955    /// use domain_key::{Key, Domain, KeyDomain};
956    ///
957    /// #[derive(Debug)]
958    /// struct TestDomain;
959    /// impl Domain for TestDomain {
960    ///     const DOMAIN_NAME: &'static str = "test";
961    /// }
962    /// impl KeyDomain for TestDomain {}
963    /// type TestKey = Key<TestDomain>;
964    ///
965    /// let key = TestKey::new("profile")?;
966    /// let prefixed = key.ensure_prefix("user_")?;
967    /// assert_eq!(prefixed.as_str(), "user_profile");
968    ///
969    /// // If prefix already exists, returns the same key
970    /// let already_prefixed = prefixed.ensure_prefix("user_")?;
971    /// assert_eq!(already_prefixed.as_str(), "user_profile");
972    /// # Ok::<(), domain_key::KeyParseError>(())
973    /// ```
974    /// # Errors
975    ///
976    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
977    pub fn ensure_prefix(&self, prefix: &str) -> Result<Self, KeyParseError> {
978        if self.starts_with(prefix) {
979            return Ok(self.clone());
980        }
981
982        let new_len = prefix.len() + self.len();
983        if new_len > T::MAX_LENGTH {
984            return Err(KeyParseError::TooLong {
985                max_length: T::MAX_LENGTH,
986                actual_length: new_len,
987            });
988        }
989
990        let result = utils::add_prefix_optimized(&self.inner, prefix);
991
992        // Full structural validation (start char, end char, consecutive chars at junction)
993        Self::validate_common(&result)?;
994
995        T::validate_domain_rules(&result).map_err(Self::fix_domain_error)?;
996
997        let hash = Self::compute_hash(&result);
998
999        Ok(Self {
1000            inner: result,
1001            hash,
1002            _marker: PhantomData,
1003        })
1004    }
1005
1006    /// Returns the key with a suffix if it doesn't already have it
1007    ///
1008    /// This method efficiently adds a suffix to a key if it doesn't already
1009    /// end with that suffix.
1010    ///
1011    /// # Arguments
1012    ///
1013    /// * `suffix` - The suffix to ensure is present
1014    ///
1015    /// # Examples
1016    ///
1017    /// ```rust
1018    /// use domain_key::{Key, Domain, KeyDomain};
1019    ///
1020    /// #[derive(Debug)]
1021    /// struct TestDomain;
1022    /// impl Domain for TestDomain {
1023    ///     const DOMAIN_NAME: &'static str = "test";
1024    /// }
1025    /// impl KeyDomain for TestDomain {}
1026    /// type TestKey = Key<TestDomain>;
1027    ///
1028    /// let key = TestKey::new("user")?;
1029    /// let suffixed = key.ensure_suffix("_profile")?;
1030    /// assert_eq!(suffixed.as_str(), "user_profile");
1031    ///
1032    /// // If suffix already exists, returns the same key
1033    /// let already_suffixed = suffixed.ensure_suffix("_profile")?;
1034    /// assert_eq!(already_suffixed.as_str(), "user_profile");
1035    /// # Ok::<(), domain_key::KeyParseError>(())
1036    /// ```
1037    /// # Errors
1038    ///
1039    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
1040    pub fn ensure_suffix(&self, suffix: &str) -> Result<Self, KeyParseError> {
1041        if self.ends_with(suffix) {
1042            return Ok(self.clone());
1043        }
1044
1045        let new_len = self.len() + suffix.len();
1046        if new_len > T::MAX_LENGTH {
1047            return Err(KeyParseError::TooLong {
1048                max_length: T::MAX_LENGTH,
1049                actual_length: new_len,
1050            });
1051        }
1052
1053        let result = utils::add_suffix_optimized(&self.inner, suffix);
1054
1055        // Full structural validation (start char, end char, consecutive chars at junction)
1056        Self::validate_common(&result)?;
1057
1058        T::validate_domain_rules(&result).map_err(Self::fix_domain_error)?;
1059
1060        let hash = Self::compute_hash(&result);
1061
1062        Ok(Self {
1063            inner: result,
1064            hash,
1065            _marker: PhantomData,
1066        })
1067    }
1068
1069    /// Get validation rules that this key satisfies
1070    ///
1071    /// Returns detailed information about the validation characteristics
1072    /// of this key and its domain, useful for debugging and introspection.
1073    ///
1074    /// # Examples
1075    ///
1076    /// ```rust
1077    /// use domain_key::{Key, Domain, KeyDomain};
1078    ///
1079    /// #[derive(Debug)]
1080    /// struct TestDomain;
1081    /// impl Domain for TestDomain {
1082    ///     const DOMAIN_NAME: &'static str = "test";
1083    /// }
1084    /// impl KeyDomain for TestDomain {
1085    ///     const MAX_LENGTH: usize = 32;
1086    ///     const HAS_CUSTOM_VALIDATION: bool = true;
1087    /// }
1088    /// type TestKey = Key<TestDomain>;
1089    ///
1090    /// let key = TestKey::new("example")?;
1091    /// let info = key.validation_info();
1092    ///
1093    /// assert_eq!(info.domain_info.name, "test");
1094    /// assert_eq!(info.domain_info.max_length, 32);
1095    /// assert_eq!(info.length, 7);
1096    /// assert!(info.domain_info.has_custom_validation);
1097    /// # Ok::<(), domain_key::KeyParseError>(())
1098    /// ```
1099    #[must_use]
1100    pub fn validation_info(&self) -> KeyValidationInfo {
1101        KeyValidationInfo {
1102            domain_info: crate::domain::domain_info::<T>(),
1103            length: self.len(),
1104        }
1105    }
1106}
1107
1108// ============================================================================
1109// KEY IMPLEMENTATION - HELPER METHODS
1110// ============================================================================
1111
1112impl<T: KeyDomain> Key<T> {
1113    /// Fix domain name in domain validation errors
1114    ///
1115    /// This helper ensures that domain validation errors have the correct
1116    /// domain name, even when they're created generically.
1117    #[inline]
1118    fn fix_domain_error(e: KeyParseError) -> KeyParseError {
1119        match e {
1120            KeyParseError::DomainValidation { message, .. } => KeyParseError::DomainValidation {
1121                domain: T::DOMAIN_NAME,
1122                message,
1123            },
1124            other => other,
1125        }
1126    }
1127
1128    /// Common validation pipeline
1129    ///
1130    /// Performs validation that's common to all domains: length checking,
1131    /// character validation, and structural validation.
1132    ///
1133    /// # Errors
1134    ///
1135    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
1136    pub(crate) fn validate_common(key: &str) -> Result<(), KeyParseError> {
1137        // The caller (new_optimized / from_string) has already normalized
1138        // the key (which includes trimming), so we validate `key` directly
1139        // without an extra .trim() pass.
1140
1141        if key.is_empty() {
1142            return Err(KeyParseError::Empty);
1143        }
1144
1145        if key.len() > T::MAX_LENGTH {
1146            return Err(KeyParseError::TooLong {
1147                max_length: T::MAX_LENGTH,
1148                actual_length: key.len(),
1149            });
1150        }
1151
1152        if key.len() < T::min_length() {
1153            return Err(KeyParseError::TooShort {
1154                min_length: T::min_length(),
1155                actual_length: key.len(),
1156            });
1157        }
1158
1159        // Use fast validation
1160        Self::validate_fast(key)
1161    }
1162
1163    /// Fast validation path using optimized algorithms
1164    /// # Errors
1165    ///
1166    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
1167    fn validate_fast(key: &str) -> Result<(), KeyParseError> {
1168        let mut chars = key.char_indices();
1169        let mut prev_char = None;
1170
1171        // Validate first character
1172        if let Some((pos, first)) = chars.next() {
1173            let char_allowed = crate::utils::char_validation::is_key_char_fast(first)
1174                || T::allowed_start_character(first);
1175
1176            if !char_allowed {
1177                return Err(KeyParseError::InvalidCharacter {
1178                    character: first,
1179                    position: pos,
1180                    expected: Some("allowed by domain"),
1181                });
1182            }
1183
1184            prev_char = Some(first);
1185        }
1186
1187        // Validate remaining characters
1188        for (pos, c) in chars {
1189            let char_allowed = T::allowed_characters(c);
1190
1191            if !char_allowed {
1192                return Err(KeyParseError::InvalidCharacter {
1193                    character: c,
1194                    position: pos,
1195                    expected: Some("allowed by domain"),
1196                });
1197            }
1198
1199            if let Some(prev) = prev_char {
1200                if !T::allowed_consecutive_characters(prev, c) {
1201                    return Err(KeyParseError::InvalidStructure {
1202                        reason: "consecutive characters not allowed",
1203                    });
1204                }
1205            }
1206            prev_char = Some(c);
1207        }
1208
1209        // Check last character
1210        if let Some(last) = prev_char {
1211            if !T::allowed_end_character(last) {
1212                return Err(KeyParseError::InvalidStructure {
1213                    reason: "invalid end character",
1214                });
1215            }
1216        }
1217
1218        Ok(())
1219    }
1220
1221    /// Normalize a borrowed string
1222    pub(crate) fn normalize(key: &str) -> Cow<'_, str> {
1223        let trimmed = key.trim();
1224
1225        let needs_lowercase =
1226            T::CASE_INSENSITIVE && trimmed.chars().any(|c| c.is_ascii_uppercase());
1227
1228        let base = if needs_lowercase {
1229            Cow::Owned(trimmed.to_ascii_lowercase())
1230        } else {
1231            // Borrow the trimmed slice — no allocation needed
1232            Cow::Borrowed(trimmed)
1233        };
1234
1235        // Apply domain-specific normalization
1236        T::normalize_domain(base)
1237    }
1238
1239    /// Normalize an owned string efficiently
1240    fn normalize_owned(mut key: String) -> String {
1241        // In-place trim: remove leading whitespace by draining, then truncate trailing
1242        let start = key.len() - key.trim_start().len();
1243        if start > 0 {
1244            key.drain(..start);
1245        }
1246        let trimmed_len = key.trim_end().len();
1247        key.truncate(trimmed_len);
1248
1249        if T::CASE_INSENSITIVE {
1250            key.make_ascii_lowercase();
1251        }
1252
1253        // Apply domain normalization
1254        match T::normalize_domain(Cow::Owned(key)) {
1255            Cow::Owned(s) => s,
1256            Cow::Borrowed(s) => s.to_owned(),
1257        }
1258    }
1259
1260    /// Compute hash using the configured algorithm
1261    ///
1262    /// The hash algorithm is selected at compile time based on feature flags,
1263    /// allowing for different performance/security trade-offs.
1264    pub(crate) fn compute_hash(key: &str) -> u64 {
1265        if key.is_empty() {
1266            return 0;
1267        }
1268
1269        Self::compute_hash_inner(key.as_bytes())
1270    }
1271
1272    /// Inner hash computation dispatched by feature flags
1273    ///
1274    /// Separated to keep each cfg branch as the sole return path,
1275    /// avoiding mixed `return` statements and dead-code warnings.
1276    fn compute_hash_inner(bytes: &[u8]) -> u64 {
1277        // Priority: fast > secure > crypto > default
1278
1279        #[cfg(feature = "fast")]
1280        {
1281            #[cfg(any(
1282                all(target_arch = "x86_64", target_feature = "aes"),
1283                all(
1284                    target_arch = "aarch64",
1285                    target_feature = "aes",
1286                    target_feature = "neon"
1287                )
1288            ))]
1289            {
1290                gxhash::gxhash64(bytes, 0)
1291            }
1292
1293            #[cfg(not(any(
1294                all(target_arch = "x86_64", target_feature = "aes"),
1295                all(
1296                    target_arch = "aarch64",
1297                    target_feature = "aes",
1298                    target_feature = "neon"
1299                )
1300            )))]
1301            {
1302                use core::hash::Hasher;
1303                let mut hasher = ahash::AHasher::default();
1304                hasher.write(bytes);
1305                hasher.finish()
1306            }
1307        }
1308
1309        #[cfg(all(feature = "secure", not(feature = "fast")))]
1310        {
1311            use core::hash::Hasher;
1312            let mut hasher = ahash::AHasher::default();
1313            hasher.write(bytes);
1314            hasher.finish()
1315        }
1316
1317        #[cfg(all(feature = "crypto", not(any(feature = "fast", feature = "secure"))))]
1318        {
1319            let hash = blake3::hash(bytes);
1320            let h = hash.as_bytes();
1321            u64::from_le_bytes([h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]])
1322        }
1323
1324        #[cfg(not(any(feature = "fast", feature = "secure", feature = "crypto")))]
1325        {
1326            #[cfg(feature = "std")]
1327            {
1328                use core::hash::Hasher;
1329                use std::collections::hash_map::DefaultHasher;
1330                let mut hasher = DefaultHasher::new();
1331                hasher.write(bytes);
1332                hasher.finish()
1333            }
1334
1335            #[cfg(not(feature = "std"))]
1336            {
1337                Self::fnv1a_hash(bytes)
1338            }
1339        }
1340    }
1341
1342    /// FNV-1a hash implementation for `no_std` environments
1343    #[expect(
1344        dead_code,
1345        reason = "fallback hash used only when no hash feature is enabled"
1346    )]
1347    fn fnv1a_hash(bytes: &[u8]) -> u64 {
1348        const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
1349        const FNV_PRIME: u64 = 0x0100_0000_01b3;
1350
1351        let mut hash = FNV_OFFSET_BASIS;
1352        for &byte in bytes {
1353            hash ^= u64::from(byte);
1354            hash = hash.wrapping_mul(FNV_PRIME);
1355        }
1356        hash
1357    }
1358}
1359
1360// ============================================================================
1361// SUPPORTING TYPES
1362// ============================================================================
1363
1364/// Information about a key's validation characteristics
1365///
1366/// This structure provides detailed information about how a key was validated
1367/// and what domain-specific features are enabled. Domain-level configuration
1368/// is available through the embedded [`DomainInfo`](crate::DomainInfo).
1369#[derive(Debug, Clone, PartialEq, Eq)]
1370pub struct KeyValidationInfo {
1371    /// Full domain configuration
1372    pub domain_info: crate::domain::DomainInfo,
1373    /// Actual length of the key
1374    pub length: usize,
1375}
1376
1377// ============================================================================
1378// STANDARD TRAIT IMPLEMENTATIONS
1379// ============================================================================
1380
1381/// Display implementation shows the key value
1382///
1383/// Outputs just the key string, consistent with `AsRef<str>`, `From<Key<T>> for String`,
1384/// and serde serialization. Use [`Key::domain`] separately when domain context is needed.
1385impl<T: KeyDomain> fmt::Display for Key<T> {
1386    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1387        f.write_str(&self.inner)
1388    }
1389}
1390
1391/// `Deref` implementation for automatic coercion to `&str`
1392///
1393/// This allows `&key` to automatically coerce to `&str` in contexts
1394/// that expect a string slice, eliminating the need for explicit
1395/// `.as_ref()` or `.as_str()` calls in most situations.
1396///
1397/// # Examples
1398///
1399/// ```rust
1400/// use domain_key::{Key, Domain, KeyDomain};
1401///
1402/// #[derive(Debug)]
1403/// struct TestDomain;
1404/// impl Domain for TestDomain {
1405///     const DOMAIN_NAME: &'static str = "test";
1406/// }
1407/// impl KeyDomain for TestDomain {}
1408/// type TestKey = Key<TestDomain>;
1409///
1410/// let key = TestKey::new("example")?;
1411///
1412/// // Automatic coercion — no .as_ref() needed
1413/// let s: &str = &key;
1414/// assert_eq!(s, "example");
1415///
1416/// // Works with functions expecting &str
1417/// fn takes_str(_s: &str) {}
1418/// takes_str(&key);
1419/// # Ok::<(), domain_key::KeyParseError>(())
1420/// ```
1421impl<T: KeyDomain> Deref for Key<T> {
1422    type Target = str;
1423
1424    #[inline]
1425    fn deref(&self) -> &str {
1426        &self.inner
1427    }
1428}
1429
1430/// `AsRef` implementation for string conversion
1431impl<T: KeyDomain> AsRef<str> for Key<T> {
1432    #[inline]
1433    fn as_ref(&self) -> &str {
1434        self
1435    }
1436}
1437
1438/// `Borrow<str>` implementation enabling `HashMap<Key<T>, V>::get("str")`
1439///
1440/// This is sound because the [`Hash`] trait implementation for `Key<T>`
1441/// delegates to `str`'s hash, satisfying the contract
1442/// `hash(key) == hash(key.borrow())`.
1443impl<T: KeyDomain> Borrow<str> for Key<T> {
1444    #[inline]
1445    fn borrow(&self) -> &str {
1446        self
1447    }
1448}
1449
1450/// From implementation for converting to String
1451impl<T: KeyDomain> From<Key<T>> for String {
1452    fn from(key: Key<T>) -> Self {
1453        key.inner.into()
1454    }
1455}
1456
1457/// Creates a `Key` from a pre-validated [`SmartString`] **without re-validation**.
1458///
1459/// This is intended for internal or advanced usage where the caller has
1460/// already ensured that the string satisfies all domain rules (length,
1461/// allowed characters, normalization, etc.).  Hash is computed
1462/// automatically, but **no validation or normalization is performed**.
1463///
1464/// # Safety (logical)
1465///
1466/// If the string does not satisfy the domain's invariants the resulting
1467/// key will silently violate those invariants.  Prefer [`Key::new`] or
1468/// [`Key::from_string`] unless you are certain the input is valid.
1469impl<T: KeyDomain> From<SmartString> for Key<T> {
1470    #[inline]
1471    fn from(inner: SmartString) -> Self {
1472        let hash = Self::compute_hash(&inner);
1473
1474        Self {
1475            inner,
1476            hash,
1477            _marker: PhantomData,
1478        }
1479    }
1480}
1481
1482/// `TryFrom<String>` implementation for owned string conversion
1483///
1484/// This avoids re-borrowing through `&str` when you already have a `String`.
1485impl<T: KeyDomain> TryFrom<String> for Key<T> {
1486    type Error = KeyParseError;
1487
1488    fn try_from(s: String) -> Result<Self, Self::Error> {
1489        Key::from_string(s)
1490    }
1491}
1492
1493/// `TryFrom<&str>` implementation for borrowed string conversion
1494impl<T: KeyDomain> TryFrom<&str> for Key<T> {
1495    type Error = KeyParseError;
1496
1497    fn try_from(s: &str) -> Result<Self, Self::Error> {
1498        Key::new(s)
1499    }
1500}
1501
1502/// `FromStr` implementation for parsing from strings
1503impl<T: KeyDomain> FromStr for Key<T> {
1504    type Err = KeyParseError;
1505
1506    fn from_str(s: &str) -> Result<Self, Self::Err> {
1507        Key::new(s)
1508    }
1509}
1510
1511// ============================================================================
1512// TESTS
1513// ============================================================================
1514
1515#[cfg(test)]
1516mod tests {
1517    use super::*;
1518    use crate::domain::{DefaultDomain, Domain};
1519    #[cfg(not(feature = "std"))]
1520    use alloc::format;
1521    #[cfg(not(feature = "std"))]
1522    use alloc::string::ToString;
1523    #[cfg(not(feature = "std"))]
1524    use alloc::vec;
1525    #[cfg(not(feature = "std"))]
1526    use alloc::vec::Vec;
1527
1528    // Test domain
1529    #[derive(Debug)]
1530    struct TestDomain;
1531
1532    impl Domain for TestDomain {
1533        const DOMAIN_NAME: &'static str = "test";
1534    }
1535
1536    impl KeyDomain for TestDomain {
1537        const MAX_LENGTH: usize = 32;
1538        const HAS_CUSTOM_VALIDATION: bool = true;
1539        const HAS_CUSTOM_NORMALIZATION: bool = true;
1540        const CASE_INSENSITIVE: bool = true;
1541
1542        fn validate_domain_rules(key: &str) -> Result<(), KeyParseError> {
1543            if key.starts_with("invalid_") {
1544                return Err(KeyParseError::domain_error(
1545                    Self::DOMAIN_NAME,
1546                    "Keys cannot start with 'invalid_'",
1547                ));
1548            }
1549            Ok(())
1550        }
1551
1552        fn normalize_domain(key: Cow<'_, str>) -> Cow<'_, str> {
1553            if key.contains('-') {
1554                Cow::Owned(key.replace('-', "_"))
1555            } else {
1556                key
1557            }
1558        }
1559
1560        fn allowed_characters(c: char) -> bool {
1561            c.is_ascii_alphanumeric() || c == '_' || c == '-'
1562        }
1563
1564        fn validation_help() -> Option<&'static str> {
1565            Some("Use alphanumeric characters, underscores, and hyphens. Cannot start with 'invalid_'.")
1566        }
1567    }
1568
1569    type TestKey = Key<TestDomain>;
1570
1571    #[test]
1572    fn new_key_stores_value_and_domain() {
1573        let key = TestKey::new("valid_key").unwrap();
1574        assert_eq!(key.as_str(), "valid_key");
1575        assert_eq!(key.domain(), "test");
1576        assert_eq!(key.len(), 9);
1577    }
1578
1579    #[test]
1580    fn case_insensitive_domain_lowercases_and_normalizes() {
1581        let key = TestKey::new("Test-Key").unwrap();
1582        assert_eq!(key.as_str(), "test_key");
1583    }
1584
1585    #[test]
1586    fn domain_rules_reject_invalid_prefix() {
1587        let result = TestKey::new("invalid_key");
1588        assert!(result.is_err());
1589
1590        if let Err(KeyParseError::DomainValidation { domain, message }) = result {
1591            assert_eq!(domain, "test");
1592            assert!(message.contains("invalid_"));
1593        } else {
1594            panic!("Expected domain validation error");
1595        }
1596    }
1597
1598    #[test]
1599    fn rejects_empty_too_long_and_invalid_characters() {
1600        // Empty key
1601        assert!(matches!(TestKey::new(""), Err(KeyParseError::Empty)));
1602
1603        // Too long key
1604        let long_key = "a".repeat(50);
1605        assert!(matches!(
1606            TestKey::new(&long_key),
1607            Err(KeyParseError::TooLong {
1608                max_length: 32,
1609                actual_length: 50
1610            })
1611        ));
1612
1613        // Invalid character
1614        let result = TestKey::new("key with spaces");
1615        assert!(matches!(
1616            result,
1617            Err(KeyParseError::InvalidCharacter {
1618                character: ' ',
1619                position: 3,
1620                ..
1621            })
1622        ));
1623    }
1624
1625    #[test]
1626    fn equal_keys_produce_same_hash() {
1627        use core::hash::{Hash, Hasher};
1628
1629        let key1 = TestKey::new("test_key").unwrap();
1630        let key2 = TestKey::new("test_key").unwrap();
1631
1632        // Pre-computed hashes should match
1633        assert_eq!(key1.hash(), key2.hash());
1634
1635        let key3 = TestKey::new("different_key").unwrap();
1636        assert_ne!(key1.hash(), key3.hash());
1637
1638        // Hash trait should produce same result as hashing the raw &str,
1639        // so Borrow<str> contract is upheld.
1640        #[cfg(feature = "std")]
1641        {
1642            let mut h = std::collections::hash_map::DefaultHasher::new();
1643            Hash::hash(&key1, &mut h);
1644            let key_trait_hash = h.finish();
1645            let mut h = std::collections::hash_map::DefaultHasher::new();
1646            Hash::hash(key1.as_str(), &mut h);
1647            let str_trait_hash = h.finish();
1648            assert_eq!(key_trait_hash, str_trait_hash);
1649        }
1650    }
1651
1652    #[test]
1653    fn string_query_methods_work_correctly() {
1654        let key = TestKey::new("test_key_example").unwrap();
1655        assert!(key.starts_with("test_"));
1656        assert!(key.ends_with("_example"));
1657        assert!(key.contains("_key_"));
1658        assert_eq!(key.len(), 16);
1659        assert!(!key.is_empty());
1660    }
1661
1662    #[test]
1663    fn from_string_validates_owned_input() {
1664        let key = TestKey::from_string("test_key".to_string()).unwrap();
1665        assert_eq!(key.as_str(), "test_key");
1666    }
1667
1668    #[test]
1669    fn try_from_static_rejects_empty_string() {
1670        let key = TestKey::try_from_static("static_key").unwrap();
1671        assert_eq!(key.as_str(), "static_key");
1672
1673        let invalid = TestKey::try_from_static("");
1674        assert!(invalid.is_err());
1675    }
1676
1677    #[test]
1678    fn validation_info_reflects_domain_config() {
1679        let key = TestKey::new("test_key").unwrap();
1680        let info = key.validation_info();
1681
1682        assert_eq!(info.domain_info.name, "test");
1683        assert_eq!(info.domain_info.max_length, 32);
1684        assert_eq!(info.length, 8);
1685        assert!(info.domain_info.has_custom_validation);
1686        assert!(info.domain_info.has_custom_normalization);
1687    }
1688
1689    #[cfg(feature = "serde")]
1690    #[test]
1691    fn serde_roundtrip_preserves_key() {
1692        let key = TestKey::new("test_key").unwrap();
1693
1694        // Test JSON serialization
1695        let json = serde_json::to_string(&key).unwrap();
1696        assert_eq!(json, r#""test_key""#);
1697
1698        // Test JSON deserialization
1699        let deserialized: TestKey = serde_json::from_str(&json).unwrap();
1700        assert_eq!(deserialized, key);
1701    }
1702
1703    #[test]
1704    fn from_parts_joins_and_splits_roundtrip() {
1705        let key = TestKey::from_parts(&["user", "123", "profile"], "_").unwrap();
1706        assert_eq!(key.as_str(), "user_123_profile");
1707
1708        let parts: Vec<&str> = key.split('_').collect();
1709        assert_eq!(parts, vec!["user", "123", "profile"]);
1710    }
1711
1712    #[test]
1713    fn ensure_prefix_suffix_is_idempotent() {
1714        let key = TestKey::new("profile").unwrap();
1715
1716        let prefixed = key.ensure_prefix("user_").unwrap();
1717        assert_eq!(prefixed.as_str(), "user_profile");
1718
1719        // Already has prefix
1720        let same = prefixed.ensure_prefix("user_").unwrap();
1721        assert_eq!(same.as_str(), "user_profile");
1722
1723        let suffixed = key.ensure_suffix("_v1").unwrap();
1724        assert_eq!(suffixed.as_str(), "profile_v1");
1725
1726        // Already has suffix
1727        let same = suffixed.ensure_suffix("_v1").unwrap();
1728        assert_eq!(same.as_str(), "profile_v1");
1729    }
1730
1731    #[test]
1732    fn display_shows_raw_key_value() {
1733        let key = TestKey::new("example").unwrap();
1734        assert_eq!(format!("{key}"), "example");
1735    }
1736
1737    #[test]
1738    fn into_string_extracts_value() {
1739        let key = TestKey::new("example").unwrap();
1740        let string: String = key.into();
1741        assert_eq!(string, "example");
1742    }
1743
1744    #[test]
1745    fn parse_str_creates_validated_key() {
1746        let key: TestKey = "example".parse().unwrap();
1747        assert_eq!(key.as_str(), "example");
1748    }
1749
1750    #[test]
1751    fn default_domain_accepts_simple_keys() {
1752        type DefaultKey = Key<DefaultDomain>;
1753        let key = DefaultKey::new("test_key").unwrap();
1754        assert_eq!(key.domain(), "default");
1755        assert_eq!(key.as_str(), "test_key");
1756    }
1757
1758    #[test]
1759    fn len_returns_consistent_cached_value() {
1760        let key = TestKey::new("test_key").unwrap();
1761        assert_eq!(key.len(), 8);
1762        assert_eq!(key.len(), 8); // Second call — same result
1763    }
1764
1765    #[test]
1766    fn split_methods_produce_same_parts() {
1767        let key = TestKey::new("user_profile_settings").unwrap();
1768
1769        let parts: Vec<&str> = key.split('_').collect();
1770        assert_eq!(parts, vec!["user", "profile", "settings"]);
1771
1772        let cached_parts: Vec<&str> = key.split_cached('_').collect();
1773        assert_eq!(cached_parts, vec!["user", "profile", "settings"]);
1774
1775        let str_parts: Vec<&str> = key.split_str("_").collect();
1776        assert_eq!(str_parts, vec!["user", "profile", "settings"]);
1777    }
1778
1779    #[test]
1780    fn deref_coerces_to_str() {
1781        fn takes_str(s: &str) -> &str {
1782            s
1783        }
1784        let key = TestKey::new("hello").unwrap();
1785        // Deref allows &Key<T> → &str coercion
1786        let s: &str = &key;
1787        assert_eq!(s, "hello");
1788
1789        // Works with functions that accept &str
1790        assert_eq!(takes_str(&key), "hello");
1791    }
1792
1793    #[test]
1794    fn from_smartstring_creates_key_without_revalidation() {
1795        use smartstring::alias::String as SmartString;
1796
1797        let smart = SmartString::from("pre_validated");
1798        let key: TestKey = TestKey::from(smart);
1799        assert_eq!(key.as_str(), "pre_validated");
1800        assert_eq!(key.len(), 13);
1801        // Hash should be computed correctly
1802        assert_ne!(key.hash(), 0);
1803    }
1804
1805    #[cfg(feature = "std")]
1806    #[test]
1807    fn borrow_str_enables_hashmap_get_by_str() {
1808        use std::collections::HashMap;
1809
1810        let mut map: HashMap<TestKey, u32> = HashMap::new();
1811        let key = TestKey::new("lookup_test").unwrap();
1812        map.insert(key, 42);
1813
1814        // Lookup by &str — works thanks to Borrow<str>
1815        assert_eq!(map.get("lookup_test"), Some(&42));
1816        assert_eq!(map.get("nonexistent"), None);
1817    }
1818
1819    #[test]
1820    fn struct_is_32_bytes() {
1821        // SmartString(24) + u64 hash(8) + PhantomData(0) = 32 bytes
1822        assert_eq!(core::mem::size_of::<TestKey>(), 32);
1823    }
1824}