Skip to content

Commit

Permalink
Bail for excluded languages
Browse files Browse the repository at this point in the history
  • Loading branch information
ackwell committed Jun 23, 2024
1 parent ccf272f commit 02e7215
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions boilmaster.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ filter.exdschema.list = "Name,Singular,Icon"

[read.language]
default = "en"
# This default configuration is set up for the global game client, which does not ship Chinese or Korean data.
exclude = ["chs", "cht", "kr"]

[version]
interval = 3600 # 1 hour
Expand Down
9 changes: 8 additions & 1 deletion src/read/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fmt, str::FromStr};
use ironworks::excel::Language;
use schemars::{
gen::SchemaGenerator,
schema::{InstanceType, Schema, SchemaObject},
schema::{InstanceType, Metadata, Schema, SchemaObject},
};
use serde::de;

Expand Down Expand Up @@ -93,6 +93,13 @@ fn languagestring_schema(_generator: &mut SchemaGenerator) -> Schema {
];

Schema::Object(SchemaObject {
metadata: Some(
Metadata {
description: Some("Known languages supported by the game data format. **NOTE:** Not all languages that are supported by the format are valid for all editions of the game. For example, the global game client acknowledges the existence of `chs` and `kr`, however does not provide any data for them.".into()),
..Default::default()
}
.into(),
),
instance_type: Some(InstanceType::String.into()),
enum_values: Some(
languages
Expand Down
33 changes: 29 additions & 4 deletions src/read/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
borrow::Cow,
collections::{hash_map, HashMap},
collections::{hash_map, HashMap, HashSet},
iter,
ops::Range,
};
Expand Down Expand Up @@ -28,16 +28,24 @@ pub struct Config {
#[derive(Debug, Deserialize)]
struct LanguageConfig {
default: LanguageString,
exclude: Vec<LanguageString>,
}

pub struct Read {
default_language: excel::Language,
excluded_languages: HashSet<excel::Language>,
}

impl Read {
pub fn new(config: Config) -> Self {
Self {
default_language: config.language.default.into(),
excluded_languages: config
.language
.exclude
.into_iter()
.map(|language| language.into())
.collect(),
}
}

Expand All @@ -60,6 +68,8 @@ impl Read {
depth: u8,
) -> Result<Value> {
let value = read_sheet(ReaderContext {
read: self,

excel,
schema,

Expand Down Expand Up @@ -224,7 +234,7 @@ fn read_scalar_reference(
// TODO: handle target selectors
let row_data = match sheet_data
.with()
.language(context.language)
.language(context.validated_language()?)
.row(target_value)
{
Err(ironworks::Error::NotFound(ironworks::ErrorValue::Row { .. })) => continue,
Expand Down Expand Up @@ -482,6 +492,8 @@ fn unknown_suffix(kind: exh::ColumnKind) -> &'static str {
}

struct ReaderContext<'a> {
read: &'a Read,

excel: &'a excel::Excel<'a>,
schema: &'a dyn schema::Schema,

Expand All @@ -504,20 +516,33 @@ impl ReaderContext<'_> {
)
})?;

let row = match self.rows.entry(self.language) {
let language = self.validated_language()?;

let row = match self.rows.entry(language) {
hash_map::Entry::Occupied(entry) => entry.into_mut(),
hash_map::Entry::Vacant(entry) => entry.insert(
self.excel
.sheet(self.sheet)?
.with()
.language(self.language)
.language(language)
.subrow(self.row_id, self.subrow_id)?,
),
};

Ok(row.field(column)?)
}

fn validated_language(&self) -> Result<excel::Language> {
if self.read.excluded_languages.contains(&self.language) {
return Err(Error::InvalidLanguage(format!(
"{}",
LanguageString::from(self.language)
)));
}

Ok(self.language)
}

fn mismatch_error(&self, reason: impl ToString) -> MismatchError {
MismatchError {
field: "TODO: contextual filter path".into(),
Expand Down

0 comments on commit 02e7215

Please sign in to comment.