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/Email.rakumod | 8 ++----- lib/MaildirIndexer/Index/ByAddresses.rakumod | 11 ++++----- lib/MaildirIndexer/Index/ByRef.rakumod | 11 ++++----- lib/MaildirIndexer/Parser.rakumod | 35 ++++++++++++++-------------- lib/MaildirIndexer/ScanDir.rakumod | 2 +- lib/MaildirIndexer/Store.rakumod | 17 +++++++------- t/store.t | 2 +- 7 files changed, 39 insertions(+), 47 deletions(-) diff --git a/lib/MaildirIndexer/Email.rakumod b/lib/MaildirIndexer/Email.rakumod index 4f92106..aabf185 100644 --- a/lib/MaildirIndexer/Email.rakumod +++ b/lib/MaildirIndexer/Email.rakumod @@ -18,11 +18,9 @@ method refs(--> Iterable) { multi split-refs(Any --> Iterable) { return () } multi split-refs(Str:D $str --> Iterable) { - my @result; MaildirIndexer::LogTimelineSchema::Parse::Header.log: { - @result = $/».Str if $str ~~ m{'<' $ = (<-[<>]>+)+ % [ '>' .*? '<' ] '>' }; + return $/».Str if $str ~~ m{'<' $ = (<-[<>]>+)+ % [ '>' .*? '<' ] '>' }; } - return @result; } method addresses (--> Iterable) { @@ -59,11 +57,9 @@ my grammar Address { multi sub extract-addresses(Any --> Iterable) { return () } multi sub extract-addresses(Str:D $str --> Iterable) { - my @result; MaildirIndexer::LogTimelineSchema::Parse::Header.log: { with Address.parse($str) { - @result = $_».Str; + return $_».Str; } } - return @result; } diff --git a/lib/MaildirIndexer/Index/ByAddresses.rakumod b/lib/MaildirIndexer/Index/ByAddresses.rakumod index 40e98f9..c6cc412 100644 --- a/lib/MaildirIndexer/Index/ByAddresses.rakumod +++ b/lib/MaildirIndexer/Index/ByAddresses.rakumod @@ -41,7 +41,7 @@ submethod account-for(Str @addresses,Str $mailbox,Int $step) { } method add-mail(MaildirIndexer::Email:D $email, Str:D $mailbox --> Nil) { - MaildirIndexer::LogTimelineSchema::Index::Add.log: :class('ByAddresses'),:$mailbox, -> { + MaildirIndexer::LogTimelineSchema::Index::Add.log: :class('ByAddresses'),:$mailbox, { # ignore adding the same file twice, files in maildirs are # immutable return if %!addresses-for-file{ $email.path }:exists; @@ -56,7 +56,7 @@ method add-mail(MaildirIndexer::Email:D $email, Str:D $mailbox --> Nil) { } method del-path(IO:D $file, Str:D $mailbox --> Nil) { - MaildirIndexer::LogTimelineSchema::Index::Rm.log: :class('ByAddresses'),:$mailbox, -> { + MaildirIndexer::LogTimelineSchema::Index::Rm.log: :class('ByAddresses'),:$mailbox, { # using assignment would fail when the path isn't present in # the hash, because it tries to assign the (undefined) # Array[Str] as a single element, instead of splatting it; @@ -93,13 +93,12 @@ submethod predict-mailbox-given-addresses(@addresses) { } method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { - my Str $result; - MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByAddresses'), -> { + MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByAddresses'), { my %prediction = self.predict-mailbox-given-addresses($email.addresses); my @most-probable-mailboxes = %prediction.pairs.sort(*.value); - if @most-probable-mailboxes -> $_ { $result = .[*-1].key } + if @most-probable-mailboxes -> $_ { return .[*-1].key } + else { return Nil } } - return $result; } diff --git a/lib/MaildirIndexer/Index/ByRef.rakumod b/lib/MaildirIndexer/Index/ByRef.rakumod index 6e5b7f5..3a5ebdb 100644 --- a/lib/MaildirIndexer/Index/ByRef.rakumod +++ b/lib/MaildirIndexer/Index/ByRef.rakumod @@ -13,7 +13,7 @@ method dump() { } method add-mail(MaildirIndexer::Email:D $email, Str:D $mailbox --> Nil) { - MaildirIndexer::LogTimelineSchema::Index::Add.log: :class('ByRef'),:$mailbox, -> { + MaildirIndexer::LogTimelineSchema::Index::Add.log: :class('ByRef'),:$mailbox, { # ignore adding the same file twice, files in maildirs are # immutable return if %!id-for-file{ $email.path }:exists; @@ -26,7 +26,7 @@ method add-mail(MaildirIndexer::Email:D $email, Str:D $mailbox --> Nil) { } method del-path(IO:D $file, Str:D $mailbox --> Nil) { - MaildirIndexer::LogTimelineSchema::Index::Rm.log: :class('ByRef'),:$mailbox, -> { + MaildirIndexer::LogTimelineSchema::Index::Rm.log: :class('ByRef'),:$mailbox, { my $id = %!id-for-file{ $file.path }:delete or return; with %!mailboxes-for-id{ $id } -> $boxes { $boxes{$mailbox}:delete; @@ -36,11 +36,10 @@ method del-path(IO:D $file, Str:D $mailbox --> Nil) { } method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { - my Str $result; - MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByRef'), -> { + MaildirIndexer::LogTimelineSchema::Index::Find.log: :class('ByRef'), { for |$email.refs() -> $ref { - with %!mailboxes-for-id{$ref} { $result = .keys.sort.[0] } + with %!mailboxes-for-id{$ref} { return .keys.sort.[0] } } + return Nil; } - return $result; } 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; } diff --git a/lib/MaildirIndexer/ScanDir.rakumod b/lib/MaildirIndexer/ScanDir.rakumod index f0ea0b7..4d6230d 100644 --- a/lib/MaildirIndexer/ScanDir.rakumod +++ b/lib/MaildirIndexer/ScanDir.rakumod @@ -7,7 +7,7 @@ sub scan-dir(*@paths --> Supply) is export { supply { my %watched-dirs; - CATCH { when X::IO { }; default { warn $_; done } } + CATCH { when X::IO { }; default { warn .gist; done } } sub start-watching(IO::Path $dir) { %watched-dirs{$dir.Str} = True; diff --git a/lib/MaildirIndexer/Store.rakumod b/lib/MaildirIndexer/Store.rakumod index 27ec915..66ec086 100644 --- a/lib/MaildirIndexer/Store.rakumod +++ b/lib/MaildirIndexer/Store.rakumod @@ -19,18 +19,18 @@ method dump(--> Nil) { method start(--> Nil) { for ^$.workers { start react { - CATCH { warn $_ }; + CATCH { warn .gist }; whenever $.file-channel -> $file { when $file ~~ MaildirIndexer::ScanDir::End { MaildirIndexer::LogTimelineSchema::Scan::End.log(); } when $file ~~ :e & :f { - MaildirIndexer::LogTimelineSchema::Store::Add.log: :file($file.path), -> { + MaildirIndexer::LogTimelineSchema::Store::Add.log: :file($file.path), { self.add-file($file); } } when $file ~~ :!e { - MaildirIndexer::LogTimelineSchema::Store::Rm.log: :file($file.path), -> { + MaildirIndexer::LogTimelineSchema::Store::Rm.log: :file($file.path), { self.del-file($file); } } @@ -40,9 +40,9 @@ method start(--> Nil) { } method add-file(IO:D $file --> Nil) { - my $mailbox = mailbox-from-path($file.path) or return; - my $email = parse-email($file,:headers-only) or return; - CATCH { warn $_ }; + my Str $mailbox = mailbox-from-path($file.path) or return; + my MaildirIndexer::Email $email = parse-email($file,:headers-only) or return; + CATCH { warn .gist; return }; $!lock.protect: { .add-mail($email,$mailbox) for @!indices; } @@ -58,15 +58,14 @@ method del-file(IO:D $file --> Nil) { } method mailbox-for-email(MaildirIndexer::Email:D $email --> Str) { - my Str $result; MaildirIndexer::LogTimelineSchema::Store::Find.log: { $!lock.protect: { for @!indices -> $index { - with $index.mailbox-for-email($email) { $result = $_; last }; + with $index.mailbox-for-email($email) { return $_ }; } } + return Nil; } - return $result; } sub mailbox-from-path(Str() $path --> Str) { diff --git a/t/store.t b/t/store.t index d5b2502..8544442 100644 --- a/t/store.t +++ b/t/store.t @@ -42,7 +42,7 @@ subtest 'finding' => { is-deeply( @responses, - $['1','2','1',Str], + $['1','2','1',Nil], 'indexes are consulted until a defined value', ); } -- cgit v1.2.3