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 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
//! Improve and strengthen your strings
//!
//! Strongly-typed APIs reduce errors and confusion over passing around un-typed strings.
//! Braid helps in that endeavor by making it painless to create wrappers around your
//! string values, ensuring that you use them in the right way every time.
//!
//! Examples of the documentation and implementations provided for braids are available
//! below and in the [`aliri_braid_examples`] crate documentation.
//!
//! [`aliri_braid_examples`]: https://docs.rs/aliri_braid_examples/
//!
//! # Usage
//!
//! A braid is created by attaching `#[braid]` to a struct definition. The macro will take
//! care of automatically updating the representation of the struct to wrap a string and
//! generate the borrowed form of the strong type.
//!
//! ```
//! use aliri_braid::braid;
//!
//! #[braid]
//! pub struct DatabaseName;
//! ```
//!
//! Braids of custom string types are also supported, so long as they implement a set of
//! expected traits. If not specified, the type named `String` in the current namespace
//! will be used. See the section on [custom string types] for more information.
//!
//! [custom string types]: #custom-string-types
//!
//! ```
//! use aliri_braid::braid;
//! use smartstring::alias::String;
//!
//! #[braid]
//! pub struct UserId;
//! ```
//!
//! Once created, braids can be passed around as strongly-typed, immutable strings.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! fn take_strong_string(n: DatabaseName) {}
//! fn borrow_strong_string(n: &DatabaseNameRef) {}
//!
//!# #[braid]
//!# pub struct DatabaseName;
//!#
//! let owned = DatabaseName::new(String::from("mongo"));
//! borrow_strong_string(&owned);
//! take_strong_string(owned);
//! ```
//!
//! A braid can also be untyped for use in stringly-typed interfaces.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! fn take_raw_string(s: String) {}
//! fn borrow_raw_str(s: &str) {}
//!
//!# #[braid]
//!# pub struct DatabaseName;
//!#
//! let owned = DatabaseName::new(String::from("mongo"));
//! borrow_raw_str(owned.as_str());
//! take_raw_string(owned.take());
//! ```
//!
//! By default, the name of the borrowed form will be the same as the owned form
//! with `Ref` appended to the end.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[braid]
//! pub struct DatabaseName;
//!
//! let owned = DatabaseName::from_static("mongo");
//! let borrowed = DatabaseNameRef::from_static("mongo");
//!# assert_eq!(owned, borrowed);
//! ```
//!
//! If the name ends with `Buf`, however, then the borrowed form will drop the `Buf`, similar
//! to the relationship between
//! [`PathBuf`][std::path::PathBuf] and [`Path`][std::path::Path].
//!
//! [std::path::PathBuf]: https://doc.rust-lang.org/std/path/struct.PathBuf.html
//! [std::path::Path]: https://doc.rust-lang.org/std/path/struct.Path.html
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[braid]
//! pub struct DatabaseNameBuf;
//!
//! let owned = DatabaseNameBuf::from_static("mongo");
//! let borrowed = DatabaseName::from_static("mongo");
//!# assert_eq!(owned, borrowed);
//! ```
//!
//! If a different name is desired, this behavior can be
//! overridden by specifying the name of the reference type to create using the `ref`
//! parameter.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[braid(ref = "TempDb")]
//! pub struct DatabaseNameBuf;
//!
//! let owned = DatabaseNameBuf::from_static("mongo");
//! let borrowed = TempDb::from_static("mongo");
//! let to_owned: DatabaseNameBuf = borrowed.to_owned();
//!# assert_eq!(owned, borrowed);
//! ```
//!
//! A default doc comment is added to the borrowed form that refers back to the owned form.
//! If a custom doc comment is desired, the `ref_doc` parameter allows supplying custom
//! documentation.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[braid(ref_doc = "A temporary reference to a database name")]
//! pub struct DatabaseName;
//!#
//!# let owned = DatabaseName::from_static("mongo");
//!# let borrowed = DatabaseNameRef::from_static("mongo");
//!# assert_eq!(owned, borrowed);
//! ```
//!
//! Attributes added to the braid will be applied to both the owned and borrowed forms
//! with the exception of `///` and `#[doc = ""]` attributes. To add an attribute to
//! only the owned form, use the `owned_attr` parameter. Similarly, use `ref_attr` to
//! add an attribute to only the borrowed form.
//!
//! ```
//! use aliri_braid::braid;
//!
//! #[braid(
//! owned_attr(must_use = "database name should always be used"),
//! ref_attr(must_use = "created a reference, but never used it"),
//! )]
//! #[cfg(not(feature = "nightly"))]
//! pub struct DatabaseName;
//! ```
//!
//! # Extensibility
//!
//! The types created by the `braid` macro are placed in the same module where declared.
//! This means additional functionality, including mutations, can be implemented easily.
//!
//! As a basic example, here is a type built to hold Amazon ARNs. The type has been
//! extended to support some mutation and introspection.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[braid]
//! pub struct AmazonArnBuf;
//!
//! impl AmazonArnBuf {
//! /// Append an ARN segment
//! pub fn add_segment(&mut self, segment: &str) {
//! self.0.push_str(":");
//! self.0.push_str(segment);
//! }
//! }
//!
//! impl AmazonArn {
//! /// Returns an iterator of all ARN segments
//! pub fn get_segments(&self) -> std::str::Split<char> {
//! self.0.split(':')
//! }
//!
//! /// Returns the service segment of the ARN
//! pub fn get_service(&self) -> &str {
//! self.get_segments().nth(2).unwrap_or("")
//! }
//! }
//! ```
//!
//! # Encapsulation
//!
//! Because code within the same module where the braid is defined are allowed to
//! access the internal value, you can use a module in order to more strictly
//! enforce encapsulation and limit accessibility that might otherwise violate
//! established invariants. This may be particularly desired when the wrapped type
//! requires [validation](#validation).
//!
//! ```
//! mod amazon_arn {
//! #[aliri_braid::braid]
//! pub struct AmazonArnBuf;
//!
//! /* Additional impls that need access to the inner values */
//!# impl AmazonArn {
//!# pub fn get_segments(&self) -> std::str::Split<char> {
//!# self.0.split(':')
//!# }
//!#
//!# pub fn get_service(&self) -> &str {
//!# self.get_segments().nth(2).unwrap_or("")
//!# }
//!# }
//! }
//!
//! pub use amazon_arn::{AmazonArnBuf, AmazonArn};
//!
//! # fn main() {
//! let x = AmazonArnBuf::from_static("arn:aws:iam::123456789012:user/Development");
//! assert_eq!("iam", x.get_service());
//! # }
//! ```
//!
//! # Soundness
//!
//! This crate ensures that the `from_str` implementation provided for wrapping
//! borrowed `str` slices does not extend lifetimes.
//!
//! In the example below, we verify that the borrowed `DatabaseNameRef` is unable
//! to escape the lifetime of `data`. The following code snippet will fail to
//! compile, because `data` will go out of scope and be dropped at the end of
//! the block creating `ex_ref`.
//!
//! ```compile_fail
//!# use aliri_braid::braid;
//!#
//!# #[braid]
//!# pub struct DatabaseName;
//!#
//! let ex_ref = {
//! let data = DatabaseName::new("test string");
//! DatabaseNameRef::from_str(data.as_str())
//! }; // `data` is dropped at this point
//!
//! // Which means that `ex_ref` would be invalid if allowed.
//! println!("{}", ex_ref);
//! ```
//!
//! # Validation
//!
//! Types can be configured to only contain certain values. This can be used to strongly
//! enforce domain type boundaries, thus making invalid values unrepresentable.
//!
//! For example, if you wanted to have a username type that did not accept the `root` user,
//! you have a few options:
//!
//! 1. Pass the username around as a string, validate that it isn't `root` at known entry points.
//! 2. Create a username type and allow creation from a raw string, then validate it
//! just after creation.
//! 3. Create a strong username type that requires the value to be validated prior to being
//! creatable.
//!
//! Braided strings give the strongest, third guarantee. The other two methods require constant
//! vigilance to ensure that an unexpected `root` value doesn't sneak in through other backdoors.
//!
//! By default, Rust's module system allows items within the same module to have access to
//! each other's non-public members. If not handled properly, this can lead to unintentionally
//! violating invariants. Thus, for the strongest guarantees, it is recommended to use the module
//! system to further control access to the interior values held by the braided type as
//! described in the section on [encapsulation](#encapsulation).
//!
//! As a convenience, `from_static` functions are provided that accept `&'static str`. For fallible
//! braids and the owned form of normalized braids, this function will panic if the value is not
//! valid. For borrowed form of normalized braids, the function will panic if the value is not
//! normalized.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[derive(Debug, PartialEq, Eq)]
//! pub struct InvalidUsername;
//! // Error implementation elided
//!# impl std::fmt::Display for InvalidUsername {
//!# fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//!# f.write_str("invalid username")
//!# }
//!# }
//!# impl std::error::Error for InvalidUsername {}
//!
//! #[braid(validator)]
//! pub struct NonRootUsername;
//!
//! impl aliri_braid::Validator for NonRootUsername {
//! type Error = InvalidUsername;
//! fn validate(s: &str) -> Result<(), Self::Error> {
//! if s.is_empty() || s.eq_ignore_ascii_case("root") {
//! Err(InvalidUsername)
//! } else {
//! Ok(())
//! }
//! }
//! }
//!
//! assert!(NonRootUsername::new("".to_string()).is_err());
//! assert!(NonRootUsername::new("root".to_string()).is_err());
//! assert!(NonRootUsername::new("nobody".to_string()).is_ok());
//!
//! NonRootUsername::from_static("nobody");
//!
//! assert!(NonRootUsernameRef::from_str("").is_err());
//! assert!(NonRootUsernameRef::from_str("root").is_err());
//! assert!(NonRootUsernameRef::from_str("nobody").is_ok());
//!
//! NonRootUsernameRef::from_static("nobody");
//! ```
//!
//! Foreign validators can also be used by specifying the name of the type that
//! implements the validation logic.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//!# #[derive(Debug, PartialEq, Eq)]
//!# pub struct InvalidUsername;
//!# // Error implementation elided
//!# impl std::fmt::Display for InvalidUsername {
//!# fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//!# f.write_str("invalid username")
//!# }
//!# }
//!# impl std::error::Error for InvalidUsername {}
//!#
//! #[braid(validator = "UsernameValidator")]
//! pub struct NonRootUsername;
//!
//! pub struct UsernameValidator;
//!
//! impl aliri_braid::Validator for UsernameValidator {
//! /* … */
//!# type Error = InvalidUsername;
//!# fn validate(s: &str) -> Result<(), Self::Error> {
//!# if s.is_empty() || s.eq_ignore_ascii_case("root") {
//!# Err(InvalidUsername)
//!# } else {
//!# Ok(())
//!# }
//!# }
//! }
//!
//! assert!(NonRootUsername::new("".to_string()).is_err());
//! assert!(NonRootUsername::new("root".to_string()).is_err());
//! assert!(NonRootUsername::new("nobody".to_string()).is_ok());
//!
//! NonRootUsername::from_static("nobody");
//!
//! assert!(NonRootUsernameRef::from_str("").is_err());
//! assert!(NonRootUsernameRef::from_str("root").is_err());
//! assert!(NonRootUsernameRef::from_str("nobody").is_ok());
//!
//! NonRootUsernameRef::from_static("nobody");
//! ```
//!
//! ## Normalization
//!
//! Braided strings can also have enforced normalization, which is carried out at the creation
//! boundary. In this case, the `.from_str()` function on the borrowed form will return a
//! [`Cow<Borrowed>`][alloc::borrow::Cow], which can be inspected to determine whether
//! normalization and conversion to an owned value was required. In cases where the incoming
//! value is expected to already be normalized, the `.from_normalized_str()` function can
//! be used. This function will return an error if the value required normalization.
//!
//! Note that when implementing [`Validator`] for a braided type, the `validate` method
//! must ensure that the value is already in normalized form and return an error if it is
//! not.
//!
//! When using `serde` to deserialze directly to the borrowed form, care must be taken, as
//! only already normalized values will be able to be deserialized. If normalization is
//! expected, deserialize into the owned form or `Cow<Borrowed>`.
//!
//! Here is a toy example where the value must not be empty and must be composed of ASCII
//! characters, but that is also normalized to use lowercase ASCII letters.
//!
//! ```
//!# use aliri_braid::braid;
//! use std::borrow::Cow;
//!
//! #[derive(Debug, PartialEq, Eq)]
//! pub struct InvalidHeaderName;
//! // Error implementation elided
//!# impl std::fmt::Display for InvalidHeaderName {
//!# fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//!# f.write_str("invalid header name")
//!# }
//!# }
//!# impl std::error::Error for InvalidHeaderName {}
//!
//! #[braid(normalizer)]
//! pub struct HeaderName;
//!
//! impl aliri_braid::Validator for HeaderName {
//! type Error = InvalidHeaderName;
//! fn validate(s: &str) -> Result<(), Self::Error> {
//! if s.is_empty() || !s.is_ascii() || s.as_bytes().iter().any(|&b| b'A' <= b && b <= b'Z') {
//! Err(InvalidHeaderName)
//! } else {
//! Ok(())
//! }
//! }
//! }
//!
//! impl aliri_braid::Normalizer for HeaderName {
//! fn normalize(s: &str) -> Result<Cow<str>, Self::Error> {
//! if s.is_empty() || !s.is_ascii() {
//! Err(InvalidHeaderName)
//! } else if s.as_bytes().iter().any(|&b| b'A' <= b && b <= b'Z') {
//! Ok(Cow::Owned(s.to_ascii_lowercase()))
//! } else {
//! Ok(Cow::Borrowed(s))
//! }
//! }
//! }
//!
//! assert!(HeaderName::new("".to_string()).is_err());
//! assert_eq!("mixedcase", HeaderName::new("MixedCase".to_string()).unwrap().as_str());
//! assert_eq!("lowercase", HeaderName::new("lowercase".to_string()).unwrap().as_str());
//!
//! assert_eq!("mixedcase", HeaderName::from_static("MixedCase").as_str());
//! assert_eq!("lowercase", HeaderName::from_static("lowercase").as_str());
//!
//! assert!(HeaderNameRef::from_str("").is_err());
//! assert_eq!("mixedcase", HeaderNameRef::from_str("MixedCase").unwrap().as_str());
//! assert_eq!("lowercase", HeaderNameRef::from_str("lowercase").unwrap().as_str());
//!
//! assert!(HeaderNameRef::from_normalized_str("").is_err());
//! assert!(HeaderNameRef::from_normalized_str("MixedCase").is_err());
//! assert_eq!("lowercase", HeaderNameRef::from_normalized_str("lowercase").unwrap().as_str());
//!
//! assert_eq!("lowercase", HeaderNameRef::from_static("lowercase").as_str());
//! ```
//!
//! ## Unchecked creation
//!
//! Where necessary for efficiency, it is possible to bypass the validations on creation through
//! the use of the `.new_unchecked()` or `from_str_unchecked()` functions. These functions are
//! marked as `unsafe`, as they require the caller to assert that they are fulfilling the
//! implicit contract that the value be both valid and in normal form. If either of these
//! constraints are violated, undefined behavior could result when downstream consumers depend
//! on these constraints being upheld.
//!
//! ```compile_fail
//!# use aliri_braid::braid;
//!#
//!# #[derive(Debug, PartialEq, Eq)]
//!# pub struct InvalidUsername;
//!# // Error implementation elided
//!# impl std::fmt::Display for InvalidUsername {
//!# fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//!# f.write_str("invalid username")
//!# }
//!# }
//!# impl std::error::Error for InvalidUsername {}
//!#
//!# #[braid(validator)]
//!# pub struct NonRootUsername;
//!#
//!# impl aliri_braid::Validator for NonRootUsername {
//!# type Error = InvalidUsername;
//!# fn validate(s: &str) -> Result<(), Self::Error> {
//!# if s.is_empty() || s.eq_ignore_ascii_case("root") {
//!# Err(InvalidUsername)
//!# } else {
//!# Ok(())
//!# }
//!# }
//!# }
//!#
//! NonRootUsername::new_unchecked("");
//! NonRootUsernameRef::from_str_unchecked("nobody");
//! ```
//!
//! If you find violations of your guarantees, you can look specifically for uses of `unsafe`.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//!# #[derive(Debug, PartialEq, Eq)]
//!# pub struct InvalidUsername;
//!# // Error implementation elided
//!# impl std::fmt::Display for InvalidUsername {
//!# fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//!# f.write_str("invalid username")
//!# }
//!# }
//!# impl std::error::Error for InvalidUsername {}
//!#
//!# #[braid(validator)]
//!# pub struct NonRootUsername;
//!#
//!# impl aliri_braid::Validator for NonRootUsername {
//!# type Error = InvalidUsername;
//!# fn validate(s: &str) -> Result<(), Self::Error> {
//!# if s.is_empty() || s.eq_ignore_ascii_case("root") {
//!# Err(InvalidUsername)
//!# } else {
//!# Ok(())
//!# }
//!# }
//!# }
//!#
//! unsafe {
//! NonRootUsername::new_unchecked(String::from(""));
//! NonRootUsernameRef::from_str_unchecked("root");
//! }
//! ```
//!
//! # Provided trait impls
//!
//! By default, the following traits will be automatically implemented.
//!
//! For the `Owned` type
//! * [`core::clone::Clone`]
//! * [`core::fmt::Debug`]
//! * [`core::fmt::Display`]
//! * [`core::hash::Hash`]
//! * [`core::cmp::Eq`]
//! * [`core::cmp::Ord`]
//! * [`core::cmp::PartialEq<Owned>`]
//! * [`core::cmp::PartialEq<Borrowed>`]
//! * [`core::cmp::PartialEq<&Borrowed>`]
//! * [`core::cmp::PartialEq<Box<Borrowed>>`]
//! * [`core::cmp::PartialOrd`]
//! * [`core::convert::AsRef<Borrowed>`]
//! * [`core::convert::AsRef<str>`]
//! * [`core::convert::From<&Borrowed>`]
//! * [`core::convert::From<Box<Borrowed>>`]
//! * [`core::convert::From<Cow<Borrowed>>`]
//! * [`core::borrow::Borrow<Borrowed>`]
//! * [`core::str::FromStr`]
//! * [`core::ops::Deref`] where `Target = Borrowed`
//!
//! Additionally, unvalidated owned types implement
//! * [`core::convert::From<String>`]
//! * [`core::convert::From<&str>`]
//!
//! Validated and normalized owned types will instead implement
//! * [`core::convert::TryFrom<String>`]
//! * [`core::convert::TryFrom<&str>`]
//!
//! When normalized, the above conversions will normalize values.
//!
//! For the `Borrowed` type
//! * [`core::fmt::Debug`]
//! * [`core::fmt::Display`]
//! * [`core::hash::Hash`]
//! * [`core::cmp::Eq`]
//! * [`core::cmp::Ord`]
//! * [`core::cmp::PartialEq<Owned>`]
//! * [`core::cmp::PartialEq<Borrowed>`]
//! * [`core::cmp::PartialEq<&Borrowed>`]
//! * [`core::cmp::PartialEq<Box<Borrowed>>`]
//! * [`core::cmp::PartialOrd`]
//! * [`core::convert::From<&Cow<Borrowed>>`]
//! * [`alloc::borrow::ToOwned`] where `Owned = Owned`
//!
//! Additionally, unvalidated borrowed types implement
//! * [`core::convert::From<&str>`]
//!
//! Validated and normalize borrowed types will instead implement
//! * [`core::convert::TryFrom<&str>`]
//!
//! For `Cow<'static, Borrowed>`
//! * [`core::convert::From<Owned>`]
//!
//! For `Cow<Borrowed>`
//! * [`core::convert::From<&Borrowed>`]
//!
//! For `Box<Borrowed>`
//! * [`core::convert::From<Owned>`]
//!
//! The above conversion will fail if the value is not already normalized.
//!
//! Types that are not normalized will additionally implement
//! * [`core::borrow::Borrow<str>`]
//!
//! `Borrow<str>` cannot be implemented for normalized braids because equality and hashing
//! of equivalent braid values will have differing results for equality, which violates the
//! contract implied by the `Borrow` trait.
//!
//! `Deref` to a `str` is explicitly not implemented. This means that an explicit call is
//! required to treat a value as an untyped string, whether `.as_str()`, `.to_string()`, or
//! `.into_string()`
//!
//! ## Omitting `Clone`
//!
//! For some types, it may be desirable to prevent arbitrary cloning of a type. In that case,
//! the `clone` parameter can be used to prevent automatically deriving [`Clone`][core::clone::Clone].
//!
//! ```
//!# use aliri_braid::braid;
//!# use static_assertions::assert_not_impl_any;
//!#
//! #[braid(clone = "omit")]
//! pub struct Sensitive;
//!
//! assert_not_impl_any!(Sensitive: Clone);
//! ```
//!
//! ## Custom `Display`, `Debug`, and `PartialOrd`/`Ord` implementations
//!
//! By default, the implementations of [`Display`][core::fmt::Display], [`Debug`][core::fmt::Debug]
//! [`PartialOrd`][core::cmp::PartialOrd], and [`Ord`][core::cmp::Ord]
//! provided by a braid delegate directly to the underlying [`String`][alloc::string::String]
//! or [`str`] types. If a custom implementation is desired, the automatic derivation of these
//! traits can be controlled by the `display`, `debug`, and `ord` parameters. Both of these
//! parameters accept one of `impl`, `owned`, or `omit`. By default, the `impl` derivation
//! mode is used.
//!
//! The modes have the following effects:
//!
//! * `impl`: Format the owned and reference type transparently as the underlying string (slice) type.
//! * `owned`: Automatically provide an owned implementation that transparently delegates to the
//! implementation of the borrowed form. The consumer must provide their custom implementation on
//! the borrowed form.
//! * `omit`: No implementations are provided for the owned or borrowed forms. These must be
//! implemented by the consumer if they are desired.
//!
//! Note: Omitting a `PartialOrd` and `Ord` implementation will make the braid unable to be
//! used as a key in a `BTreeMap` or `BTreeSet`.
//!
//! As an example:
//!
//! ```
//!# use aliri_braid::braid;
//! use std::fmt;
//!#
//! #[braid(clone = "omit", display = "owned", debug = "owned")]
//! pub struct Sensitive;
//!
//! impl fmt::Debug for SensitiveRef {
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//! f.write_str("SENSITIVE")
//! }
//! }
//!
//! impl fmt::Display for SensitiveRef {
//! fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//! f.write_str("SENSITIVE DISPLAY")
//! }
//! }
//!
//! let owned = Sensitive::from_static("secret value");
//! assert_eq!("SENSITIVE", format!("{:?}", owned));
//! assert_eq!("SENSITIVE DISPLAY", format!("{}", owned));
//! assert_eq!("secret value", owned.as_str());
//!
//! let borrowed: &SensitiveRef = &owned;
//! assert_eq!("SENSITIVE", format!("{:?}", borrowed));
//! assert_eq!("SENSITIVE DISPLAY", format!("{}", borrowed));
//! assert_eq!("secret value", borrowed.as_str());
//! ```
//!
//! # Serde
//!
//! [`Serialize`] and [`Deserialize`] implementations from the [`serde`] crate
//! can be automatically generated by including `serde` in the argument list for the macro.
//!
//! [`serde`]: https://docs.rs/serde/*/serde/
//! [`Serialize`]: https://docs.rs/serde/*/serde/trait.Serialize.html
//! [`Deserialize`]: https://docs.rs/serde/*/serde/trait.Deserialize.html
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[braid(serde)]
//! pub struct Username;
//!
//! let username = Username::from_static("root");
//! let json = serde_json::to_string(&username).unwrap();
//! let new_username: Username = serde_json::from_str(&json).unwrap();
//!# assert_eq!(username, new_username);
//! ```
//!
//! Such automatic implementations will also properly handle string values that require
//! validation. This automatic validation has the benefit of easing use with _Serde_ while
//! still protecting the integrity of the type.
//!
//! ```
//!# use aliri_braid::braid;
//!#
//! #[derive(Debug, PartialEq, Eq)]
//! pub struct InvalidUsername;
//! // Error implementation elided
//!# impl std::fmt::Display for InvalidUsername {
//!# fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
//!# f.write_str("invalid username")
//!# }
//!# }
//!# impl std::error::Error for InvalidUsername {}
//!
//! #[braid(serde, validator)]
//! pub struct Username;
//!
//! impl aliri_braid::Validator for Username {
//! type Error = InvalidUsername;
//! fn validate(s: &str) -> Result<(), Self::Error> {
//! if s.is_empty() || s.eq_ignore_ascii_case("root") {
//! Err(InvalidUsername)
//! } else {
//! Ok(())
//! }
//! }
//! }
//!
//! assert!(serde_json::from_str::<Username>("\"\"").is_err());
//! assert!(serde_json::from_str::<Username>("\"root\"").is_err());
//! assert!(serde_json::from_str::<Username>("\"nobody\"").is_ok());
//!
//! assert!(serde_json::from_str::<&UsernameRef>("\"\"").is_err());
//! assert!(serde_json::from_str::<&UsernameRef>("\"root\"").is_err());
//! assert!(serde_json::from_str::<&UsernameRef>("\"nobody\"").is_ok());
//! ```
//!
//! # Custom string types
//!
//! The `braid` macro can be used to define a custom string type that wraps types
//! other than the standard `String`. This allows defining a braid that is backed
//! by a type that offers small-string optimizations, such as [`SmartString`].
//!
//! [`SmartString`]: https://docs.rs/smartstring/*/smartstring/struct.SmartString.html
//!
//! ```
//! # use aliri_braid::braid;
//! use smartstring::{SmartString, LazyCompact};
//!
//! #[braid]
//! pub struct UserId(SmartString<LazyCompact>);
//! ```
//!
//! It can also be used to wrap a [`ByteString`], which is a string backed by
//! [`Bytes`], which may be useful if the type is primarily used in contexts
//! where a zero-copy implementation is preferred.
//!
//! [`ByteString`]: https://docs.rs/bytestring/*/bytestring/struct.ByteString.html
//! [`Bytes`]: https://docs.rs/bytes/*/bytes/struct.Bytes.html
//!
//! ```
//! # use aliri_braid::braid;
//! use bytestring::ByteString;
//!
//! #[braid]
//! pub struct ZeroCopyIdentifier(ByteString);
//! ```
//!
//! ## Requirements
//!
//! In order to be used as a custom string type, the type must implement the
//! following traits:
//!
//! * [`core::clone::Clone`] (unless `clone` is `omit`)
//! * [`core::fmt::Debug`] (unless `debug` is `omit`)
//! * [`core::fmt::Display`] (unless `display` is `omit`)
//! * [`core::cmp::Eq`]
//! * [`core::cmp::PartialEq`]
//! * [`core::hash::Hash`]
//! * [`core::cmp::Ord`] (unless `ord` is `omit`)
//! * [`core::cmp::PartialOrd`] (unless `ord` is `omit`)
//! * [`serde::Serialize`] (unless `serde` is `omit`)
//! * [`serde::Deserialize`] (unless `serde` is `omit`)
//! * [`core::convert::From<&str>`]
//! * [`core::convert::From<Box<str>>`]
//! * [`core::convert::AsRef<str>`]
//! * [`core::convert::Into<String>`]
//!
//! [`serde::Serialize`]: https://docs.rs/serde/*/serde/trait.Serialize.html
//! [`serde::Deserialize`]: https://docs.rs/serde/*/serde/trait.Deserialize.html
//!
//! # `no_std` support
//!
//! Braids can be implemented in `no_std` environments with `alloc`. By adding the
//! `no_std` parameter to the macro, all impls will reference the `core` or `alloc`
//! crates instead of the `std` crate, as appropriate.
//!
//! ```
//! extern crate alloc;
//!
//! use aliri_braid::braid;
//! use alloc::string::String;
//!
//! #[braid(no_std)]
//! pub struct NoStdLibWrapper;
//! #
//! # fn main() {}
//! ```
//!
//! In environments without an allocator, `braid_ref` can be used to create a
//! reference-only braid. In order to remove the `alloc` dependency in `aliri_braid`,
//! specify `default-features = "false"` in the `Cargo.toml` file.
//!
//! ```
//! use aliri_braid::braid_ref;
//!
//! #[braid_ref(no_std)]
//! pub struct NoStdValue;
//! #
//! # fn main() {}
//! ```
//!
//! # Safety
//!
//! Braid uses limited `unsafe` in order to be able to reinterpret string slices
//! (`&str`) as the borrowed form. Because this functionality is provided as a
//! macro, using the `#![forbid(unsafe_code)]` lint level on a crate that generates
//! braids will result in compiler errors. Instead, the crate can be annotated with
//! `#![deny(unsafe_code)]`, which allows for overrides as appropriate. The functions
//! that require `unsafe` to work correctly are annotated with `#[allow(unsafe_code)]`,
//! and all usages of unsafe that the macro generates are annotated with `SAFETY`
//! code comments.
//!
//! If strict adherence to forbid unsafe code is required, then the types can be
//! segregated into an accessory crate without the prohibition, and then consumed
//! safely from crates that otherwise forbid unsafe code.
#![warn(
missing_docs,
unused_import_braces,
unused_imports,
unused_qualifications
)]
#![deny(
missing_debug_implementations,
trivial_casts,
trivial_numeric_casts,
unused_must_use
)]
#![deny(unsafe_code)]
#![no_std]
#[cfg(feature = "alloc")]
extern crate alloc;
/// A validator that can verify a given input is valid given certain preconditions
///
/// If the type can be normalized, this implementation should also validate that
/// the value is _already in normalized form_.
pub trait Validator {
/// The error produced when the string is invalid
type Error;
/// Validates a string according to a predetermined set of rules
///
/// # Errors
///
/// Returns an error if the string is invalid or not in normalized form.
fn validate(raw: &str) -> Result<(), Self::Error>;
}
/// A normalizer that can verify a given input is valid
/// and performs necessary normalization
#[cfg(feature = "alloc")]
pub trait Normalizer: Validator {
/// Validates and normalizes the borrowed input
///
/// # Errors
///
/// Returns an error if the string is invalid and cannot be normalized.
fn normalize(raw: &str) -> Result<::alloc::borrow::Cow<str>, Self::Error>;
}
pub use aliri_braid_impl::braid;
pub use aliri_braid_impl::braid_ref;