mirror of
https://github.com/nix-community/disko.git
synced 2026-12-03 06:23:28 +01:00
disko2: Make Result generic over the value it contains
This will be essential for type safety later on.
This commit is contained in:
1 parent
d4a944c327
commit
88a85411ba
2 files changed
+15
-9
No files matched your search
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Literal
|
from typing import Any, Literal, Union
|
||||||
|
|
||||||
from lib.logging import LOGGER, DiskoMessage, debug, info
|
from lib.logging import LOGGER, DiskoMessage, debug, info
|
||||||
from lib.result import DiskoError, DiskoSuccess, DiskoResult, exit_on_error
|
from lib.result import DiskoError, DiskoSuccess, DiskoResult, exit_on_error
|
||||||
@@ -32,7 +32,9 @@ MODE_DESCRIPTION: dict[Mode, str] = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def run_apply(*, mode: str, disko_file: Path | None, flake: str | None) -> DiskoResult:
|
def run_apply(
|
||||||
|
*, mode: str, disko_file: Path | None, flake: str | None
|
||||||
|
) -> DiskoResult[dict[str, Any]]:
|
||||||
if flake is None and disko_file is None:
|
if flake is None and disko_file is None:
|
||||||
return DiskoError([DiskoMessage("ERR_MISSING_ARGUMENTS")], "validate args")
|
return DiskoError([DiskoMessage("ERR_MISSING_ARGUMENTS")], "validate args")
|
||||||
if flake is not None and disko_file is not None:
|
if flake is not None and disko_file is not None:
|
||||||
@@ -48,11 +50,13 @@ def run_apply(*, mode: str, disko_file: Path | None, flake: str | None) -> Disko
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def run_generate() -> DiskoResult:
|
def run_generate() -> DiskoResult[Literal["generate"]]:
|
||||||
return DiskoSuccess("generate", "generate config")
|
return DiskoSuccess("generate", "generate config")
|
||||||
|
|
||||||
|
|
||||||
def run(args: argparse.Namespace) -> DiskoResult:
|
def run(
|
||||||
|
args: argparse.Namespace,
|
||||||
|
) -> DiskoResult[Literal["generate"] | dict[str, Any]]:
|
||||||
if args.verbose == True:
|
if args.verbose == True:
|
||||||
LOGGER.setLevel("DEBUG")
|
LOGGER.setLevel("DEBUG")
|
||||||
debug("Enabled debug logging.")
|
debug("Enabled debug logging.")
|
||||||
|
|||||||
+7
-5
@@ -1,12 +1,14 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Literal, Union
|
from typing import Generic, Literal, TypeVar
|
||||||
|
|
||||||
from lib.logging import DiskoMessage, debug, print_msg
|
from lib.logging import DiskoMessage, debug, print_msg
|
||||||
|
|
||||||
|
T = TypeVar("T", covariant=True)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DiskoSuccess:
|
class DiskoSuccess(Generic[T]):
|
||||||
value: object
|
value: T
|
||||||
context: None | str = None
|
context: None | str = None
|
||||||
success: Literal[True] = True
|
success: Literal[True] = True
|
||||||
|
|
||||||
@@ -18,10 +20,10 @@ class DiskoError:
|
|||||||
success: Literal[False] = False
|
success: Literal[False] = False
|
||||||
|
|
||||||
|
|
||||||
DiskoResult = Union[DiskoSuccess, DiskoError]
|
DiskoResult = DiskoSuccess[T] | DiskoError
|
||||||
|
|
||||||
|
|
||||||
def exit_on_error(result: DiskoResult) -> object:
|
def exit_on_error(result: DiskoResult[T]) -> T:
|
||||||
if isinstance(result, DiskoSuccess):
|
if isinstance(result, DiskoSuccess):
|
||||||
if result.context is None:
|
if result.context is None:
|
||||||
print_msg("BUG_SUCCESS_WITHOUT_CONTEXT", {"value": result.value})
|
print_msg("BUG_SUCCESS_WITHOUT_CONTEXT", {"value": result.value})
|
||||||
|
|||||||
Reference in new issue
Block a user