| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#; |
| 2 | |
| 3 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.107 crate/::{Spanrust-analyzer cargo proc-macro2 1.0.107 Span#, TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#}; |
| 4 | use quoterust-analyzer cargo quote 1.0.47 crate/::ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens#; |
| 5 | use synrust-analyzer cargo syn 2.0.119 crate/::{spannedrust-analyzer cargo syn 2.0.119 spanned/::Spannedrust-analyzer cargo syn 2.0.119 spanned/Spanned#, Blockrust-analyzer cargo syn 2.0.119 stmt/Block#, LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#}; |
| 6 | |
| 7 | /// A wrapper for expressions/blocks which automatically adds the start and end |
| 8 | /// braces. |
| 9 | /// |
| 10 | /// - **full access** to variables environment. |
| 11 | /// - **full access** to control-flow of the environment via `return`, `?` etc. |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 13 | pub struct BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#(Blockrust-analyzer cargo syn 2.0.119 stmt/Block#); |
| 14 | |
| 15 | impl BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents# { |
| 16 | pub fn is_emptyrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents]is_empty().(&self) -> bool { |
| 17 | self.0rust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#0..stmtsrust-analyzer cargo syn 2.0.119 stmt/Block#stmts..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() |
| 18 | } |
| 19 | |
| 20 | pub fn spanrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents]span().(&self) -> Spanrust-analyzer cargo proc-macro2 1.0.107 Span# { |
| 21 | self.0rust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#0..spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl ToTokensrust-analyzer cargo quote 1.0.47 to_tokens/ToTokens# for BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents# { |
| 26 | fn to_tokensrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][ToTokens]to_tokens().(&self, tokens: &mut TokenStreamrust-analyzer cargo proc-macro2 1.0.107 TokenStream#) { |
| 27 | self.0rust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#0..to_tokensrust-analyzer cargo syn 2.0.119 stmt/printing/impl#[Block][ToTokens]to_tokens().(tokens) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<&'_ LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#> for BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents# { |
| 32 | type Errorrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`TryFrom<&LitStr>`]Error# = synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#; |
| 33 | |
| 34 | fn try_fromrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`TryFrom<&LitStr>`]try_from().(s: &LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`TryFrom<&LitStr>`], Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`TryFrom<&LitStr>`]::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> { |
| 35 | let mut block_str = s.valuerust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]value().(); |
| 36 | block_str.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]insert().(0, '{'); |
| 37 | block_str.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('}'); |
| 38 | LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#::newrust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]new().(&block_str, s.spanrust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]span().()).parserust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]parse().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`TryFrom<&LitStr>`]) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#> for BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents# { |
| 43 | fn fromrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`From<syn::Expr>`]from().(v: synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#) -> Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`From<syn::Expr>`] { |
| 44 | Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`From<syn::Expr>`](Blockrust-analyzer cargo syn 2.0.119 stmt/Block# { |
| 45 | brace_tokenrust-analyzer cargo syn 2.0.119 stmt/Block#brace_token.: synrust-analyzer cargo syn 2.0.119 crate/::tokenrust-analyzer cargo syn 2.0.119 token/::Bracerust-analyzer cargo syn 2.0.119 token/Brace().(v.spanrust-analyzer cargo syn 2.0.119 spanned/impl#[T][Spanned]span().()), |
| 46 | stmtsrust-analyzer cargo syn 2.0.119 stmt/Block#stmts.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![synrust-analyzer cargo syn 2.0.119 crate/::Stmtrust-analyzer cargo syn 2.0.119 stmt/Stmt#::Exprrust-analyzer cargo syn 2.0.119 stmt/Stmt#Expr#(v, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)], |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl darlingrust-analyzer cargo darling 0.23.0 crate/::FromMetarust-analyzer cargo darling_core 0.23.0 from_meta/FromMeta# for BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents# { |
| 52 | fn from_valuerust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][FromMeta]from_value().(value: &synrust-analyzer cargo syn 2.0.119 crate/::Litrust-analyzer cargo syn 2.0.119 lit/Lit#) -> darlingrust-analyzer cargo darling 0.23.0 crate/::Resultrust-analyzer cargo darling_core 0.23.0 error/Result#<Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][FromMeta]> { |
| 53 | if let synrust-analyzer cargo syn 2.0.119 crate/::Litrust-analyzer cargo syn 2.0.119 lit/Lit#::Strrust-analyzer cargo syn 2.0.119 lit/Lit#Str#(s) = value { |
| 54 | let contents = BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#::try_fromrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`TryFrom<&LitStr>`]try_from().(s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 55 | if contents.is_emptyrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents]is_empty().() { |
| 56 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(darlingrust-analyzer cargo darling 0.23.0 crate/::Errorrust-analyzer cargo darling_core 0.23.0 error/Error#::unknown_valuerust-analyzer cargo darling_core 0.23.0 error/impl#[Error]unknown_value().("").with_spanrust-analyzer cargo darling_core 0.23.0 error/impl#[Error]with_span().(s)) |
| 57 | } else { |
| 58 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(contents) |
| 59 | } |
| 60 | } else { |
| 61 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(darlingrust-analyzer cargo darling 0.23.0 crate/::Errorrust-analyzer cargo darling_core 0.23.0 error/Error#::unexpected_lit_typerust-analyzer cargo darling_core 0.23.0 error/impl#[Error]unexpected_lit_type().(value)) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn from_exprrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][FromMeta]from_expr().(expr: &synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#) -> darlingrust-analyzer cargo darling 0.23.0 crate/::Resultrust-analyzer cargo darling_core 0.23.0 error/Result#<Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][FromMeta]> { |
| 66 | if let synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#::Litrust-analyzer cargo syn 2.0.119 expr/Expr#Lit#(lit) = expr { |
| 67 | Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][FromMeta]::from_valuerust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][FromMeta]from_value().(&lit.litrust-analyzer cargo syn 2.0.119 expr/ExprLit#lit.) |
| 68 | } else { |
| 69 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][FromMeta]::fromrust-analyzer cargo derive_builder_core 0.20.2 block/impl#[BlockContents][`From<syn::Expr>`]from().(expr.clonerust-analyzer cargo syn 2.0.119 gen/clone/impl#[`crate::Expr`][Clone]clone().())) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #[cfg(test)] |
| 75 | mod testrust-analyzer cargo derive_builder_core 0.20.2 block/test/ { |
| 76 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryIntorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#; |
| 77 | |
| 78 | use superrust-analyzer cargo derive_builder_core 0.20.2 block/::*; |
| 79 | |
| 80 | fn parserust-analyzer cargo derive_builder_core 0.20.2 block/test/parse().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<BlockContentsrust-analyzer cargo derive_builder_core 0.20.2 block/BlockContents#, synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#> { |
| 81 | (&LitStrrust-analyzer cargo syn 2.0.119 lit/LitStr#::newrust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]new().(s, Spanrust-analyzer cargo proc-macro2 1.0.107 Span#::call_siterust-analyzer cargo proc-macro2 1.0.107 impl#[Span]call_site().())).try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() |
| 82 | } |
| 83 | |
| 84 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 85 | #[should_panic(expected = r#"cannot parse"#)] |
| 86 | fn block_invalid_token_treesrust-analyzer cargo derive_builder_core 0.20.2 block/test/block_invalid_token_trees().() { |
| 87 | parserust-analyzer cargo derive_builder_core 0.20.2 block/test/parse().("let x = 2; { x+1").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 88 | } |
| 89 | |
| 90 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 91 | fn block_delimited_token_treerust-analyzer cargo derive_builder_core 0.20.2 block/test/block_delimited_token_tree().() { |
| 92 | let expr = parserust-analyzer cargo derive_builder_core 0.20.2 block/test/parse().("let x = 2; { x+1 }").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 93 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 94 | quoterust-analyzer cargo quote 1.0.47 quote!!(#expr).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 95 | quoterust-analyzer cargo quote 1.0.47 quote!!({ |
| 96 | let x = 2; |
| 97 | { |
| 98 | x + 1 |
| 99 | } |
| 100 | }) |
| 101 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 106 | fn block_single_token_treerust-analyzer cargo derive_builder_core 0.20.2 block/test/block_single_token_tree().() { |
| 107 | let expr = parserust-analyzer cargo derive_builder_core 0.20.2 block/test/parse().("42").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 108 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(quoterust-analyzer cargo quote 1.0.47 quote!!(#expr).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), quoterust-analyzer cargo quote 1.0.47 quote!!({ 42 }).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 109 | } |
| 110 | } |