| 1 | |
| 2 |
|
| 3 | #[cfg(feature = "parsing")]
|
| 4 | use crate::error::Error;
|
| 5 | #[cfg(feature = "parsing")]
|
| 6 | use crate::error::Result;
|
| 7 | use crate::expr::Expr;
|
| 8 | use crate::mac::MacroDelimiter;
|
| 9 | #[cfg(feature = "parsing")]
|
| 10 | use crate::meta::{self, ParseNestedMeta};
|
| 11 | #[cfg(feature = "parsing")]
|
| 12 | use crate::parse::{Parse, ParseStream, Parser};
|
| 13 | use crate::path::Path;
|
| 14 | use crate::token;
|
| 15 | use proc_macro2::TokenStream;
|
| 16 | #[cfg(feature = "printing")]
|
| 17 | use std::iter;
|
| 18 | #[cfg(feature = "printing")]
|
| 19 | use std::slice;
|
| 20 |
|
| 21 | ast_struct! {
|
| 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 | #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))]
|
| 175 | pub struct Attribute {
|
| 176 | pub pound_token: Token![#],
|
| 177 | pub style: AttrStyle,
|
| 178 | pub bracket_token: token::Bracket,
|
| 179 | pub meta: Meta,
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | impl Attribute {
|
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | pub fn path(&self) -> &Path {
|
| 189 | self.meta.path()
|
| 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 | #[cfg(feature = "parsing")]
|
| 223 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 224 | pub fn parse_args<T: Parse>(&self) -> Result<T> {
|
| 225 | self.parse_args_with(T::parse)
|
| 226 | }
|
| 227 |
|
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | #[cfg(feature = "parsing")]
|
| 246 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 247 | pub fn parse_args_with<F: Parser>(&self, parser: F) -> Result<F::Output> {
|
| 248 | match &self.meta {
|
| 249 | Meta::Path(path) => Err(crate::error::new2(
|
| 250 | path.segments.first().unwrap().ident.span(),
|
| 251 | path.segments.last().unwrap().ident.span(),
|
| 252 | format!(
|
| 253 | "expected attribute arguments in parentheses: {}[{}(...)]",
|
| 254 | parsing::DisplayAttrStyle(&self.style),
|
| 255 | parsing::DisplayPath(path),
|
| 256 | ),
|
| 257 | )),
|
| 258 | Meta::NameValue(meta) => Err(Error::new(
|
| 259 | meta.eq_token.span,
|
| 260 | format_args!(
|
| 261 | "expected parentheses: {}[{}(...)]",
|
| 262 | parsing::DisplayAttrStyle(&self.style),
|
| 263 | parsing::DisplayPath(&meta.path),
|
| 264 | ),
|
| 265 | )),
|
| 266 | Meta::List(meta) => meta.parse_args_with(parser),
|
| 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 | #[cfg(feature = "parsing")]
|
| 392 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 393 | pub fn parse_nested_meta(
|
| 394 | &self,
|
| 395 | logic: impl FnMut(ParseNestedMeta) -> Result<()>,
|
| 396 | ) -> Result<()> {
|
| 397 | self.parse_args_with(meta::parser(logic))
|
| 398 | }
|
| 399 |
|
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | #[cfg(feature = "parsing")]
|
| 407 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 408 | pub fn parse_outer(input: ParseStream) -> Result<Vec<Self>> {
|
| 409 | let mut attrs = Vec::new();
|
| 410 | while input.peek(Token![#]) {
|
| 411 | attrs.push(input.call(parsing::single_parse_outer)?);
|
| 412 | }
|
| 413 | Ok(attrs)
|
| 414 | }
|
| 415 |
|
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | #[cfg(feature = "parsing")]
|
| 423 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 424 | pub fn parse_inner(input: ParseStream) -> Result<Vec<Self>> {
|
| 425 | let mut attrs = Vec::new();
|
| 426 | parsing::parse_inner(input, &mut attrs)?;
|
| 427 | Ok(attrs)
|
| 428 | }
|
| 429 | }
|
| 430 |
|
| 431 | ast_enum! {
|
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))]
|
| 447 | pub enum AttrStyle {
|
| 448 | Outer,
|
| 449 | Inner(Token![!]),
|
| 450 | }
|
| 451 | }
|
| 452 |
|
| 453 | ast_enum! {
|
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))]
|
| 475 | pub enum Meta {
|
| 476 | Path(Path),
|
| 477 |
|
| 478 | |
| 479 | List(MetaList),
|
| 480 |
|
| 481 | |
| 482 | NameValue(MetaNameValue),
|
| 483 | }
|
| 484 | }
|
| 485 |
|
| 486 | ast_struct! {
|
| 487 | |
| 488 | #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))]
|
| 489 | pub struct MetaList {
|
| 490 | pub path: Path,
|
| 491 | pub delimiter: MacroDelimiter,
|
| 492 | pub tokens: TokenStream,
|
| 493 | }
|
| 494 | }
|
| 495 |
|
| 496 | ast_struct! {
|
| 497 | |
| 498 | #[cfg_attr(docsrs, doc(cfg(any(feature = "full", feature = "derive"))))]
|
| 499 | pub struct MetaNameValue {
|
| 500 | pub path: Path,
|
| 501 | pub eq_token: Token![=],
|
| 502 | pub value: Expr,
|
| 503 | }
|
| 504 | }
|
| 505 |
|
| 506 | impl Meta {
|
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | pub fn path(&self) -> &Path {
|
| 512 | match self {
|
| 513 | Meta::Path(path) => path,
|
| 514 | Meta::List(meta) => &meta.path,
|
| 515 | Meta::NameValue(meta) => &meta.path,
|
| 516 | }
|
| 517 | }
|
| 518 |
|
| 519 | |
| 520 | #[cfg(feature = "parsing")]
|
| 521 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 522 | pub fn require_path_only(&self) -> Result<&Path> {
|
| 523 | let error_span = match self {
|
| 524 | Meta::Path(path) => return Ok(path),
|
| 525 | Meta::List(meta) => meta.delimiter.span().open(),
|
| 526 | Meta::NameValue(meta) => meta.eq_token.span,
|
| 527 | };
|
| 528 | Err(Error::new(error_span, "unexpected token in attribute"))
|
| 529 | }
|
| 530 |
|
| 531 | |
| 532 | #[cfg(feature = "parsing")]
|
| 533 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 534 | pub fn require_list(&self) -> Result<&MetaList> {
|
| 535 | match self {
|
| 536 | Meta::List(meta) => Ok(meta),
|
| 537 | Meta::Path(path) => Err(crate::error::new2(
|
| 538 | path.segments.first().unwrap().ident.span(),
|
| 539 | path.segments.last().unwrap().ident.span(),
|
| 540 | format!(
|
| 541 | "expected attribute arguments in parentheses: `{}(...)`",
|
| 542 | parsing::DisplayPath(path),
|
| 543 | ),
|
| 544 | )),
|
| 545 | Meta::NameValue(meta) => Err(Error::new(meta.eq_token.span, "expected `(`")),
|
| 546 | }
|
| 547 | }
|
| 548 |
|
| 549 | |
| 550 | #[cfg(feature = "parsing")]
|
| 551 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 552 | pub fn require_name_value(&self) -> Result<&MetaNameValue> {
|
| 553 | match self {
|
| 554 | Meta::NameValue(meta) => Ok(meta),
|
| 555 | Meta::Path(path) => Err(crate::error::new2(
|
| 556 | path.segments.first().unwrap().ident.span(),
|
| 557 | path.segments.last().unwrap().ident.span(),
|
| 558 | format!(
|
| 559 | "expected a value for this attribute: `{} = ...`",
|
| 560 | parsing::DisplayPath(path),
|
| 561 | ),
|
| 562 | )),
|
| 563 | Meta::List(meta) => Err(Error::new(meta.delimiter.span().open(), "expected `=`")),
|
| 564 | }
|
| 565 | }
|
| 566 | }
|
| 567 |
|
| 568 | impl MetaList {
|
| 569 | |
| 570 | #[cfg(feature = "parsing")]
|
| 571 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 572 | pub fn parse_args<T: Parse>(&self) -> Result<T> {
|
| 573 | self.parse_args_with(T::parse)
|
| 574 | }
|
| 575 |
|
| 576 | |
| 577 | #[cfg(feature = "parsing")]
|
| 578 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 579 | pub fn parse_args_with<F: Parser>(&self, parser: F) -> Result<F::Output> {
|
| 580 | let scope = self.delimiter.span().close();
|
| 581 | crate::parse::parse_scoped(parser, scope, self.tokens.clone())
|
| 582 | }
|
| 583 |
|
| 584 | |
| 585 | #[cfg(feature = "parsing")]
|
| 586 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 587 | pub fn parse_nested_meta(
|
| 588 | &self,
|
| 589 | logic: impl FnMut(ParseNestedMeta) -> Result<()>,
|
| 590 | ) -> Result<()> {
|
| 591 | self.parse_args_with(meta::parser(logic))
|
| 592 | }
|
| 593 | }
|
| 594 |
|
| 595 | #[cfg(feature = "printing")]
|
| 596 | pub(crate) trait FilterAttrs<'a> {
|
| 597 | type Ret: Iterator<Item = &'a Attribute>;
|
| 598 |
|
| 599 | fn outer(self) -> Self::Ret;
|
| 600 | #[cfg(feature = "full")]
|
| 601 | fn inner(self) -> Self::Ret;
|
| 602 | }
|
| 603 |
|
| 604 | #[cfg(feature = "printing")]
|
| 605 | impl<'a> FilterAttrs<'a> for &'a [Attribute] {
|
| 606 | type Ret = iter::Filter<slice::Iter<'a, Attribute>, fn(&&Attribute) -> bool>;
|
| 607 |
|
| 608 | fn outer(self) -> Self::Ret {
|
| 609 | fn is_outer(attr: &&Attribute) -> bool {
|
| 610 | match attr.style {
|
| 611 | AttrStyle::Outer => true,
|
| 612 | AttrStyle::Inner(_) => false,
|
| 613 | }
|
| 614 | }
|
| 615 | self.iter().filter(is_outer)
|
| 616 | }
|
| 617 |
|
| 618 | #[cfg(feature = "full")]
|
| 619 | fn inner(self) -> Self::Ret {
|
| 620 | fn is_inner(attr: &&Attribute) -> bool {
|
| 621 | match attr.style {
|
| 622 | AttrStyle::Inner(_) => true,
|
| 623 | AttrStyle::Outer => false,
|
| 624 | }
|
| 625 | }
|
| 626 | self.iter().filter(is_inner)
|
| 627 | }
|
| 628 | }
|
| 629 |
|
| 630 | impl From<Path> for Meta {
|
| 631 | fn from(meta: Path) -> Meta {
|
| 632 | Meta::Path(meta)
|
| 633 | }
|
| 634 | }
|
| 635 |
|
| 636 | impl From<MetaList> for Meta {
|
| 637 | fn from(meta: MetaList) -> Meta {
|
| 638 | Meta::List(meta)
|
| 639 | }
|
| 640 | }
|
| 641 |
|
| 642 | impl From<MetaNameValue> for Meta {
|
| 643 | fn from(meta: MetaNameValue) -> Meta {
|
| 644 | Meta::NameValue(meta)
|
| 645 | }
|
| 646 | }
|
| 647 |
|
| 648 | #[cfg(feature = "parsing")]
|
| 649 | pub(crate) mod parsing {
|
| 650 | use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
|
| 651 | use crate::error::Result;
|
| 652 | use crate::expr::{Expr, ExprLit};
|
| 653 | use crate::lit::Lit;
|
| 654 | use crate::parse::discouraged::Speculative as _;
|
| 655 | use crate::parse::{Parse, ParseStream};
|
| 656 | use crate::path::Path;
|
| 657 | use crate::{mac, token};
|
| 658 | use proc_macro2::Ident;
|
| 659 | use std::fmt::{self, Display};
|
| 660 |
|
| 661 | pub(crate) fn parse_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> {
|
| 662 | while input.peek(Token![#]) && input.peek2(Token![!]) {
|
| 663 | attrs.push(input.call(single_parse_inner)?);
|
| 664 | }
|
| 665 | Ok(())
|
| 666 | }
|
| 667 |
|
| 668 | pub(crate) fn single_parse_inner(input: ParseStream) -> Result<Attribute> {
|
| 669 | let content;
|
| 670 | Ok(Attribute {
|
| 671 | pound_token: input.parse()?,
|
| 672 | style: AttrStyle::Inner(input.parse()?),
|
| 673 | bracket_token: bracketed!(content in input),
|
| 674 | meta: content.parse()?,
|
| 675 | })
|
| 676 | }
|
| 677 |
|
| 678 | pub(crate) fn single_parse_outer(input: ParseStream) -> Result<Attribute> {
|
| 679 | let content;
|
| 680 | Ok(Attribute {
|
| 681 | pound_token: input.parse()?,
|
| 682 | style: AttrStyle::Outer,
|
| 683 | bracket_token: bracketed!(content in input),
|
| 684 | meta: content.parse()?,
|
| 685 | })
|
| 686 | }
|
| 687 |
|
| 688 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 689 | impl Parse for Meta {
|
| 690 | fn parse(input: ParseStream) -> Result<Self> {
|
| 691 | let path = parse_outermost_meta_path(input)?;
|
| 692 | parse_meta_after_path(path, input)
|
| 693 | }
|
| 694 | }
|
| 695 |
|
| 696 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 697 | impl Parse for MetaList {
|
| 698 | fn parse(input: ParseStream) -> Result<Self> {
|
| 699 | let path = parse_outermost_meta_path(input)?;
|
| 700 | parse_meta_list_after_path(path, input)
|
| 701 | }
|
| 702 | }
|
| 703 |
|
| 704 | #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
|
| 705 | impl Parse for MetaNameValue {
|
| 706 | fn parse(input: ParseStream) -> Result<Self> {
|
| 707 | let path = parse_outermost_meta_path(input)?;
|
| 708 | parse_meta_name_value_after_path(path, input)
|
| 709 | }
|
| 710 | }
|
| 711 |
|
| 712 | |
| 713 | |
| 714 | fn parse_outermost_meta_path(input: ParseStream) -> Result<Path> {
|
| 715 | if input.peek(Token![unsafe]) {
|
| 716 | let unsafe_token: Token![unsafe] = input.parse()?;
|
| 717 | Ok(Path::from(Ident::new("unsafe", unsafe_token.span)))
|
| 718 | } else {
|
| 719 | Path::parse_mod_style(input)
|
| 720 | }
|
| 721 | }
|
| 722 |
|
| 723 | pub(crate) fn parse_meta_after_path(path: Path, input: ParseStream) -> Result<Meta> {
|
| 724 | if input.peek(token::Paren) || input.peek(token::Bracket) || input.peek(token::Brace) {
|
| 725 | parse_meta_list_after_path(path, input).map(Meta::List)
|
| 726 | } else if input.peek(Token![=]) {
|
| 727 | parse_meta_name_value_after_path(path, input).map(Meta::NameValue)
|
| 728 | } else {
|
| 729 | Ok(Meta::Path(path))
|
| 730 | }
|
| 731 | }
|
| 732 |
|
| 733 | fn parse_meta_list_after_path(path: Path, input: ParseStream) -> Result<MetaList> {
|
| 734 | let (delimiter, tokens) = mac::parse_delimiter(input)?;
|
| 735 | Ok(MetaList {
|
| 736 | path,
|
| 737 | delimiter,
|
| 738 | tokens,
|
| 739 | })
|
| 740 | }
|
| 741 |
|
| 742 | fn parse_meta_name_value_after_path(path: Path, input: ParseStream) -> Result<MetaNameValue> {
|
| 743 | let eq_token: Token![=] = input.parse()?;
|
| 744 | let ahead = input.fork();
|
| 745 | let lit: Option<Lit> = ahead.parse()?;
|
| 746 | let value = if let (Some(lit), true) = (lit, ahead.is_empty()) {
|
| 747 | input.advance_to(&ahead);
|
| 748 | Expr::Lit(ExprLit {
|
| 749 | attrs: Vec::new(),
|
| 750 | lit,
|
| 751 | })
|
| 752 | } else if input.peek(Token![#]) && input.peek2(token::Bracket) {
|
| 753 | return Err(input.error("unexpected attribute inside of attribute"));
|
| 754 | } else {
|
| 755 | input.parse()?
|
| 756 | };
|
| 757 | Ok(MetaNameValue {
|
| 758 | path,
|
| 759 | eq_token,
|
| 760 | value,
|
| 761 | })
|
| 762 | }
|
| 763 |
|
| 764 | pub(super) struct DisplayAttrStyle<'a>(pub &'a AttrStyle);
|
| 765 |
|
| 766 | impl<'a> Display for DisplayAttrStyle<'a> {
|
| 767 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
| 768 | formatter.write_str(match self.0 {
|
| 769 | AttrStyle::Outer => "#",
|
| 770 | AttrStyle::Inner(_) => "#!",
|
| 771 | })
|
| 772 | }
|
| 773 | }
|
| 774 |
|
| 775 | pub(super) struct DisplayPath<'a>(pub &'a Path);
|
| 776 |
|
| 777 | impl<'a> Display for DisplayPath<'a> {
|
| 778 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
| 779 | for (i, segment) in self.0.segments.iter().enumerate() {
|
| 780 | if i > 0 || self.0.leading_colon.is_some() {
|
| 781 | formatter.write_str("::")?;
|
| 782 | }
|
| 783 | write!(formatter, "{}", segment.ident)?;
|
| 784 | }
|
| 785 | Ok(())
|
| 786 | }
|
| 787 | }
|
| 788 | }
|
| 789 |
|
| 790 | #[cfg(feature = "printing")]
|
| 791 | mod printing {
|
| 792 | use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
|
| 793 | use crate::path;
|
| 794 | use crate::path::printing::PathStyle;
|
| 795 | use proc_macro2::TokenStream;
|
| 796 | use quote::ToTokens;
|
| 797 |
|
| 798 | #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
|
| 799 | impl ToTokens for Attribute {
|
| 800 | fn to_tokens(&self, tokens: &mut TokenStream) {
|
| 801 | self.pound_token.to_tokens(tokens);
|
| 802 | if let AttrStyle::Inner(b) = &self.style {
|
| 803 | b.to_tokens(tokens);
|
| 804 | }
|
| 805 | self.bracket_token.surround(tokens, |tokens| {
|
| 806 | self.meta.to_tokens(tokens);
|
| 807 | });
|
| 808 | }
|
| 809 | }
|
| 810 |
|
| 811 | #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
|
| 812 | impl ToTokens for Meta {
|
| 813 | fn to_tokens(&self, tokens: &mut TokenStream) {
|
| 814 | match self {
|
| 815 | Meta::Path(path) => path::printing::print_path(tokens, path, PathStyle::Mod),
|
| 816 | Meta::List(meta_list) => meta_list.to_tokens(tokens),
|
| 817 | Meta::NameValue(meta_name_value) => meta_name_value.to_tokens(tokens),
|
| 818 | }
|
| 819 | }
|
| 820 | }
|
| 821 |
|
| 822 | #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
|
| 823 | impl ToTokens for MetaList {
|
| 824 | fn to_tokens(&self, tokens: &mut TokenStream) {
|
| 825 | path::printing::print_path(tokens, &self.path, PathStyle::Mod);
|
| 826 | self.delimiter.surround(tokens, self.tokens.clone());
|
| 827 | }
|
| 828 | }
|
| 829 |
|
| 830 | #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
|
| 831 | impl ToTokens for MetaNameValue {
|
| 832 | fn to_tokens(&self, tokens: &mut TokenStream) {
|
| 833 | path::printing::print_path(tokens, &self.path, PathStyle::Mod);
|
| 834 | self.eq_token.to_tokens(tokens);
|
| 835 | self.value.to_tokens(tokens);
|
| 836 | }
|
| 837 | }
|
| 838 | }
|