# Path: xmanager/xm/pattern_matching.py
# R = TypeVar('R')
# class Case(Generic[R]):
#   def __init__(
#       self,
#       kind: Iterable[Type[Any]],
#       handler: Callable[..., R],
#   ) -> None:
#   def matches(self, *values: Any) -> bool:
# def _deduce_types(handler: Callable[..., Any]) -> Tuple[Type[Any]]:
#   def get_arg_type(arg: str):
# def match(*handlers: Union[Case[R], Callable[..., R]]) -> Callable[..., R]:
#   def apply(*values: Any) -> R:
# def async_match(*handlers: Callable[..., R]) -> Callable[..., Awaitable[R]]:


import enum
import functools
import itertools
import operator
import re
import immutabledict
from typing import Any, Iterable, Iterator, Dict, Mapping, MutableMapping, Optional, Tuple, Union
from xmanager.xm import pattern_matching as pm
    return f'xm.Topology({self.name!r})'

  def __eq__(self, other: 'Topology') -> bool:
    return self.name == other.name

  def __hash__(self) -> int:
    return hash(self.name)


ResourceQuantity = Union[int, float, str, Topology]


def _parse_resource_quantity(
    value: ResourceQuantity) -> Tuple[float, Optional[Topology]]:
  """Parses a string representation of a resource quantity."""

  def parse_string(value: str):
    if 'x' in value:
      topology = Topology(value)
      return topology.chip_count, topology
    else:
      # TODO: Parse SI suffixes, like GiB.
      return float(value), None

  def parse_topology(topology: Topology):
    return topology.chip_count, topology

  def parse_number(value: Any):
    return float(value), None


# Path: xmanager/xm/pattern_matching.py
# R = TypeVar('R')
# class Case(Generic[R]):
#   def __init__(
#       self,
#       kind: Iterable[Type[Any]],
#       handler: Callable[..., R],
#   ) -> None:
#   def matches(self, *values: Any) -> bool:
# def _deduce_types(handler: Callable[..., Any]) -> Tuple[Type[Any]]:
#   def get_arg_type(arg: str):
# def match(*handlers: Union[Case[R], Callable[..., R]]) -> Callable[..., R]:
#   def apply(*values: Any) -> R:
# def async_match(*handlers: Callable[..., R]) -> Callable[..., Awaitable[R]]:


# Path: xmanager/xm/resources.py
import enum
import functools
import itertools
import operator
import re
import immutabledict
from typing import Any, Iterable, Iterator, Dict, Mapping, MutableMapping, Optional, Tuple, Union
from xmanager.xm import pattern_matching as pm

    return f'xm.Topology({self.name!r})'

  def __eq__(self, other: 'Topology') -> bool:
    return self.name == other.name

  def __hash__(self) -> int:
    return hash(self.name)


ResourceQuantity = Union[int, float, str, Topology]


def _parse_resource_quantity(
    value: ResourceQuantity) -> Tuple[float, Optional[Topology]]:
  """Parses a string representation of a resource quantity."""

  def parse_string(value: str):
    if 'x' in value:
      topology = Topology(value)
      return topology.chip_count, topology
    else:
      # TODO: Parse SI suffixes, like GiB.
      return float(value), None

  def parse_topology(topology: Topology):
    return topology.chip_count, topology

  def parse_number(value: Any):
    return float(value), None


  return pm.match(parse_string, parse_topology, parse_number)(value)