-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGuidBytesParser.cs
More file actions
32 lines (26 loc) · 1019 Bytes
/
GuidBytesParser.cs
File metadata and controls
32 lines (26 loc) · 1019 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using RDMSharp.RDM;
using System;
using System.Linq;
namespace RDMSharp.Extensions.BytesParser;
public class GuidBytesParser : IBytesParser
{
private static readonly byte _bytesLength = 16;
private static readonly string _formatIdentifyer = "guid";
private static readonly PDL _payloadDataLength = new PDL(_bytesLength);
public string FormatIdentifyer => _formatIdentifyer;
public PDL? PayloadDataLength => _payloadDataLength;
public object ParseToObject(ref byte[] data)
{
if (data.Length < _bytesLength)
throw new ArgumentException("Data length is less than required for Guid parsing.", nameof(data));
Guid guid = new Guid(data.Take(_bytesLength).ToArray());
data = data.Skip(_bytesLength).ToArray();
return guid;
}
public byte[] ParseToData(object obj)
{
if (obj is Guid guid)
return guid.ToByteArray();
throw new ArgumentException($"Object is not of type {nameof(Guid)}", nameof(obj));
}
}