Skip to content

Commit

Permalink
Introducing IO::Path::Parts
Browse files Browse the repository at this point in the history
- takes a volume, dirname and basename into a single object
- should replace List of Pairs approach used by IO::Spec::X.split
- completely compatible with List of Pairs interface for now

Did not use the improved interface in IO::Path yet.  Since this commit
is spectest clean, it proves that the List of Pairs interface is working.
Any ecosystem module out there should also have the benefit of this
interface, which should probably need to have an official deprecation
cycle.
  • Loading branch information
lizmat committed May 22, 2020
1 parent c821bbc commit 4387f28
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/core.c/IO/Path/Parts.pm6
@@ -0,0 +1,52 @@
class IO::Path::Parts
does Positional # all of these can go as soon as we don't need the compat
does Iterable # with the original "List of Pairs" implemention anymore.
{
has str $.volume;
has str $.dirname;
has str $.basename;

method !SET-SELF($!volume, $!dirname, $!basename) { self }
method new(\volume, \dirname, \basename) {
nqp::create(self)!SET-SELF(volume, dirname, basename)
}

method raku() {
'IO::Path::Parts.new('
~ $!volume.raku
~ ','
~ $!dirname.raku
~ ','
~ $!basename.raku
~ ')'
}

#-------------------------------------------------------------------------------
# all of the code below is just to provide a compatibility layer with the
# original List of Pairs / Map implementation. As soon as this is no longer
# needed, this can go.

method by-index(int $pos) is implementation-detail {
$pos == 2
?? :$!basename
!! $pos == 1
?? :$!dirname
!! $pos
?? Nil
!! :$!volume
}

class Iterate does Iterator {
has $!parts;
has int $!i;
method new(\parts) {
nqp::p6bindattrinvres(nqp::create(self),self,'$!parts',parts)
}
method pull-one() { $!i < 3 ?? $!parts.by-index($!i++) !! IterationEnd }
}
method iterator() { Iterate.new(self) }

method AT-POS(int $pos) { self.by-index($pos) }
}

# vim: ft=perl6 expandtab sw=4
2 changes: 1 addition & 1 deletion src/core.c/IO/Spec/Unix.pm6
Expand Up @@ -173,7 +173,7 @@ my class IO::Spec::Unix is IO::Spec {
);

# shell dirname '' produces '.', but we don't because it's probably user error
(:volume(''), :$dirname, :$basename);
IO::Path::Parts.new('', $dirname, $basename)
}

method join ($, \dir, \file) {
Expand Down
2 changes: 1 addition & 1 deletion src/core.c/IO/Spec/Win32.pm6
Expand Up @@ -125,7 +125,7 @@ my class IO::Spec::Win32 is IO::Spec::Unix {
$basename && nqp::isfalse($dirname),
$dirname = '.'));

(:$volume, :$dirname, :$basename)
IO::Path::Parts.new($volume, $dirname, $basename)
}

method join (Str \vol, Str $dir is copy, Str $file is copy) {
Expand Down
1 change: 1 addition & 0 deletions tools/templates/6.c/core_sources
Expand Up @@ -104,6 +104,7 @@ src/core.c/Regex.pm6
src/core.c/allomorphs.pm6
src/core.c/IO.pm6
src/core.c/IO/Spec.pm6
src/core.c/IO/Path/Parts.pm6
src/core.c/IO/Spec/Unix.pm6
src/core.c/IO/Spec/Win32.pm6
src/core.c/IO/Spec/Cygwin.pm6
Expand Down

0 comments on commit 4387f28

Please sign in to comment.