toml-rs 0.0.9__pp311-pypy311_pp73-win_amd64.whl → 0.1.0__pp311-pypy311_pp73-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of toml-rs might be problematic. Click here for more details.

toml_rs/__init__.py CHANGED
@@ -1,15 +1,19 @@
1
1
  __all__ = (
2
2
  "TOMLDecodeError",
3
+ "TOMLEncodeError",
3
4
  "__version__",
5
+ "dump",
6
+ "dumps",
4
7
  "load",
5
8
  "loads",
6
9
  )
7
10
 
8
11
  from collections.abc import Callable
9
- from typing import Any, BinaryIO
12
+ from pathlib import Path
13
+ from typing import Any, BinaryIO, TextIO
10
14
 
11
15
  from ._toml_rs import (
12
- _load,
16
+ _dumps,
13
17
  _loads,
14
18
  _version,
15
19
  )
@@ -18,7 +22,13 @@ __version__: str = _version
18
22
 
19
23
 
20
24
  def load(fp: BinaryIO, /, *, parse_float: Callable[[str], Any] = float) -> dict[str, Any]:
21
- return _load(fp, parse_float=parse_float)
25
+ _bytes = fp.read()
26
+ try:
27
+ _str = _bytes.decode()
28
+ except AttributeError:
29
+ msg = "File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`"
30
+ raise TypeError(msg) from None
31
+ return loads(_str, parse_float=parse_float)
22
32
 
23
33
 
24
34
  def loads(s: str, /, *, parse_float: Callable[[str], Any] = float) -> dict[str, Any]:
@@ -27,6 +37,33 @@ def loads(s: str, /, *, parse_float: Callable[[str], Any] = float) -> dict[str,
27
37
  return _loads(s, parse_float=parse_float)
28
38
 
29
39
 
40
+ def dump(
41
+ obj: Any,
42
+ /,
43
+ file: str | Path | TextIO,
44
+ inline_tables: set[str] | None = None,
45
+ *,
46
+ pretty: bool = False,
47
+ ) -> int:
48
+ _str = _dumps(obj, inline_tables=inline_tables, pretty=pretty)
49
+ if isinstance(file, str):
50
+ file = Path(file)
51
+ if isinstance(file, Path):
52
+ return file.write_text(_str, encoding="utf-8")
53
+ else:
54
+ return file.write(_str)
55
+
56
+
57
+ def dumps(
58
+ obj: Any,
59
+ /,
60
+ inline_tables: set[str] | None = None,
61
+ *,
62
+ pretty: bool = False,
63
+ ) -> str:
64
+ return _dumps(obj, inline_tables=inline_tables, pretty=pretty)
65
+
66
+
30
67
  class TOMLDecodeError(ValueError):
31
68
  def __init__(self, msg: str, doc: str, pos: int, *args: Any):
32
69
  msg = msg.rstrip()
@@ -41,3 +78,10 @@ class TOMLDecodeError(ValueError):
41
78
  self.pos = pos
42
79
  self.colno = colno
43
80
  self.lineno = lineno
81
+
82
+
83
+ class TOMLEncodeError(TypeError):
84
+ def __init__(self, msg: str, *args: Any):
85
+ msg = msg.rstrip()
86
+ super().__init__(msg)
87
+ self.msg = msg
Binary file
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: toml-rs
3
- Version: 0.0.9
3
+ Version: 0.1.0
4
4
  Classifier: Typing :: Typed
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Programming Language :: Python
@@ -31,7 +31,7 @@ Project-URL: Bug Tracker, https://github.com/lava-sh/toml-rs/issues
31
31
 
32
32
  * The fastest TOML parser in Python (see [benchmarks](https://github.com/lava-sh/toml-rs/tree/main/benchmark))
33
33
 
34
- * Drop-in compatibility with most [`tomllib`](https://docs.python.org/3/library/tomllib.html) use cases ([see below](#differences-with-tomllib))
34
+ * Drop-in compatibility with most [`tomllib`](https://docs.python.org/3/library/tomllib.html) use cases (see [below](#differences-with-tomllib))
35
35
 
36
36
  ## Installation
37
37
  ```bash
@@ -44,10 +44,10 @@ uv pip install toml-rs
44
44
 
45
45
  ## Examples
46
46
  ```python
47
+ import tomllib
47
48
  from pprint import pprint
48
49
 
49
50
  import toml_rs
50
- import tomllib
51
51
 
52
52
  toml = """\
53
53
  title = "TOML Example"
@@ -73,13 +73,14 @@ role = "backend"
73
73
 
74
74
  tomllib_loads = tomllib.loads(toml)
75
75
  toml_rs_loads = toml_rs.loads(toml)
76
+ toml_rs_dumps = toml_rs.dumps(toml_rs_loads)
76
77
 
77
78
  assert tomllib_loads == toml_rs_loads
78
79
 
79
- print("tomllib:")
80
- pprint(tomllib_loads)
81
- print("toml_rs:")
80
+ print("toml_rs.loads:")
82
81
  pprint(toml_rs_loads)
82
+ print("toml_rs.dumps:")
83
+ print(toml_rs_dumps)
83
84
  ```
84
85
 
85
86
  ## Differences with [`tomllib`](https://docs.python.org/3/library/tomllib.html)
@@ -136,4 +137,22 @@ print(toml_rs.loads(t))
136
137
  # 1 | x = 999_999_999_999_999_999_999_999
137
138
  # | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
138
139
  # invalid type: integer `999999999999999999999999` as i128, expected any valid TOML value
140
+ ```
141
+
142
+ 3. Supports serialization (`toml_rs.dumps` and `toml_rs.dump`)
143
+
144
+ ```python
145
+ from pathlib import Path
146
+
147
+ import toml_rs
148
+
149
+ data = {
150
+ "title": "TOML Example",
151
+ "owner": {"name": "Alice", "age": 30},
152
+ }
153
+
154
+ print(toml_rs.dumps(data))
155
+
156
+ toml_rs.dump(data, Path("example.toml"))
157
+ # or `toml_rs.dump(data, "example.toml")`
139
158
  ```
@@ -0,0 +1,7 @@
1
+ toml_rs-0.1.0.dist-info/METADATA,sha256=FsQ8OXhhxugTkjFoRmEhUw5_VN8tLrs_-JoHGyaosM4,4048
2
+ toml_rs-0.1.0.dist-info/WHEEL,sha256=XvltcVfFjkSizTjc8NGh5IW8xXp3Lufh9SqWYuaiv-c,103
3
+ toml_rs-0.1.0.dist-info/licenses/UNLICENSE,sha256=pO9SJPWzhoS7yVUh8EuWMlwkAsA9o-yzXiMtgqmWKC8,1233
4
+ toml_rs/__init__.py,sha256=Iveymfrcmv6oMs_ixXXsD26L0Qy8T_Ab1bj5kt3M3M8,2250
5
+ toml_rs/_toml_rs.pypy311-pp73-win_amd64.pyd,sha256=SnPQTX3AYEn9A3Ohg4xbX4vq3hyksIGbSQNXyLGQAE4,659456
6
+ toml_rs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ toml_rs-0.1.0.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- toml_rs-0.0.9.dist-info/METADATA,sha256=G6BDV0sZXc7ntdvx7w6of9g4n16LOGj6dNnEPPVMlC0,3655
2
- toml_rs-0.0.9.dist-info/WHEEL,sha256=XvltcVfFjkSizTjc8NGh5IW8xXp3Lufh9SqWYuaiv-c,103
3
- toml_rs-0.0.9.dist-info/licenses/UNLICENSE,sha256=pO9SJPWzhoS7yVUh8EuWMlwkAsA9o-yzXiMtgqmWKC8,1233
4
- toml_rs/__init__.py,sha256=NIiAfcSc1whF7NT0jfypUpEACbx4_yfP1PATASZiQi8,1113
5
- toml_rs/_toml_rs.pypy311-pp73-win_amd64.pyd,sha256=BhyrGWAWtYVqeBak-NGjUf5rxoZ-IZWLuiG7rmYp8gI,536576
6
- toml_rs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- toml_rs-0.0.9.dist-info/RECORD,,