sltcodec 0.6.0__tar.gz → 1.0.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: sltcodec
3
- Version: 0.6.0
3
+ Version: 1.0.0
4
4
  Summary: Decode and encode bytearrays according to struct layout definitions using sltcore.
5
5
  Project-URL: Homepage, https://github.com/fangface-hub/StructLayoutToolkitCodec
6
6
  Project-URL: Documentation, https://readthedocs.org
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "sltcodec"
3
- version = "0.6.0"
3
+ version = "1.0.0"
4
4
  description = "Decode and encode bytearrays according to struct layout definitions using sltcore."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -1,5 +1,6 @@
1
1
  from .codec import (PRIMITIVE_TYPES, decode, encode, encode_field,
2
- load_struct_def_dict, save_struct_def_dict)
2
+ load_enum_def_dict, load_struct_def_dict,
3
+ save_enum_def_dict, save_struct_def_dict)
3
4
  from .types import EnumDef, FieldDef, FieldInstance, StructDef, StructInstance
4
5
 
5
6
  __all__ = [
@@ -12,6 +13,8 @@ __all__ = [
12
13
  "decode",
13
14
  "encode",
14
15
  "encode_field",
16
+ "load_enum_def_dict",
15
17
  "load_struct_def_dict",
18
+ "save_enum_def_dict",
16
19
  "save_struct_def_dict",
17
20
  ]
@@ -66,6 +66,26 @@ def load_struct_def_dict(path: str | Path) -> dict[str, StructDef]:
66
66
  }
67
67
 
68
68
 
69
+ def save_enum_def_dict(path: str | Path, enum_def_dict: dict[str,
70
+ EnumDef]) -> None:
71
+ """Save an enum definition dictionary to a JSON file."""
72
+ payload = {
73
+ name: enum_def.to_dict()
74
+ for name, enum_def in enum_def_dict.items()
75
+ }
76
+ Path(path).write_text(json.dumps(payload, ensure_ascii=False, indent=2),
77
+ encoding="utf-8")
78
+
79
+
80
+ def load_enum_def_dict(path: str | Path) -> dict[str, EnumDef]:
81
+ """Load an enum definition dictionary from a JSON file."""
82
+ data = json.loads(Path(path).read_text(encoding="utf-8"))
83
+ return {
84
+ name: EnumDef.from_dict(enum_def_data)
85
+ for name, enum_def_data in data.items()
86
+ }
87
+
88
+
69
89
  def _resolve_info_size(value: InfoSize | str, env: dict[str, Any]) -> InfoSize:
70
90
  """Resolve an InfoSize value that can be static or expression-based."""
71
91
  if isinstance(value, str):
@@ -7,7 +7,8 @@ from sltcore import InfoSize
7
7
 
8
8
  from sltcodec import (PRIMITIVE_TYPES, EnumDef, FieldDef, FieldInstance,
9
9
  StructDef, StructInstance, decode, encode,
10
- load_struct_def_dict, save_struct_def_dict)
10
+ load_enum_def_dict, load_struct_def_dict,
11
+ save_enum_def_dict, save_struct_def_dict)
11
12
  from sltcodec.codec import decode_field
12
13
 
13
14
 
@@ -413,6 +414,22 @@ def test_struct_def_dict_load_and_save(tmp_path: Path):
413
414
  assert loaded["value"] == struct_def
414
415
 
415
416
 
417
+ def test_enum_def_dict_load_and_save(tmp_path: Path):
418
+ """Test that an EnumDef dictionary can be saved and reloaded."""
419
+ enum_def = EnumDef(name="Status",
420
+ description="Status enum",
421
+ values={
422
+ "OK": 1,
423
+ "NG": 2
424
+ })
425
+ path = tmp_path / "enum_defs.json"
426
+
427
+ save_enum_def_dict(path, {"status": enum_def})
428
+ loaded = load_enum_def_dict(path)
429
+
430
+ assert loaded["status"] == enum_def
431
+
432
+
416
433
  def test_struct_def_to_json_from_json_round_trip():
417
434
  """Test StructDef.to_json/from_json round-trip conversion."""
418
435
  struct_def = StructDef(name="ValueStruct",
@@ -74,7 +74,7 @@ wheels = [
74
74
 
75
75
  [[package]]
76
76
  name = "sltcodec"
77
- version = "0.4.0"
77
+ version = "0.6.0"
78
78
  source = { editable = "." }
79
79
  dependencies = [
80
80
  { name = "sltcalc" },
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes