blob: c69773016b82f84eaf45336ee3a7210f02c1c549 [file] [log] [blame]
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +01001package eu.mulk.jgvariant.core;
2
3import java.nio.ByteBuffer;
4import java.nio.charset.StandardCharsets;
5import java.text.ParseException;
6import java.util.ArrayList;
7import java.util.List;
8import java.util.Objects;
Matthias Andreas Benkard25b7f902021-12-17 06:02:11 +01009import org.apiguardian.api.API;
10import org.apiguardian.api.API.Status;
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +010011
12/**
13 * A GVariant signature string.
14 *
15 * <p>Describes a type in the GVariant type system. The type can be arbitrarily complex.
16 *
17 * <p><strong>Examples</strong>
18 *
19 * <dl>
20 * <dt>{@code "i"}
21 * <dd>a single 32-bit integer
22 * <dt>{@code "ai"}
23 * <dd>an array of 32-bit integers
24 * <dt>{@code "(bbb(sai))"}
25 * <dd>a record consisting of three booleans and a nested record, which consists of a string and
26 * an array of 32-bit integers
27 * </dl>
28 */
Matthias Andreas Benkard25b7f902021-12-17 06:02:11 +010029@API(status = Status.STABLE)
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +010030public final class Signature {
31
32 private final String signatureString;
33 private final Decoder<?> decoder;
34
35 Signature(ByteBuffer signatureBytes) throws ParseException {
36 this.decoder = parseSignature(signatureBytes);
37
38 signatureBytes.rewind();
39 this.signatureString = StandardCharsets.US_ASCII.decode(signatureBytes).toString();
40 }
41
42 static Signature parse(ByteBuffer signatureBytes) throws ParseException {
43 return new Signature(signatureBytes);
44 }
45
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010046 public static Signature parse(String signatureString) throws ParseException {
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +010047 var signatureBytes = ByteBuffer.wrap(signatureString.getBytes(StandardCharsets.US_ASCII));
48 return parse(signatureBytes);
49 }
50
51 /**
52 * Returns a {@link Decoder} that can decode values conforming to this signature.
53 *
54 * @return a {@link Decoder} for this signature
55 */
56 @SuppressWarnings("unchecked")
57 Decoder<Object> decoder() {
58 return (Decoder<Object>) decoder;
59 }
60
61 /**
62 * Returns the signature formatted as a GVariant signature string.
63 *
64 * @return a GVariant signature string.
65 */
66 @Override
67 public String toString() {
68 return signatureString;
69 }
70
71 @Override
72 public boolean equals(Object o) {
73 return (o instanceof Signature signature)
74 && Objects.equals(signatureString, signature.signatureString);
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hash(signatureString);
80 }
81
82 private static Decoder<?> parseSignature(ByteBuffer signature) throws ParseException {
83 char c = (char) signature.get();
84 return switch (c) {
85 case 'b' -> Decoder.ofBoolean();
86 case 'y' -> Decoder.ofByte();
87 case 'n', 'q' -> Decoder.ofShort();
88 case 'i', 'u' -> Decoder.ofInt();
89 case 'x', 't' -> Decoder.ofLong();
90 case 'd' -> Decoder.ofDouble();
91 case 's', 'o', 'g' -> Decoder.ofString(StandardCharsets.UTF_8);
92 case 'v' -> Decoder.ofVariant();
93 case 'm' -> Decoder.ofMaybe(parseSignature(signature));
Matthias Andreas Benkardcd924f62021-12-28 00:46:06 +010094 case '(' -> Decoder.ofStructure(parseTupleTypes(signature).toArray(new Decoder<?>[0]));
Matthias Andreas Benkardd6a25d12021-12-28 01:13:58 +010095 case 'a' -> {
96 char elementC = (char) signature.get(signature.position());
97 if (elementC == '{') {
98 signature.get();
99 List<Decoder<?>> entryTypes = parseDictionaryEntryTypes(signature);
100 yield Decoder.ofDictionary(entryTypes.get(0), entryTypes.get(1));
101 } else {
102 yield Decoder.ofArray(parseSignature(signature));
Matthias Andreas Benkardcd924f62021-12-28 00:46:06 +0100103 }
Matthias Andreas Benkardd6a25d12021-12-28 01:13:58 +0100104 }
105 case '{' -> {
106 List<Decoder<?>> entryTypes = parseDictionaryEntryTypes(signature);
107 yield Decoder.ofDictionaryEntry(entryTypes.get(0), entryTypes.get(1));
Matthias Andreas Benkardcd924f62021-12-28 00:46:06 +0100108 }
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100109 default -> throw new ParseException(
110 String.format("encountered unknown signature byte '%c'", c), signature.position());
111 };
112 }
113
Matthias Andreas Benkardd6a25d12021-12-28 01:13:58 +0100114 private static List<Decoder<?>> parseDictionaryEntryTypes(ByteBuffer signature)
115 throws ParseException {
116 var tupleTypes = parseTupleTypes(signature);
117 if (tupleTypes.size() != 2) {
118 throw new ParseException(
119 String.format("dictionary entry type with %d components, expected 2", tupleTypes.size()),
120 signature.position());
121 }
122 return tupleTypes;
123 }
124
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100125 private static List<Decoder<?>> parseTupleTypes(ByteBuffer signature) throws ParseException {
126 List<Decoder<?>> decoders = new ArrayList<>();
127
128 while (true) {
129 char c = (char) signature.get(signature.position());
130 if (c == ')' || c == '}') {
131 signature.get();
132 break;
133 }
134
135 decoders.add(parseSignature(signature));
136 }
137
138 return decoders;
139 }
140}