blob: 6a4d01de91b373fd09d0a5673ae37be59a0e2925 [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));
94 case 'a' -> Decoder.ofArray(parseSignature(signature));
Matthias Andreas Benkardcd924f62021-12-28 00:46:06 +010095 case '(' -> Decoder.ofStructure(parseTupleTypes(signature).toArray(new Decoder<?>[0]));
96 case '{' -> {
97 var tupleTypes = parseTupleTypes(signature);
98 if (tupleTypes.size() != 2) {
99 throw new ParseException(
100 String.format(
101 "dictionary entry type with %d components, expected 2", tupleTypes.size()),
102 signature.position());
103 }
104 yield Decoder.ofDictionaryEntry(tupleTypes.get(0), tupleTypes.get(1));
105 }
Matthias Andreas Benkard31c61e72021-12-16 20:06:39 +0100106 default -> throw new ParseException(
107 String.format("encountered unknown signature byte '%c'", c), signature.position());
108 };
109 }
110
111 private static List<Decoder<?>> parseTupleTypes(ByteBuffer signature) throws ParseException {
112 List<Decoder<?>> decoders = new ArrayList<>();
113
114 while (true) {
115 char c = (char) signature.get(signature.position());
116 if (c == ')' || c == '}') {
117 signature.get();
118 break;
119 }
120
121 decoders.add(parseSignature(signature));
122 }
123
124 return decoders;
125 }
126}