Skip to content

Commit 0c0a71e

Browse files
committed
feat: protocol object for supported libp2p protocols
1 parent a08145f commit 0c0a71e

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Protocol object that allows use to interface
3+
with supported protocols on the synlink network.
4+
"""
5+
from synlink.typing import TProtocol
6+
from synlink.utils import _check_minimum_version
7+
8+
if _check_minimum_version(3, 11):
9+
from typing import Self
10+
else:
11+
from typing_extensions import Self
12+
13+
14+
class Protocol(object):
15+
"""Network object allows us to see our StreamNetwork struct."""
16+
_protocol : TProtocol
17+
18+
def __init__(self, data : TProtocol):
19+
if not data.startswith('/'):
20+
raise ValueError("Protocols should start with a /.")
21+
self._protocol = data
22+
23+
@classmethod
24+
def from_bytes(cls, data : bytes) -> Self:
25+
data = data.decode()
26+
cls(data)
27+
28+
def to_bytes(self) -> bytes:
29+
data = self._protocol.encode("utf-8")
30+
return data
31+
32+
@classmethod
33+
def from_str(cls, data : str) -> Self:
34+
if not data.startswith('/'):
35+
raise ValueError("Protocols should start with a /.")
36+
cls(data)
37+
38+
@property
39+
def protocol(self):
40+
self._protocol
41+
42+
def __repr__(self) -> str:
43+
return f"<synlin.swarm.protocol {self._protocol!s}>"
44+
45+
def __str__(self) -> str:
46+
return self._protocol

0 commit comments

Comments
 (0)