blob: ef7d05f69e8b13b4c43cd6e529b9378e28d7daf0 [file] [log] [blame]
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01001package eu.mulk.jgvariant.ostree;
2
3import eu.mulk.jgvariant.core.Decoder;
4import java.nio.charset.StandardCharsets;
5import java.util.List;
6
7/**
8 * Metadata describing files and directories of a file tree.
9 *
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010010 * <p>Often comes in a pair with {@link DirMeta}.
11 *
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010012 * <p>Referenced by {@link Commit#rootDirTreeChecksum()} and recursively by {@link
13 * Directory#treeChecksum()}.
14 *
15 * <p>Reference: {@code ostree-core.h#OSTREE_TREE_GVARIANT_STRING}
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010016 *
17 * @param files a list of files in the directory.
18 * @param directories a list of subdirectories of the directory.
19 * @see DirMeta
20 * @see ObjectType#DIR_TREE
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010021 */
22public record DirTree(List<File> files, List<Directory> directories) {
23
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010024 /**
25 * A file in a file tree.
26 *
27 * @param name the file name.
28 * @param checksum the checksum of the {@link ObjectType#FILE} object.
29 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010030 public record File(String name, Checksum checksum) {
31
32 private static final Decoder<File> DECODER =
33 Decoder.ofStructure(
34 File.class, Decoder.ofString(StandardCharsets.UTF_8), Checksum.decoder());
35
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010036 /**
37 * Acquires a {@link Decoder} for the enclosing type.
38 *
39 * @return a possibly shared {@link Decoder}.
40 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010041 public static Decoder<File> decoder() {
42 return DECODER;
43 }
44 }
45
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010046 /**
47 * A subdirectory in a file tree.
48 *
49 * @param name the name of the subdirectory.
50 * @param treeChecksum the checksum of the {@link DirTree} object.
51 * @param dirChecksum the checksum of the {@link DirMeta} object.
52 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010053 public record Directory(String name, Checksum treeChecksum, Checksum dirChecksum) {
54
55 private static final Decoder<Directory> DECODER =
56 Decoder.ofStructure(
57 Directory.class,
58 Decoder.ofString(StandardCharsets.UTF_8),
59 Checksum.decoder(),
60 Checksum.decoder());
61
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010062 /**
63 * Acquires a {@link Decoder} for the enclosing type.
64 *
65 * @return a possibly shared {@link Decoder}.
66 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010067 public static Decoder<Directory> decoder() {
68 return DECODER;
69 }
70 }
71
72 private static final Decoder<DirTree> DECODER =
73 Decoder.ofStructure(
74 DirTree.class, Decoder.ofArray(File.decoder()), Decoder.ofArray(Directory.decoder()));
75
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010076 /**
77 * Acquires a {@link Decoder} for the enclosing type.
78 *
79 * @return a possibly shared {@link Decoder}.
80 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010081 public static Decoder<DirTree> decoder() {
82 return DECODER;
83 }
84}