aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2020-02-01 14:49:43 +0100
committerPatrick Spek <p.spek@tyil.nl>2020-02-01 14:49:43 +0100
commitb8c744b233f9bb566b32541ef25fcfc99db32e18 (patch)
tree0170ba34d54a8de46a9e99c3a6849eb5f9360cdb
parent15fad7e951362a2f898ac29b41fb86dc84f2ee87 (diff)
downloadApp::GTD-b8c744b233f9bb566b32541ef25fcfc99db32e18.tar.gz
App::GTD-b8c744b233f9bb566b32541ef25fcfc99db32e18.tar.bz2
Parse todo.txt line-by-line
This is to cleanly skip over incorrect lines, allowing the application to work more often even if the todo.txt file itself is not completely valid according to the spec. In this particular case, it is to skip over empty lines.
-rw-r--r--lib/App/GTD.pm629
-rw-r--r--lib/App/GTD/GrammarActions.pm66
-rw-r--r--t/objectification.t2
3 files changed, 28 insertions, 9 deletions
diff --git a/lib/App/GTD.pm6 b/lib/App/GTD.pm6
index e639bbf..6f547c0 100644
--- a/lib/App/GTD.pm6
+++ b/lib/App/GTD.pm6
@@ -75,27 +75,42 @@ multi sub gtd-config (
$config.get($key)
}
-sub gtd-parse-todo (
- Str:D $input,
- --> List
+multi sub gtd-parse-todo (
+ Str:D $line,
+ --> Iterable
+) is export {
+ samewith([$line]);
+}
+
+multi sub gtd-parse-todo (
+ @lines,
+ --> Iterable
) is export {
- Grammar::TodoTxt.parse($input, actions => App::GTD::GrammarActions).made
+ @lines
+ .map({
+ Grammar::TodoTxt
+ .parse($_, actions => App::GTD::GrammarActions)
+ .made
+ })
+ .grep(*.defined)
}
sub gtd-records-read (
- --> List
+ --> Iterable
) is export {
my $file = gtd-config('data.file').IO;
return unless $file.e;
- gtd-parse-todo($file.slurp)
+ gtd-parse-todo($file.lines)
}
sub gtd-records-write (
@records,
) is export {
- gtd-config('data.file').IO.spurt(@records.grep(*.defined).join("\n") ~ "\n")
+ gtd-config('data.file')
+ .IO
+ .spurt(@records.grep(*.defined).join("\n") ~ "\n")
}
sub gtd-records-string (
diff --git a/lib/App/GTD/GrammarActions.pm6 b/lib/App/GTD/GrammarActions.pm6
index e061190..7a372c4 100644
--- a/lib/App/GTD/GrammarActions.pm6
+++ b/lib/App/GTD/GrammarActions.pm6
@@ -10,6 +10,10 @@ my $line-number = 1;
method TOP ($/)
{
+ # In App::GTD, only a single line is parsed at a time, so you can work
+ # under the assumption that you will always have only one line to parse,
+ # and thus, should only return a single record.
+
make $<records>
.map({
my $record = $_.made<record>;
@@ -40,7 +44,7 @@ method TOP ($/)
App::GTD::Record.new(|%data)
})
- .List
+ .first
}
method record ($/)
diff --git a/t/objectification.t b/t/objectification.t
index 19234fe..803192e 100644
--- a/t/objectification.t
+++ b/t/objectification.t
@@ -12,7 +12,7 @@ EOF
plan 1;
-my @records = gtd-parse-todo($input);
+my @records = gtd-parse-todo($input.lines);
is @records.elems, $input.lines.elems, 'All lines parsed to records';