1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
use std::fmt::{self, Display, Formatter, LowerHex, UpperHex};
/// Unique identifiers used to indicate various components of a WEBC file.
///
/// # Top-Level Sections
///
/// In general, all top-level sections are [Type-Length-Value][tlv] encoded.
///
/// - [`Tag`] (`u8`)
/// - Length (`u64` LE)
/// - Value (`Length` bytes)
///
/// # Versioning
///
/// Besides acting as an identifier for various elements in a WEBC file, the
/// [`Tag`] plays an important part in versioning. An item's layout is tied
/// to its [`Tag`], so any time the layout is changed, a new unique [`Tag`]
/// should be created.
///
/// For example, if the format for a volume needs to be changed, a new
/// `Tag::VolumeV2` variant would be added instead of modifying the spec for
/// existing volumes. Future reader implementations then need to handle
/// [`Tag::Volume`] and `Tag::VolumeV2` gracefully.
///
/// [tlv]: https://en.wikipedia.org/wiki/Type%E2%80%93length%E2%80%93value
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
#[non_exhaustive]
#[serde(rename_all = "kebab-case")]
pub enum Tag {
/*
Top-level sections
*/
/// The manifest section, containing a CBOR-serialized
/// [`crate::metadata::Manifest`].
Manifest = 1,
/// The index section, containing a CBOR-serialized [`crate::v3::Index`].
///
/// The index section is laid out as follows:
///
/// - [`Tag::Index`]
/// - overall section length (`u64` LE)
/// - CBOR data
/// - padding
///
/// # Note to Implementors
///
/// This section may contain trailing padding bytes that should be ignored
/// when deserializing the index.
///
/// Depending on the writer implementation, padding bytes may be necessary
/// because the [`crate::v3::Index`] needs to be at the start of a WEBC file,
/// yet it can only be calculated after everything has been serialized.
Index = 2,
/// The atoms section.
///
/// An atoms section is laid out similar to [`Tag::Volume`].
///
/// - [`Tag::Atoms`]
/// - overall section length (`u64` LE)
/// - volume header length (`u64` LE)
/// - volume header
/// - volume data length (`u64` LE)
/// - volume data
Atoms = 3,
/// The volume section.
///
/// A volume section is laid out similar to [`Tag::Atoms`].
///
/// - [`Tag::Volume`]
/// - overall section length
/// - volume name length (`u64` LE)
/// - volume name (`u64` LE)
/// - volume header length (`u64` LE)
/// - volume header
/// - volume data length (`u64` LE)
/// - volume data
Volume = 4,
/*
* Identifiers for components of the index.
*/
/// A tag for the empty checksum (i.e. [`crate::v3::Checksum::none()`]).
ChecksumNone = 20,
/// A tag indicating that the [`crate::v3::Checksum`] is calculated using the
/// SHA-256 hash of a section's contents.
///
/// See also, [`crate::v3::Checksum::sha256()`].
ChecksumSha256 = 21,
/// A tag for the empty signature (i.e. [`crate::v3::Signature::none()`]).
SignatureNone = 22,
/*
* Identifiers for components in a volume header.
*/
/// Metadata for a directory in the [`Tag::Volume`] header.
///
/// Each directory follows the type-length-value format.
///
/// - [`Tag::Directory`]
/// - overall directory length (`u64` LE)
/// - zero or more entries
///
/// Where each entry is
///
/// - offset of the entry relative to the start of the header (`u64` LE)
/// - name length (`u64` LE)
/// - name (arbitrary number of bytes)
Directory = 30,
/// Metadata for a file in the [`Tag::Volume`] header.
///
/// File metadata follows the following format:
///
/// - [`Tag::File`]
/// - start offset in data section (`u64` LE)
/// - end offset in data section (`u64` LE)
/// - SHA256 checksum (32 bytes)
File = 31,
}
impl Tag {
/// Get an iterator over all the tag variants.
pub const ALL: [Tag; 9] = [
Tag::Manifest,
Tag::Index,
Tag::Atoms,
Tag::Volume,
Tag::ChecksumNone,
Tag::ChecksumSha256,
Tag::SignatureNone,
Tag::Directory,
Tag::File,
];
/// Try to load a [`Tag`] from its [`u8`] representation.
pub const fn from_u8(tag: u8) -> Option<Self> {
match tag {
1 => Some(Tag::Manifest),
2 => Some(Tag::Index),
3 => Some(Tag::Atoms),
4 => Some(Tag::Volume),
20 => Some(Tag::ChecksumNone),
21 => Some(Tag::ChecksumSha256),
22 => Some(Tag::SignatureNone),
30 => Some(Tag::Directory),
31 => Some(Tag::File),
_ => None,
}
}
/// Get the [`Tag`]'s [`u8`] representation.
pub const fn as_u8(self) -> u8 {
self as u8
}
/// Get the [`Tag`]'s human-friendly name.
pub const fn name(self) -> &'static str {
match self {
Tag::Manifest => "Manifest",
Tag::Index => "Index",
Tag::Atoms => "Atoms",
Tag::Volume => "Volume",
Tag::ChecksumNone => "No Checksum",
Tag::ChecksumSha256 => "SHA-256 Checksum",
Tag::SignatureNone => "No Signature",
Tag::Directory => "Directory",
Tag::File => "File",
}
}
/// Gets a displayable implementation for something that may or may not be
/// a known tag.
///
/// Unknown tags are printed in their hexadecimal representation.
pub fn display(maybe_tag: u8) -> impl Display {
enum MaybeTag {
Known(Tag),
Unknown(u8),
}
impl Display for MaybeTag {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MaybeTag::Known(tag) => Display::fmt(tag, f),
MaybeTag::Unknown(other) => write!(f, "{other:#x}"),
}
}
}
match Tag::from_u8(maybe_tag) {
Some(tag) => MaybeTag::Known(tag),
None => MaybeTag::Unknown(maybe_tag),
}
}
}
impl Display for Tag {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self.name(), f)
}
}
impl LowerHex for Tag {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
LowerHex::fmt(&self.as_u8(), f)
}
}
impl UpperHex for Tag {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
UpperHex::fmt(&self.as_u8(), f)
}
}
impl PartialEq<u8> for Tag {
fn eq(&self, other: &u8) -> bool {
self.as_u8() == *other
}
}
impl PartialEq<Tag> for u8 {
fn eq(&self, other: &Tag) -> bool {
other == self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_tags_round_trip() {
for tag in Tag::ALL {
let byte = tag.as_u8();
let round_tripped = Tag::from_u8(byte).unwrap();
assert_eq!(tag, round_tripped);
}
}
}