From 314ee051c170c33d2c912b6750f916b73f9cb507 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 25 Jan 2020 15:35:02 +0000 Subject: fewer temporary variables we can `return` from an inner block, even when that block is executed somewhere else: `return` is lexical! --- lib/MaildirIndexer/Parser.rakumod | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'lib/MaildirIndexer/Parser.rakumod') diff --git a/lib/MaildirIndexer/Parser.rakumod b/lib/MaildirIndexer/Parser.rakumod index 407078d..1762e58 100644 --- a/lib/MaildirIndexer/Parser.rakumod +++ b/lib/MaildirIndexer/Parser.rakumod @@ -61,34 +61,32 @@ my class Message-actions { } multi parse-email(IO::Path:D $p --> MaildirIndexer::Email) is export { - my MaildirIndexer::Email $result; - MaildirIndexer::LogTimelineSchema::Parse::Email::File.log: :file($p.path), -> { + MaildirIndexer::LogTimelineSchema::Parse::Email::File.log: :file($p.path), { # as of 2019,11, `slurp` replaces all \r\n with \n ?? - $result = parse-email($p.slurp(:enc), path => $p); + return parse-email($p.slurp(:enc), path => $p); } - return $result; } multi parse-email(IO::Path:D $p, :$headers-only! --> MaildirIndexer::Email) is export { - my MaildirIndexer::Email $result; - MaildirIndexer::LogTimelineSchema::Parse::Email::File.log: :file($p.path), -> { - my IO::Handle $h = $p.open( + # due to a bug, we can't put that LEAVE inside the inner block; + # see https://github.com/rakudo/rakudo/issues/2380 + my IO::Handle $h; LEAVE { .close with $h }; + MaildirIndexer::LogTimelineSchema::Parse::Email::File.log: :file($p.path), { + $h = $p.open( :enc, :nl-in(@separators), :!chomp, ); - $result = parse-email( + return parse-email( $h.lines()[0], path => $p, ); - $h.close(); } - return $result; } multi parse-email(IO::Socket::Async:D $s --> MaildirIndexer::Email) is export { - my MaildirIndexer::Email $result; - MaildirIndexer::LogTimelineSchema::Parse::Email::Socket.log: -> { + MaildirIndexer::LogTimelineSchema::Parse::Email::Socket.log: { + my MaildirIndexer::Email $result; my $string; react { whenever $s.Supply(:enc) { @@ -99,17 +97,18 @@ multi parse-email(IO::Socket::Async:D $s --> MaildirIndexer::Email) is export { $result = parse-email($string) and done; } } + return $result; } - return $result; } multi parse-email(Str:D $email-str, :$path = IO --> MaildirIndexer::Email) is export { - my MaildirIndexer::Email $result; - MaildirIndexer::LogTimelineSchema::Parse::Email::Str.log: -> { - CATCH { warn $_; return Nil }; + MaildirIndexer::LogTimelineSchema::Parse::Email::Str.log: { + CATCH { return .fail }; with Message.parse($email-str,:actions(Message-actions.new(:$path))) { - $result = .made; + return .made; + } + else { + return Nil; } } - return $result; } -- cgit v1.2.3