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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
use std::fmt::{self,Display};
use std::borrow::{Cow, Borrow};
//---------------------------------------------------------------------------------------------------- Impl
impl<T: Borrow<str>> HeadTail for T {
fn as_str(&self) -> &str {
self.borrow()
}
}
/// The separator string inserted when using [`HeadTail`]'s `dot` functions.
pub const DOT: &str = "...";
//---------------------------------------------------------------------------------------------------- Head
/// Head/Tail characters of a [`str`]
///
/// This trait provides some functionality for
/// cutting off a string either by the head, tail,
/// or both, with optional `...` after/before/in-between.
///
/// Anything that implements [`std::borrow::Borrow<str>`] can use this trait automatically.
///
/// ## Examples
/// ```rust
/// use readable::HeadTail;
///
/// let string = "hello world";
/// assert_eq!(string.len(), 11);
///
/// assert_eq!( string.head(5), "hello");
/// assert_eq!( string.head_dot(5), "hello...");
/// assert_eq!( string.tail(5), "world");
/// assert_eq!( string.tail_dot(5), "...world");
/// assert_eq!( string.head_tail(5, 5), "helloworld");
/// assert_eq!(string.head_tail_dot(5, 5), "hello...world");
/// ```
///
/// The characters are split as `UTF-8` characters, so strings like this will work:
/// ```rust
/// use readable::HeadTail;
///
/// let emojis = "🦀🦀🦀🐸🐸🐸";
/// assert_eq!(emojis.len(), 24);
///
/// assert_eq!( emojis.head(2), "🦀🦀");
/// assert_eq!( emojis.head_dot(2), "🦀🦀...");
/// assert_eq!( emojis.tail(2), "🐸🐸");
/// assert_eq!( emojis.tail_dot(2), "...🐸🐸");
/// assert_eq!( emojis.head_tail(2, 2), "🦀🦀🐸🐸");
/// assert_eq!(emojis.head_tail_dot(2, 2), "🦀🦀...🐸🐸");
/// ```
///
/// ## Returned "HeadTail" Types
/// All types returned by this trait can compare with strings
/// without any allocation, e.g:
/// ```rust
/// # use readable::HeadTail;
/// let emojis = "🦀🦀🦀🐸🐸🐸";
/// // This comparison isn't allocating anything.
/// assert_eq!(emojis.head_tail_dot(2, 2), "🦀🦀...🐸🐸");
/// ```
///
/// The `head + tail` types can selectively show each side:
/// ```rust
/// use readable::str::{HeadTail, HeadTailStr};
///
/// let emojis: &str = "🦀🦀🦀🐸🐸🐸";
/// let headtail: HeadTailStr = emojis.head_tail(1, 1);
/// assert_eq!(headtail.head(), "🦀");
/// assert_eq!(headtail.tail(), "🐸");
/// ```
///
/// And they all implement [`std::fmt::Display`], so they can also use `.to_string()`:
/// ```rust
/// use readable::str::{
/// // This is the main trait.
/// HeadTail,
/// // This is the returned struct
/// // holding `str` references.
/// HeadTailDot
/// };
///
/// let string: &str = "hello world";
/// let dot: HeadTailDot = string.head_tail_dot(2, 2);
///
/// // No allocation needed here.
/// assert_eq!(dot, "he...ld");
///
/// // Now it's an owned String.
/// let new: String = dot.to_string();
/// assert_eq!(new, "he...ld");
/// ```
pub trait HeadTail {
/// Turn `self` into a [`str`].
///
/// If your type implements [`std::borrow::Borrow<str>`],
/// it will automatically implement [`HeadTail`].
fn as_str(&self) -> &str;
/// Return the first `head` UTF-8 characters of this [`str`].
///
/// This will return the full [`str`] if `head` is
/// longer than the actual inner [`str`].
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
/// assert_eq!(string.head(5), "hello");
/// ```
fn head(&self, head: usize) -> Head<'_> {
let s = self.as_str();
if let Some((index, _)) = s.char_indices().nth(head) {
Head { string: &s[..index], cut: true }
} else {
Head { string: s, cut: false }
}
}
/// Same as [`HeadTail::head()`] but this will allocate
/// a new [`String`] ending with `...`.
///
/// This will not allocate and will return the input without
/// `...` if `head` is longer than the actual inner [`str`].
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
/// assert_eq!(string.head_dot(5), "hello...");
///
/// // No dot appended.
/// let string = "hello world";
/// assert_eq!(string.head_dot(11), string);
/// ```
fn head_dot(&self, head: usize) -> HeadDot<'_> {
let s = self.as_str();
if let Some((index, _)) = s.char_indices().nth(head) {
let mut string = String::with_capacity(s.len() + 3);
string += &s[..index];
string += DOT;
HeadDot { cow: Cow::Owned(string) }
} else {
HeadDot { cow: Cow::Borrowed(s) }
}
}
/// Return the last `tail` UTF-8 characters of this [`str`].
///
/// This will return the full [`str`] if `tail` is
/// longer than the actual inner [`str`].
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
/// assert_eq!(string.tail(5), "world");
/// ```
fn tail(&self, tail: usize) -> Tail<'_> {
let s = self.as_str();
let end = s.chars().count();
if tail >= end {
return Tail { string: s, cut: false };
}
if let Some((index, _)) = s.char_indices().nth(end - tail) {
Tail { string: &s[index..], cut: true }
} else {
Tail { string: s, cut: false }
}
}
/// Same as [`HeadTail::tail()`] but this allocated
/// a new [`String`] ending with `...`.
///
/// This will return the full string without `...` if
/// `tail` is longer than the actual inner [`str`].
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
/// assert_eq!(string.tail_dot(5), "...world");
///
/// // No dot appended.
/// let string = "hello world";
/// assert_eq!(string.tail_dot(11), string);
/// ```
fn tail_dot(&self, tail: usize) -> TailDot<'_> {
let s = self.as_str();
let end = s.chars().count();
if tail >= end {
return TailDot { cow: Cow::Borrowed(s) }
}
if let Some((index, _)) = s.char_indices().nth(end - tail) {
let mut string = String::with_capacity(end + 3);
string += DOT;
string += &s[index..];
TailDot { cow: Cow::Owned(string) }
} else {
TailDot { cow: Cow::Borrowed(s) }
}
}
/// Return the first `head` UTF-8 characters and last `tail`
/// UTF-8 characters of this [`str`].
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
/// assert_eq!(string.head_tail(5, 5), "helloworld");
///
/// // No string allocated for this.
/// let string = "hello world";
/// assert_eq!(string.head_tail(6, 5), string);
///
/// // Non-ASCII characters.
/// let sixteen_chars = "🦀🦀🐸🐸";
/// let four_chars = "ですけど";
///
/// assert_eq!(sixteen_chars.len(), 16);
/// assert_eq!(four_chars.len(), 12);
///
/// assert_eq!(sixteen_chars.head_tail(1, 1), "🦀🐸");
/// assert_eq!(four_chars.head_tail(1, 1), "でど");
///
/// assert_eq!(sixteen_chars.head_tail(2, 2), sixteen_chars);
/// assert_eq!(four_chars.head_tail(2, 2), four_chars);
/// ```
fn head_tail(&self, head: usize, tail: usize) -> HeadTailStr<'_> {
let s = self.as_str();
let end = s.chars().count();
if head + tail >= end {
return HeadTailStr { head: s, tail: None }
}
// Iterator is consumed, must create twice.
let head = s.char_indices().nth(head);
let tail = s.char_indices().nth(end - tail);
if let (Some((head, _)), Some((tail, _))) = (head, tail) {
HeadTailStr { head: &s[..head], tail: Some(&s[tail..]) }
} else {
HeadTailStr { head: s, tail: None }
}
}
/// Return the first `head` UTF-8 characters and last `tail`
/// UTF-8 characters of this [`str`] separated with `...`.
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
/// assert_eq!(string.head_tail_dot(5, 5), "hello...world");
///
/// // No dot appended.
/// let string = "hello world";
/// assert_eq!(string.head_tail_dot(6, 5), string);
///
/// // Non-ASCII characters.
/// let sixteen_chars = "🦀🦀🐸🐸";
/// let four_chars = "ですけど";
///
/// assert_eq!(sixteen_chars.len(), 16);
/// assert_eq!(four_chars.len(), 12);
///
/// assert_eq!(sixteen_chars.head_tail_dot(1, 1), "🦀...🐸");
/// assert_eq!(four_chars.head_tail_dot(1, 1), "で...ど");
///
/// assert_eq!(sixteen_chars.head_tail_dot(3, 3), sixteen_chars);
/// assert_eq!(four_chars.head_tail_dot(2, 2), four_chars);
/// ```
fn head_tail_dot(&self, head: usize, tail: usize) -> HeadTailDot<'_> {
let s = self.as_str();
let end = s.chars().count();
if head + tail >= end {
return HeadTailDot { head: s, tail: None }
}
// Iterator is consumed, must create twice.
let head = s.char_indices().nth(head);
let tail = s.char_indices().nth(end - tail);
if let (Some((head, _)), Some((tail, _))) = (head, tail) {
HeadTailDot { head: &s[..head], tail: Some(&s[tail..]) }
} else {
HeadTailDot { head: s, tail: None }
}
}
}
//---------------------------------------------------------------------------------------------------- HeadTail structs
/// Struct returned from [`HeadTail::head()`]
///
/// This struct:
/// - Implements [`PartialEq`] with strings
/// - Implements [`Display`] and thus `.to_string()`
/// - Can indicate whether the input string was actually cut or not
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
pub struct Head<'a> {
string: &'a str,
cut: bool,
}
/// Struct returned from [`HeadTail::tail()`]
///
/// This struct:
/// - Implements [`PartialEq`] with strings
/// - Implements [`Display`] and thus `.to_string()`
/// - Can indicate whether the input string was actually cut or not
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
pub struct Tail<'a> {
string: &'a str,
cut: bool,
}
macro_rules! impl_string {
($($name:ident),*) => {
$(
impl<'a> $name<'a> {
#[inline]
/// Returns the inner `string`, whether cut off or not
pub const fn as_str(&self) -> &str {
self.string
}
#[inline]
/// Returns the inner parts that make this type up.
///
/// The returned `str` is the head/tail portion.
///
/// The returned `bool` is whether the input string was cut or not.
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
///
/// // Input (11) can capture the whole string, so no cutting.
/// let (head, cut) = string.head(11).into_parts();
/// assert_eq!(head, string);
/// assert!(!cut);
///
/// // If it can't capture it all (5 != 11),
/// // then a `cut` will be `true`.
/// let (head, cut) = string.head(5).into_parts();
/// assert_eq!(head, "hello");
/// assert!(cut);
/// ```
pub const fn into_parts(self) -> (&'a str, bool) {
(self.string, self.cut)
}
#[inline]
/// Returns `true` is the string was cut in any way.
///
/// Returns `false` if running `.to_string()` on this
/// would result in the same string as the input string.
pub const fn cut(&self) -> bool {
self.cut
}
}
impl PartialEq<str> for $name<'_> {
fn eq(&self, other: &str) -> bool {
self.string == other
}
}
impl PartialEq<&str> for $name<'_> {
fn eq(&self, other: &&str) -> bool {
self.string == *other
}
}
impl AsRef<str> for $name<'_> {
fn as_ref(&self) -> &str {
self.string
}
}
impl std::ops::Deref for $name<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.string
}
}
impl fmt::Display for $name<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.string)
}
}
)*
};
}
impl_string!(Head, Tail);
//---------------------------------------------------------------------------------------------------- HeadTail Cow
/// Struct returned from [`HeadTail::head_dot()`]
///
/// This struct:
/// - Implements [`PartialEq`] with strings
/// - Implements [`Display`] and thus `.to_string()`
/// - Can indicate whether the input string was actually cut or not
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
pub struct HeadDot<'a> {
cow: Cow<'a, str>,
}
/// Struct returned from [`HeadTail::tail_dot()`]
///
/// This struct:
/// - Implements [`PartialEq`] with strings
/// - Implements [`Display`] and thus `.to_string()`
/// - Can indicate whether the input string was actually cut or not
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bincode", derive(bincode::Encode, bincode::Decode))]
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
pub struct TailDot<'a> {
cow: Cow<'a, str>,
}
macro_rules! impl_cow {
($($name:ident),*) => {
$(
impl PartialEq<str> for $name<'_> {
fn eq(&self, other: &str) -> bool {
self.cow == other
}
}
impl PartialEq<&str> for $name<'_> {
fn eq(&self, other: &&str) -> bool {
self.cow == *other
}
}
impl fmt::Display for $name<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.cow)
}
}
impl AsRef<str> for $name<'_> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::ops::Deref for $name<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl<'a> $name<'a> {
#[inline]
/// Returns the inner `string`, whether cut off or not
pub fn as_str(&self) -> &str {
match self.cow {
Cow::Borrowed(s) => s,
Cow::Owned(ref s) => s,
}
}
#[inline]
/// Returns the inner `Cow<'a, str>`.
///
/// The returned `str` is the head/tail portion.
///
/// If the [`Cow`] is [`Cow::Owned`] then it means the string was cut.
///
/// If the [`Cow`] is [`Cow::Borrowed`] then it means the string was not cut.
///
/// ```rust
/// # use readable::HeadTail;
/// # use std::borrow::Cow;
/// let string = "hello world";
///
/// // Input (11) can capture the whole string,
/// // so there is no allocation or dot.
/// let head = string.head_dot(11).into_cow();
/// assert_eq!(head, string);
/// match head {
/// Cow::Owned(_) => unreachable!(),
/// Cow::Borrowed(_) => (),
/// }
///
/// // If it can't capture it all (5 != 11),
/// // then a string is allocated suffixed with `...`
/// let head = string.head_dot(5).into_cow();
/// assert_eq!(head, "hello...");
/// match head {
/// Cow::Owned(_) => (),
/// Cow::Borrowed(_) => unreachable!(),
/// }
/// ```
pub fn into_cow(self) -> Cow<'a, str> {
self.cow
}
#[inline]
/// Returns `true` is the string was cut in any way.
///
/// Returns `false` if running `.to_string()` on this
/// would result in the same string as the input string.
pub const fn cut(&self) -> bool {
match self.cow {
Cow::Borrowed(_) => false,
Cow::Owned(_) => true,
}
}
}
)*
};
}
impl_cow!(HeadDot, TailDot);
//---------------------------------------------------------------------------------------------------- HeadTail cut
/// Struct returned from [`HeadTail::head_tail()`]
///
/// This struct:
/// - Implements no-allocation [`PartialEq`] with [`str`]
/// - Implements [`Display`] and thus `.to_string()`
/// - Can selectively show head/tail portions
/// - Can indicate whether the input string was actually cut or not
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
pub struct HeadTailStr<'a> {
// This holds the whole string if `head + tail > input_len`
head: &'a str,
tail: Option<&'a str>,
}
impl fmt::Display for HeadTailStr<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.tail {
Some(t) => write!(f, "{}{t}", self.head),
None => write!(f, "{}", self.head),
}
}
}
impl HeadTailStr<'_> {
#[inline]
fn str_cmp(&self, other: &str) -> bool {
match self.tail {
Some(t) => {
let head_bytes = self.head.as_bytes();
let tail_bytes = t.as_bytes();
let str_bytes = other.as_bytes();
let head_len = head_bytes.len();
let tail_len = tail_bytes.len();
let total_len = head_len + tail_len;
total_len == str_bytes.len() &&
head_bytes == &str_bytes[..head_len] &&
tail_bytes == &str_bytes[tail_len..]
},
None => self.head == other,
}
}
}
/// Struct returned from [`HeadTail::head_tail_dot()`]
///
/// This struct:
/// - Implements no-allocation [`PartialEq`] with [`str`]
/// - Implements [`Display`] and thus `.to_string()`
/// - Can selectively show head/tail portions
/// - Can indicate whether the input string was actually cut or not
///
/// ## Note
/// If [`Self::cut()`] is `true`, the proper string to
/// compare against would also contain `...`.
///
/// If [`Self::cut()`] is `false`, no dot will appear
/// after using `.to_string()`, so a string without `...`
/// would be correct to compare against.
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "head tail";
/// let dot = string.head_tail_dot(4, 4);
/// assert_eq!(dot, "head...tail");
/// assert_eq!(dot.to_string(), "head...tail");
///
/// let no_dot = string.head_tail_dot(5, 4);
/// assert_eq!(no_dot, "head tail");
/// assert_eq!(no_dot.to_string(), "head tail");
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Debug)]
pub struct HeadTailDot<'a> {
// This holds the whole string if `head + tail > input_len`
head: &'a str,
tail: Option<&'a str>,
}
impl fmt::Display for HeadTailDot<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.tail {
Some(t) => write!(f, "{}{DOT}{t}", self.head),
None => write!(f, "{}", self.head),
}
}
}
impl HeadTailDot<'_> {
#[inline]
fn str_cmp(&self, other: &str) -> bool {
match self.tail {
Some(t) => {
let head_bytes = self.head.as_bytes();
let tail_bytes = t.as_bytes();
let str_bytes = other.as_bytes();
let head_len = head_bytes.len();
let tail_len = tail_bytes.len();
let total_len = head_len + tail_len;
// Tail exists, that means the string was
// cut so we should be expecting `...`
(total_len + 3) == str_bytes.len() &&
head_bytes == &str_bytes[..head_len] &&
tail_bytes == &str_bytes[tail_len + 3..]
},
None => self.head == other,
}
}
}
macro_rules! impl_head_tail {
($($name:ident),*) => {
$(
impl PartialEq<str> for $name<'_> {
fn eq(&self, other: &str) -> bool {
self.str_cmp(other)
}
}
impl PartialEq<&str> for $name<'_> {
fn eq(&self, other: &&str) -> bool {
self.str_cmp(other)
}
}
impl<'a> $name<'a> {
#[inline]
/// Returns `true` is the string was cut in any way.
///
/// Returns `false` if running `.to_string()` on this
/// would result in the same string as the input string.
pub const fn cut(&self) -> bool {
self.tail.is_some()
}
#[inline]
/// Return the only `head` portion of the string
pub const fn head(&self) -> &str {
self.head
}
#[inline]
/// Return the only `tail` portion of the string
pub const fn tail(&self) -> &str {
match self.tail {
Some(t) => t,
None => self.head,
}
}
#[inline]
/// Returns the inner `head/tail` `str`'s that make this type up.
///
/// The returned `&'a str` is the `head` portion.
/// If the entire input string could fit, then this will
/// contain the entire input string and `tail` will be [`None`].
///
/// The returned `Option<&'a str>` is the `tail` portion.
/// If this is [`Some`] that means the input string was cut.
/// If it is [`None`] that means the input string was not cut
/// and the entire input resides inside the `head` portion.
///
/// ```rust
/// # use readable::HeadTail;
/// let string = "hello world";
///
/// // Input (6+5 == 11) can capture the whole string.
/// let headtail = string.head_tail(6, 5);
///
/// // So there is no tail, everything is in head.
/// let (head, tail) = headtail.into_parts();
/// assert_eq!(head, string);
/// assert_eq!(tail, None);
///
/// // If it can't capture it all (5+5 != 11), then there is a tail.
/// let headtail = string.head_tail(5, 5);
/// let (head, tail) = headtail.into_parts();
/// assert_eq!(head, "hello");
/// assert_eq!(tail, Some("world"));
/// ```
pub const fn into_parts(self) -> (&'a str, Option<&'a str>) {
(self.head, self.tail)
}
}
)*
};
}
impl_head_tail!(HeadTailStr, HeadTailDot);
//---------------------------------------------------------------------------------------------------- TESTS
// #[cfg(test)]
// mod tests {
// use super::*;
// }