blob: dc38b1b5fd8756efb332647d21462bc4d9cd9b2b [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 Benkard4e8423d2021-12-19 22:56:09 +01005package eu.mulk.jgvariant.ostree;
6
7import eu.mulk.jgvariant.core.Decoder;
8import java.nio.charset.StandardCharsets;
9import java.util.List;
10
11/**
12 * Metadata describing files and directories of a file tree.
13 *
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010014 * <p>Often comes in a pair with {@link DirMeta}.
15 *
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010016 * <p>Referenced by {@link Commit#rootDirTreeChecksum()} and recursively by {@link
17 * Directory#treeChecksum()}.
18 *
19 * <p>Reference: {@code ostree-core.h#OSTREE_TREE_GVARIANT_STRING}
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010020 *
21 * @param files a list of files in the directory.
22 * @param directories a list of subdirectories of the directory.
23 * @see DirMeta
24 * @see ObjectType#DIR_TREE
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010025 */
26public record DirTree(List<File> files, List<Directory> directories) {
27
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010028 /**
29 * A file in a file tree.
30 *
31 * @param name the file name.
32 * @param checksum the checksum of the {@link ObjectType#FILE} object.
33 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010034 public record File(String name, Checksum checksum) {
35
36 private static final Decoder<File> DECODER =
37 Decoder.ofStructure(
38 File.class, Decoder.ofString(StandardCharsets.UTF_8), Checksum.decoder());
39
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010040 /**
41 * Acquires a {@link Decoder} for the enclosing type.
42 *
43 * @return a possibly shared {@link Decoder}.
44 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010045 public static Decoder<File> decoder() {
46 return DECODER;
47 }
48 }
49
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010050 /**
51 * A subdirectory in a file tree.
52 *
53 * @param name the name of the subdirectory.
54 * @param treeChecksum the checksum of the {@link DirTree} object.
55 * @param dirChecksum the checksum of the {@link DirMeta} object.
56 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010057 public record Directory(String name, Checksum treeChecksum, Checksum dirChecksum) {
58
59 private static final Decoder<Directory> DECODER =
60 Decoder.ofStructure(
61 Directory.class,
62 Decoder.ofString(StandardCharsets.UTF_8),
63 Checksum.decoder(),
64 Checksum.decoder());
65
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010066 /**
67 * Acquires a {@link Decoder} for the enclosing type.
68 *
69 * @return a possibly shared {@link Decoder}.
70 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010071 public static Decoder<Directory> decoder() {
72 return DECODER;
73 }
74 }
75
76 private static final Decoder<DirTree> DECODER =
77 Decoder.ofStructure(
78 DirTree.class, Decoder.ofArray(File.decoder()), Decoder.ofArray(Directory.decoder()));
79
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010080 /**
81 * Acquires a {@link Decoder} for the enclosing type.
82 *
83 * @return a possibly shared {@link Decoder}.
84 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010085 public static Decoder<DirTree> decoder() {
86 return DECODER;
87 }
88}