blob: 721d857b9c8e03bea92fba70eef4b116066a33c0 [file] [log] [blame]
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01001package eu.mulk.jgvariant.ostree;
2
3import static org.apiguardian.api.API.Status.STABLE;
4
5import org.apiguardian.api.API;
6
7/**
8 * An object type as found in an OSTree repository.
9 *
10 * <p>Each object type has its own file extension.
11 *
12 * <p>In an OSTree repository, objects are located in a subfolder of the {@code /objects} folder
13 * based on their {@link Checksum}. The schema for looking up objects is {@code
14 * /objects/{checksumHead}/{checksumRest}.{fileExtension}} where:
15 *
16 * <dl>
17 * <dt>{@code {checksumHead}}
18 * <dd>the first two characters of {@link Checksum#hex()}
19 * <dt>{@code {checksumRest}}
20 * <dd>the substring of {@link Checksum#hex()} starting from the 3rd character
21 * <dt>{@code {fileExtension}}
22 * <dd>the {@link #fileExtension()} of the object type
23 * </dl>
24 */
25@API(status = STABLE)
26public enum ObjectType {
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010027
28 /**
29 * A regular file.
30 *
31 * <p>File ending: {@code .file}
32 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010033 FILE((byte) 1, "file"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010034
35 /**
36 * A serialized {@link DirTree} object.
37 *
38 * <p>File ending: {@code .dirtree}
39 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010040 DIR_TREE((byte) 2, "dirtree"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010041
42 /**
43 * A serialized {@link DirMeta} object.
44 *
45 * <p>File ending: {@code .dirmeta}
46 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010047 DIR_META((byte) 3, "dirmeta"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010048
49 /**
50 * A serialized {@link Commit} object.
51 *
52 * <p>File ending: {@code .commit}
53 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010054 COMMIT((byte) 4, "commit"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010055
56 /**
57 * A tombstone file standing in for a commit that was deleted.
58 *
59 * <p>File ending: {@code .commit-tombstone}
60 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010061 TOMBSTONE_COMMIT((byte) 5, "commit-tombstone"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010062
63 /**
64 * Detached metadata for a {@link Commit}.
65 *
66 * <p>Often goes together with a {@link #TOMBSTONE_COMMIT}.
67 *
68 * <p>File ending: {@code .commitmeta}
69 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010070 COMMIT_META((byte) 6, "commitmeta"),
Matthias Andreas Benkardc7aa2b62022-01-23 18:10:03 +010071
72 /**
73 * A symlink to a {@link #FILE} that lives somewhere else.
74 *
75 * <p>File ending: {@code .payload-link}
76 */
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010077 PAYLOAD_LINK((byte) 7, "payload-link");
78
79 private final byte byteValue;
80 private final String fileExtension;
81
82 /**
83 * The serialized byte value.
84 *
85 * @return a byte representing this value in serialized GVariant structures.
86 */
87 public byte byteValue() {
88 return byteValue;
89 }
90
91 /**
92 * The file extension carried by files of this type.
93 *
94 * @return a file extension.
95 */
96 public String fileExtension() {
97 return fileExtension;
98 }
99
100 ObjectType(byte byteValue, String fileExtension) {
101 this.byteValue = byteValue;
102 this.fileExtension = fileExtension;
103 }
104
105 /**
106 * Returns the {@link ObjectType} corresponding to a serialized GVariant value.
107 *
108 * @param byteValue a serialized value as used in GVariant.
109 * @return the {@link ObjectType} corresponding to the serialized value.
110 * @throws IllegalArgumentException if the byte value is invalid.
111 */
112 public static ObjectType valueOf(byte byteValue) {
113 return switch (byteValue) {
114 case 1 -> FILE;
115 case 2 -> DIR_TREE;
116 case 3 -> DIR_META;
117 case 4 -> COMMIT;
118 case 5 -> TOMBSTONE_COMMIT;
119 case 6 -> COMMIT_META;
120 case 7 -> PAYLOAD_LINK;
121 default -> throw new IllegalArgumentException("invalid ObjectType: %d".formatted(byteValue));
122 };
123 }
124}