Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: update rman schema #32

Merged
merged 2 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rman-schema
8 changes: 4 additions & 4 deletions src/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

mod bundle_entry;
mod chunk_entry;
mod chunking_param_entry;
mod directory_entry;
mod file_entry;
mod key_entry;
mod language_entry;
mod param_entry;
mod tag_entry;

pub use self::bundle_entry::BundleEntry;
pub use self::chunk_entry::ChunkEntry;
pub use self::chunking_param_entry::ChunkingParamEntry;
pub use self::directory_entry::DirectoryEntry;
pub use self::file_entry::FileEntry;
pub use self::key_entry::KeyEntry;
pub use self::language_entry::LanguageEntry;
pub use self::param_entry::ParamEntry;
pub use self::tag_entry::TagEntry;
22 changes: 11 additions & 11 deletions src/entries/param_entry.rs → src/entries/chunking_param_entry.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::generated::rman::Param;
use crate::generated::rman::ChunkingParam;

/// Single param entry object.
/// Single chunking param entry object.
///
/// This is identical to the schema in [rman-schema][rman-schema] and exists to provide a
/// persistent structure for the `ParamEntry`.
/// persistent structure for the `ChunkingParamEntry`.
///
/// [rman-schema]: https://github.com/ev3nvy/rman-schema
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ParamEntry {
pub struct ChunkingParamEntry {
/// Field with an unknown function and type (it might also be an [`i16`]).
pub unk0: u16,
/// Determines the hash type used when generating chunks.
Expand Down Expand Up @@ -48,13 +48,13 @@ pub struct ParamEntry {
pub max_chunk_size: u32,
}

impl From<Param<'_>> for ParamEntry {
fn from(param: Param) -> Self {
let unk0 = param.unk0();
let chunking_version = param.chunking_version();
let min_chunk_size = param.min_chunk_size();
let chunk_size = param.chunk_size();
let max_chunk_size = param.max_chunk_size();
impl From<ChunkingParam<'_>> for ChunkingParamEntry {
fn from(chunking_param: ChunkingParam) -> Self {
let unk0 = chunking_param.unk0();
let chunking_version = chunking_param.chunking_version();
let min_chunk_size = chunking_param.min_chunk_size();
let chunk_size = chunking_param.chunk_size();
let max_chunk_size = chunking_param.max_chunk_size();

Self {
unk0,
Expand Down
16 changes: 8 additions & 8 deletions src/entries/file_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub struct FileEntry {
pub size: u32,
/// Name of the file entry.
pub name: String,
/// Applicable languages, stored as a bit mask.
pub language_mask: u64,
/// Applicable tags, stored as a bitmask.
pub tag_bitmask: u64,
/// Field with an unknown function and type (it might also be an [`i8`]).
pub unk5: u8,
/// Field with an unknown function and type (it might also be an [`i8`]).
Expand All @@ -33,8 +33,8 @@ pub struct FileEntry {
pub symlink: String,
/// Field with an unknown function and type (it might also be an [`i16`]).
pub unk10: u16,
/// Id of the param entry, which provides info about content-defined chunking.
pub param_id: u8,
/// Id of the chunking param entry, which provides info about content-defined chunking.
pub chunking_param_id: u8,
/// Permissions for the given file entry.
pub permissions: u8,
}
Expand All @@ -45,14 +45,14 @@ impl From<File<'_>> for FileEntry {
let directory_id = file.directory_id();
let size = file.size_();
let name = file.name().unwrap_or_default().to_owned();
let language_mask = file.language_mask();
let tag_bitmask = file.tag_bitmask();
let unk5 = file.unk5();
let unk6 = file.unk6();
let chunk_ids = file.chunk_ids().unwrap_or_default();
let unk8 = file.unk8();
let symlink = file.symlink().unwrap_or_default().to_owned();
let unk10 = file.unk10();
let param_id = file.param_id();
let chunking_param_id = file.chunking_param_id();
let permissions = file.permissions();

let chunk_ids = chunk_ids.iter().collect();
Expand All @@ -62,14 +62,14 @@ impl From<File<'_>> for FileEntry {
directory_id,
size,
name,
language_mask,
tag_bitmask,
unk5,
unk6,
chunk_ids,
unk8,
symlink,
unk10,
param_id,
chunking_param_id,
permissions,
}
}
Expand Down
36 changes: 0 additions & 36 deletions src/entries/language_entry.rs

This file was deleted.

36 changes: 36 additions & 0 deletions src/entries/tag_entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::generated::rman::Tag;

/// Single tag entry object.
///
/// This is identical to the schema in [rman-schema][rman-schema] and exists to provide a
/// persistent structure for the `TagEntry`.
///
/// [rman-schema]: https://github.com/ev3nvy/rman-schema
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TagEntry {
/// Id of the tag entry.
pub id: u8,
/// Name of the tag entry.
///
/// A non-exhaustive list of values:
/// - language in the language-region variant of the [RFC 5646 standard][rfc-5646] but with
/// underscores instead of hyphens,
/// - `krrating`,
/// - `mature`,
/// - `twmlogo`,
/// - `vnglogo`,
/// - `all_loc`.
///
/// [rfc-5646]: https://www.rfc-editor.org/rfc/rfc5646.html
pub name: String,
}

impl From<Tag<'_>> for TagEntry {
fn from(tag: Tag) -> Self {
let id = tag.id();
let name = tag.name().unwrap_or_default().to_owned();

Self { id, name }
}
}
24 changes: 12 additions & 12 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub struct File {
pub path: String,
/// Symbolic link of the file.
pub symlink: String,
/// A vector of applicable languages.
pub languages: Vec<String>,
/// A vector of applicable tags.
pub tags: Vec<String>,
#[allow(dead_code)]
chunks: Vec<(i64, u32, u32, u32)>,
}
Expand All @@ -41,9 +41,9 @@ impl File {
/// [`HashMap`]s used for fast lookups for the required data.
///
/// Here is how they are structured:
/// - Parameter `language_entries` is a [`HashMap`] where the key is a
/// [language id](crate::entries::LanguageEntry::id) and the value is a
/// [language name](crate::entries::LanguageEntry::name).
/// - Parameter `tag_entries` is a [`HashMap`] where the key is a
/// [tag id](crate::entries::TagEntry::id) and the value is a
/// [tag name](crate::entries::TagEntry::name).
///
/// - Parameter `directories` is a [`HashMap`] where the key is a
/// [directory id](crate::entries::DirectoryEntry::id) and the value is a tuple of:
Expand All @@ -70,7 +70,7 @@ impl File {
/// returned.
pub fn parse(
file: &FileEntry,
language_entries: &HashMap<u8, String>,
tag_entries: &HashMap<u8, String>,
directories: &HashMap<i64, (String, i64)>,
chunk_entries: &HashMap<i64, (i64, u32, u32, u32)>,
) -> Result<Self> {
Expand All @@ -79,7 +79,7 @@ impl File {
let permissions = file.permissions;
let size = file.size;
let symlink = file.symlink.clone();
let language_mask = file.language_mask;
let tag_bitmask = file.tag_bitmask;
let chunk_ids = &file.chunk_ids;

let mut directory_id = file.directory_id;
Expand All @@ -96,15 +96,15 @@ impl File {

path.push_str(&name);

let mut languages = Vec::new();
let mut tags = Vec::new();

for i in 0..64 {
if (language_mask & (1u64 << i)) == 0 {
if (tag_bitmask & (1u64 << i)) == 0 {
continue;
}

if let Some(lang_name) = language_entries.get(&(i + 1)) {
languages.push(lang_name.clone());
if let Some(tag_name) = tag_entries.get(&(i + 1)) {
tags.push(tag_name.clone());
}
}

Expand All @@ -125,7 +125,7 @@ impl File {
size,
path,
symlink,
languages,
tags,
chunks,
};
Ok(file)
Expand Down
31 changes: 15 additions & 16 deletions src/parser/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;

use crate::entries::{BundleEntry, DirectoryEntry, FileEntry, KeyEntry, LanguageEntry, ParamEntry};
use crate::entries::{
BundleEntry, ChunkingParamEntry, DirectoryEntry, FileEntry, KeyEntry, TagEntry,
};
use crate::generated::rman::root_as_manifest;
use crate::File;
use crate::Result;
Expand All @@ -16,10 +18,10 @@ pub struct ManifestData {
pub file_entries: Vec<FileEntry>,
/// Vector of [key entries][crate::entries::KeyEntry].
pub key_entries: Vec<KeyEntry>,
/// Vector of [language entries][crate::entries::LanguageEntry].
pub language_entries: Vec<LanguageEntry>,
/// Vector of [param entries][crate::entries::ParamEntry].
pub param_entries: Vec<ParamEntry>,
/// Vector of [tag entries][crate::entries::TagEntry].
pub tag_entries: Vec<TagEntry>,
/// Vector of [chunking param entries][crate::entries::ChunkingParamEntry].
pub chunking_param_entries: Vec<ChunkingParamEntry>,
/// Vector of [files][crate::File].
pub files: Vec<File>,
}
Expand Down Expand Up @@ -59,34 +61,31 @@ impl ManifestData {
let directory_entries: Vec<_> = map_vector!(manifest, directories, DirectoryEntry);
let file_entries: Vec<_> = map_vector!(manifest, files, FileEntry);
let key_entries = map_vector!(manifest, keys, KeyEntry);
let language_entries: Vec<_> = map_vector!(manifest, languages, LanguageEntry);
let param_entries = map_vector!(manifest, params, ParamEntry);
let tag_entries: Vec<_> = map_vector!(manifest, tags, TagEntry);
let chunking_param_entries = map_vector!(manifest, chunking_params, ChunkingParamEntry);

let mapped_languages = Self::map_languages(&language_entries);
let mapped_tags = Self::map_tags(&tag_entries);
let mapped_directories = Self::map_directories(&directory_entries);
let mapped_chunks = Self::map_chunks(&bundle_entries);

let files = file_entries
.iter()
.map(|f| File::parse(f, &mapped_languages, &mapped_directories, &mapped_chunks))
.map(|f| File::parse(f, &mapped_tags, &mapped_directories, &mapped_chunks))
.collect::<Result<Vec<File>>>()?;

Ok(Self {
bundle_entries,
directory_entries,
file_entries,
key_entries,
language_entries,
param_entries,
tag_entries,
chunking_param_entries,
files,
})
}

fn map_languages(language_entries: &[LanguageEntry]) -> HashMap<u8, String> {
language_entries
.iter()
.map(|l| (l.id, l.name.clone()))
.collect()
fn map_tags(tag_entries: &[TagEntry]) -> HashMap<u8, String> {
tag_entries.iter().map(|l| (l.id, l.name.clone())).collect()
}

fn map_directories(directory_entries: &[DirectoryEntry]) -> HashMap<i64, (String, i64)> {
Expand Down
Loading