Skip to content

Use Attempt to better explicit everywhere an action may fail #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
- Requires `innmind/io:~3.2`
- Requires `innmind/immutable:~5.15`
- `Innmind\OperatingSystem\Config::withHttpHeartbeat()` period is now expressed with a `Innmind\TimeContinuum\Period`
- `Innmind\OperatingSystem\CurrentProcess::id()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\OperatingSystem\CurrentProcess::halt()` now returns `Innmind\Immutable\Attempt<Innmind\Immutable\SideEffect>`
- `Innmind\OperatingSystem\Filesystem::mount()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\OperatingSystem\Filesystem::temporary()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\OperatingSystem\Ports::open()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\OperatingSystem\Remote::socket()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\OperatingSystem\Sockets::open()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\OperatingSystem\Sockets::takeOver()` now returns an `Innmind\Immutable\Attempt`
- `Innmind\OperatingSystem\Sockets::connectTo()` now returns an `Innmind\Immutable\Attempt`

### Fixed

Expand Down
5 changes: 4 additions & 1 deletion src/CurrentProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

interface CurrentProcess
{
public function id(): Pid;
/**
* @return Attempt<Pid>
*/
public function id(): Attempt;
public function signals(): Signals;

/**
Expand Down
14 changes: 8 additions & 6 deletions src/CurrentProcess/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public static function of(Halt $halt): self
}

#[\Override]
public function id(): Pid
public function id(): Attempt
{
/**
* @psalm-suppress ArgumentTypeCoercion
* @psalm-suppress PossiblyFalseArgument
*/
return new Pid(\getmypid());
$pid = \getmypid();

/** @psalm-suppress ArgumentTypeCoercion */
return match ($pid) {
false => Attempt::error(new \RuntimeException('Failed to retrieve process id')),
default => Attempt::result(new Pid($pid)),
};
}

#[\Override]
Expand Down
17 changes: 8 additions & 9 deletions src/CurrentProcess/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Innmind\OperatingSystem\CurrentProcess;

use Innmind\OperatingSystem\CurrentProcess;
use Innmind\Server\Control\Server\Process\Pid;
use Innmind\Server\Status\Server\Memory\Bytes;
use Innmind\TimeContinuum\Period;
use Innmind\Immutable\Attempt;
Expand All @@ -28,16 +27,16 @@ public static function psr(CurrentProcess $process, LoggerInterface $logger): se
}

#[\Override]
public function id(): Pid
public function id(): Attempt
{
$pid = $this->process->id();
return $this->process->id()->map(function($pid) {
$this->logger->debug(
'Current process id is {pid}',
['pid' => $pid->toInt()],
);

$this->logger->debug(
'Current process id is {pid}',
['pid' => $pid->toInt()],
);

return $pid;
return $pid;
});
}

#[\Override]
Expand Down
10 changes: 7 additions & 3 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
use Innmind\Url\Path;
use Innmind\FileWatch\Ping;
use Innmind\Immutable\{
Attempt,
Maybe,
Str,
Sequence,
};

interface Filesystem
{
public function mount(Path $path): Adapter;
/**
* @return Attempt<Adapter>
*/
public function mount(Path $path): Attempt;
public function contains(Path $path): bool;

/**
Expand All @@ -36,7 +40,7 @@ public function watch(Path $path): Ping;
*
* @param Sequence<Maybe<Str>> $chunks
*
* @return Maybe<Content>
* @return Attempt<Content>
*/
public function temporary(Sequence $chunks): Maybe;
public function temporary(Sequence $chunks): Attempt;
}
30 changes: 16 additions & 14 deletions src/Filesystem/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,30 @@ public static function of(Processes $processes, Config $config): self
}

#[\Override]
public function mount(Path $path): Adapter
public function mount(Path $path): Attempt
{
/**
* @var Adapter $adapter
* @var string $mounted
*/
foreach ($this->mounted as $adapter => $mounted) {
if ($path->toString() === $mounted) {
return $adapter;
return Attempt::result($adapter);
}
}

$adapter = Adapter\Filesystem::mount(
$path,
$this->config->io(),
)
->withCaseSensitivity(
$this->config->filesystemCaseSensitivity(),
);
$this->mounted[$adapter] = $path->toString();

return $adapter;
return Attempt::of(function() use ($path) {
$adapter = Adapter\Filesystem::mount(
$path,
$this->config->io(),
)
->withCaseSensitivity(
$this->config->filesystemCaseSensitivity(),
);
$this->mounted[$adapter] = $path->toString();

return $adapter;
});
}

#[\Override]
Expand Down Expand Up @@ -111,7 +113,7 @@ public function watch(Path $path): Ping
}

#[\Override]
public function temporary(Sequence $chunks): Maybe
public function temporary(Sequence $chunks): Attempt
{
return Attempt::of(
fn() => $this
Expand All @@ -129,6 +131,6 @@ public function temporary(Sequence $chunks): Maybe
->map(static fn($tmp) => $tmp->read())
->map(Content::io(...))
->unwrap(),
)->maybe();
);
}
}
13 changes: 8 additions & 5 deletions src/Filesystem/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Innmind\Url\Path;
use Innmind\FileWatch\Ping;
use Innmind\Immutable\{
Attempt,
Maybe,
Sequence,
};
Expand All @@ -37,11 +38,13 @@ public static function psr(
}

#[\Override]
public function mount(Path $path): Adapter
public function mount(Path $path): Attempt
{
return Adapter\Logger::psr(
$this->filesystem->mount($path),
$this->logger,
return $this->filesystem->mount($path)->map(
fn($adapter) => Adapter\Logger::psr(
$adapter,
$this->logger,
),
);
}

Expand Down Expand Up @@ -82,7 +85,7 @@ public function watch(Path $path): Ping
}

#[\Override]
public function temporary(Sequence $chunks): Maybe
public function temporary(Sequence $chunks): Attempt
{
return $this
->filesystem
Expand Down
6 changes: 3 additions & 3 deletions src/Ports.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
Sockets\Internet\Transport,
};
use Innmind\IP\IP;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Ports
{
/**
* @return Maybe<Server>
* @return Attempt<Server>
*/
public function open(Transport $transport, IP $ip, Port $port): Maybe;
public function open(Transport $transport, IP $ip, Port $port): Attempt;
}
4 changes: 2 additions & 2 deletions src/Ports/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Innmind\Url\Authority\Port;
use Innmind\IO\Sockets\Internet\Transport;
use Innmind\IP\IP;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;
use Psr\Log\LoggerInterface;

final class Logger implements Ports
Expand All @@ -27,7 +27,7 @@ public static function psr(Ports $ports, LoggerInterface $logger): self
}

#[\Override]
public function open(Transport $transport, IP $ip, Port $port): Maybe
public function open(Transport $transport, IP $ip, Port $port): Attempt
{
$this->logger->debug(
'Opening new port at {address}',
Expand Down
7 changes: 3 additions & 4 deletions src/Ports/Unix.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Innmind\Url\Authority\Port;
use Innmind\IO\Sockets\Internet\Transport;
use Innmind\IP\IP;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

final class Unix implements Ports
{
Expand All @@ -30,14 +30,13 @@ public static function of(Config $config): self
}

#[\Override]
public function open(Transport $transport, IP $ip, Port $port): Maybe
public function open(Transport $transport, IP $ip, Port $port): Attempt
{
return $this
->config
->io()
->sockets()
->servers()
->internet($transport, $ip, $port)
->maybe();
->internet($transport, $ip, $port);
}
}
6 changes: 3 additions & 3 deletions src/Remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
Authority,
};
use Innmind\HttpTransport\Transport as HttpTransport;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;
use Formal\AccessLayer\Connection;

interface Remote
{
public function ssh(Url $server): Server;

/**
* @return Maybe<Client>
* @return Attempt<Client>
*/
public function socket(Transport $transport, Authority $authority): Maybe;
public function socket(Transport $transport, Authority $authority): Attempt;
public function http(): HttpTransport;
public function sql(Url $server): Connection;
}
7 changes: 3 additions & 4 deletions src/Remote/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Transport as HttpTransport,
Curl,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;
use Formal\AccessLayer\Connection;

final class Generic implements Remote
Expand Down Expand Up @@ -62,15 +62,14 @@ public function ssh(Url $server): Server
}

#[\Override]
public function socket(Transport $transport, Authority $authority): Maybe
public function socket(Transport $transport, Authority $authority): Attempt
{
return $this
->config
->io()
->sockets()
->clients()
->internet($transport, $authority)
->maybe();
->internet($transport, $authority);
}

#[\Override]
Expand Down
4 changes: 2 additions & 2 deletions src/Remote/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Authority,
};
use Innmind\HttpTransport;
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;
use Formal\AccessLayer\Connection;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -41,7 +41,7 @@ public function ssh(Url $server): Control\Server
}

#[\Override]
public function socket(Transport $transport, Authority $authority): Maybe
public function socket(Transport $transport, Authority $authority): Attempt
{
$this->logger->debug(
'Opening remote socket at {address}',
Expand Down
7 changes: 2 additions & 5 deletions src/Remote/Resilient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
Transport as HttpTransport,
ExponentialBackoff,
};
use Innmind\Immutable\{
Maybe,
Attempt,
};
use Innmind\Immutable\Attempt;
use Formal\AccessLayer\Connection;

final class Resilient implements Remote
Expand All @@ -48,7 +45,7 @@ public function ssh(Url $server): Server
}

#[\Override]
public function socket(Transport $transport, Authority $authority): Maybe
public function socket(Transport $transport, Authority $authority): Attempt
{
return $this->remote->socket($transport, $authority);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Sockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
Sockets\Servers\Server,
Sockets\Unix\Address,
};
use Innmind\Immutable\Maybe;
use Innmind\Immutable\Attempt;

interface Sockets
{
/**
* This method will fail if the socket already exist
*
* @return Maybe<Server>
* @return Attempt<Server>
*/
public function open(Address $address): Maybe;
public function open(Address $address): Attempt;

/**
* This will take control of the socket if it already exist (use carefully)
*
* @return Maybe<Server>
* @return Attempt<Server>
*/
public function takeOver(Address $address): Maybe;
public function takeOver(Address $address): Attempt;

/**
* @return Maybe<Client>
* @return Attempt<Client>
*/
public function connectTo(Address $address): Maybe;
public function connectTo(Address $address): Attempt;
}
Loading