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