Skip to content

TomlRsProvider

Base classes

Name Children Inherits
TomlProviderBase
anyenv.toml_tools.base
Base class for all TOML providers.

⋔ Inheritance diagram

graph TD
  1089727493["provider.TomlRsProvider"]
  1311279371["base.TomlProviderBase"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  1311279371 --> 1089727493
  925645236 --> 1311279371
  2751680898 --> 925645236

🛈 DocStrings

Bases: TomlProviderBase

TOML-RS implementation of the TOML provider interface.

Source code in src/anyenv/toml_tools/toml_rs_provider/provider.py
class TomlRsProvider(TomlProviderBase):
    """TOML-RS implementation of the TOML provider interface."""

    @staticmethod
    def load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any:
        """Load TOML using toml_rs."""
        import toml_rs

        try:
            source_content: str | None = None
            source_path: Path | UPath | None = None
            match data:
                case Path() | UPath():
                    source_path = data
                    bytes_data = data.read_bytes()
                    source_content = bytes_data.decode(errors="replace")
                    return toml_rs.load(BytesIO(bytes_data))
                case TextIOWrapper():
                    content = data.read()
                    source_content = content
                    return toml_rs.loads(content)
                case bytes():
                    content = data.decode()
                    source_content = content
                    return toml_rs.loads(content)
                case str():
                    source_content = data
                    return toml_rs.loads(data)
        except toml_rs.TOMLDecodeError as exc:
            raise TomlLoadError(  # noqa: TRY003
                f"Invalid TOML: {exc.msg}",
                line=exc.lineno,
                column=exc.colno,
                source_path=str(source_path) if isinstance(source_path, UPath) else source_path,
                source_content=source_content,
            ) from exc

    @staticmethod
    def dump_toml(data: Any, *, pretty: bool = False) -> str:
        """Dump data to TOML string using toml_rs."""
        import toml_rs

        return toml_rs.dumps(data, pretty=pretty)

dump_toml staticmethod

dump_toml(data: Any, *, pretty: bool = False) -> str

Dump data to TOML string using toml_rs.

Source code in src/anyenv/toml_tools/toml_rs_provider/provider.py
@staticmethod
def dump_toml(data: Any, *, pretty: bool = False) -> str:
    """Dump data to TOML string using toml_rs."""
    import toml_rs

    return toml_rs.dumps(data, pretty=pretty)

load_toml staticmethod

load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any

Load TOML using toml_rs.

Source code in src/anyenv/toml_tools/toml_rs_provider/provider.py
@staticmethod
def load_toml(data: str | bytes | TextIOWrapper | Path | UPath) -> Any:
    """Load TOML using toml_rs."""
    import toml_rs

    try:
        source_content: str | None = None
        source_path: Path | UPath | None = None
        match data:
            case Path() | UPath():
                source_path = data
                bytes_data = data.read_bytes()
                source_content = bytes_data.decode(errors="replace")
                return toml_rs.load(BytesIO(bytes_data))
            case TextIOWrapper():
                content = data.read()
                source_content = content
                return toml_rs.loads(content)
            case bytes():
                content = data.decode()
                source_content = content
                return toml_rs.loads(content)
            case str():
                source_content = data
                return toml_rs.loads(data)
    except toml_rs.TOMLDecodeError as exc:
        raise TomlLoadError(  # noqa: TRY003
            f"Invalid TOML: {exc.msg}",
            line=exc.lineno,
            column=exc.colno,
            source_path=str(source_path) if isinstance(source_path, UPath) else source_path,
            source_content=source_content,
        ) from exc

:fa-brands-github: Show source on GitHub