Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  bump psalm version
  add Frame::filter
  add frames
  add Stream::unwrap()
  flag pure code
  • Loading branch information
Baptouuuu committed Nov 25, 2023
2 parents 379dbcb + 74a9774 commit 9f75a40
Show file tree
Hide file tree
Showing 20 changed files with 1,000 additions and 20 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## 2.3.0 - 2023-10-25

### Added

- `Innmind\IO\Readable\Stream::unwrap()`
- `Innmind\IO\Readable\Stream::frames()`
- `Innmind\IO\Readable\Frame`
- `Innmind\IO\Readable\Frames`
- `Innmind\IO\Readable\Frame\Chunk`
- `Innmind\IO\Readable\Frame\Chunks`
- `Innmind\IO\Readable\Frame\Composite`
- `Innmind\IO\Readable\Frame\FlatMap`
- `Innmind\IO\Readable\Frame\Line`
- `Innmind\IO\Readable\Frame\Map`
- `Innmind\IO\Readable\Frame\Sequence`

## 2.2.0 - 2023-10-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"require-dev": {
"phpunit/phpunit": "~10.2",
"vimeo/psalm": "~5.6",
"vimeo/psalm": "~5.15",
"innmind/black-box": "~5.5",
"innmind/coding-standard": "~2.0"
}
Expand Down
7 changes: 7 additions & 0 deletions src/IO.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class IO
private $watch;

/**
* @psalm-mutation-free
*
* @param callable(?ElapsedPeriod): Watch $watch
*/
private function __construct(callable $watch)
Expand All @@ -20,13 +22,18 @@ private function __construct(callable $watch)
}

/**
* @psalm-pure
*
* @param callable(?ElapsedPeriod): Watch $watch
*/
public static function of(callable $watch): self
{
return new self($watch);
}

/**
* @psalm-mutation-free
*/
public function readable(): Readable
{
return Readable::of($this->watch);
Expand Down
3 changes: 3 additions & 0 deletions src/Readable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ final class Readable
private $watch;

/**
* @psalm-mutation-free
*
* @param callable(?ElapsedPeriod): Watch $watch
*/
private function __construct(callable $watch)
Expand All @@ -24,6 +26,7 @@ private function __construct(callable $watch)

/**
* @internal
* @psalm-pure
*
* @param callable(?ElapsedPeriod): Watch $watch
*/
Expand Down
18 changes: 15 additions & 3 deletions src/Readable/Chunks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
Either,
};

/**
* @template-covariant T of LowLevelStream
*/
final class Chunks
{
/** @var T */
private LowLevelStream $stream;
/** @var callable(LowLevelStream): Maybe<LowLevelStream> */
/** @var callable(T): Maybe<T> */
private $ready;
/** @var Maybe<Str\Encoding> */
private Maybe $encoding;
Expand All @@ -24,7 +28,8 @@ final class Chunks
/**
* @psalm-mutation-free
*
* @param callable(LowLevelStream): Maybe<LowLevelStream> $ready
* @param T $stream
* @param callable(T): Maybe<T> $ready
* @param Maybe<Str\Encoding> $encoding
* @param positive-int $size
*/
Expand All @@ -43,10 +48,14 @@ private function __construct(
/**
* @psalm-mutation-free
* @internal
* @template A of LowLevelStream
*
* @param callable(LowLevelStream): Maybe<LowLevelStream> $ready
* @param A $stream
* @param callable(A): Maybe<A> $ready
* @param Maybe<Str\Encoding> $encoding
* @param positive-int $size
*
* @return self<A>
*/
public static function of(
LowLevelStream $stream,
Expand Down Expand Up @@ -101,6 +110,9 @@ public function fold(
return $finished->flatMap(static fn($fold) => $fold->maybe());
}

/**
* @psalm-mutation-free
*/
public function lazy(): Chunks\Lazy
{
return Chunks\Lazy::of(
Expand Down
24 changes: 19 additions & 5 deletions src/Readable/Chunks/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,30 @@
Maybe,
};

/**
* @template-covariant T of LowLevelStream
*/
final class Lazy
{
/** @var T */
private LowLevelStream $stream;
/** @var callable(LowLevelStream): Maybe<LowLevelStream> */
/** @var callable(T): Maybe<T> */
private $ready;
/** @var Maybe<Str\Encoding> */
private Maybe $encoding;
/** @var positive-int */
private int $size;
/** @var callable(LowLevelStream): void */
/** @var callable(T): void */
private $rewind;

/**
* @psalm-mutation-free
*
* @param callable(LowLevelStream): Maybe<LowLevelStream> $ready
* @param T $stream
* @param callable(T): Maybe<T> $ready
* @param Maybe<Str\Encoding> $encoding
* @param positive-int $size
* @param callable(LowLevelStream): void $rewind
* @param callable(T): void $rewind
*/
private function __construct(
LowLevelStream $stream,
Expand All @@ -48,10 +53,14 @@ private function __construct(
/**
* @psalm-mutation-free
* @internal
* @template A of LowLevelStream
*
* @param callable(LowLevelStream): Maybe<LowLevelStream> $ready
* @param A $stream
* @param callable(A): Maybe<A> $ready
* @param Maybe<Str\Encoding> $encoding
* @param positive-int $size
*
* @return self<A>
*/
public static function of(
LowLevelStream $stream,
Expand All @@ -62,6 +71,9 @@ public static function of(
return new self($stream, $ready, $encoding, $size, static fn() => null);
}

/**
* @psalm-mutation-free
*/
public function rewindable(): self
{
return new self(
Expand All @@ -77,6 +89,8 @@ public function rewindable(): self
}

/**
* @psalm-mutation-free
*
* @return Sequence<Str>
*/
public function sequence(): Sequence
Expand Down
51 changes: 51 additions & 0 deletions src/Readable/Frame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
declare(strict_types = 1);

namespace Innmind\IO\Readable;

use Innmind\Immutable\{
Maybe,
Str,
};

/**
* @template T
*/
interface Frame
{
/**
* @param callable(?positive-int): Maybe<Str> $read
* @param callable(): Maybe<Str> $readLine
*
* @return Maybe<T>
*/
public function __invoke(
callable $read,
callable $readLine,
): Maybe;

/**
* @param callable(T): bool $predicate
*
* @return self<T>
*/
public function filter(callable $predicate): self;

/**
* @template U
*
* @param callable(T): U $map
*
* @return self<U>
*/
public function map(callable $map): self;

/**
* @template U
*
* @param callable(T): self<U> $map
*
* @return self<U>
*/
public function flatMap(callable $map): self;
}
57 changes: 57 additions & 0 deletions src/Readable/Frame/Chunk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types = 1);

namespace Innmind\IO\Readable\Frame;

use Innmind\IO\Readable\Frame;
use Innmind\Immutable\{
Maybe,
Str,
};

/**
* @implements Frame<Str>
*/
final class Chunk implements Frame
{
/** @var positive-int */
private int $size;

/**
* @param positive-int $size
*/
private function __construct(int $size)
{
$this->size = $size;
}

public function __invoke(
callable $read,
callable $readLine,
): Maybe {
return $read($this->size);
}

/**
* @param positive-int $size
*/
public static function of(int $size): self
{
return new self($size);
}

public function filter(callable $predicate): Frame
{
return Filter::of($this, $predicate);
}

public function map(callable $map): Frame
{
return Map::of($this, $map);
}

public function flatMap(callable $map): Frame
{
return FlatMap::of($this, $map);
}
}
Loading

0 comments on commit 9f75a40

Please sign in to comment.