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

Support for JSON5 file format #143

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions leptos_i18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ nightly = ["leptos/nightly", "leptos_i18n_macro/nightly"]
show_keys_only = ["leptos_i18n_macro/show_keys_only"]
suppress_key_warnings = ["leptos_i18n_macro/suppress_key_warnings"]
json_files = ["leptos_i18n_macro/json_files"]
json5_files = ["leptos_i18n_macro/json5_files"]
yaml_files = ["leptos_i18n_macro/yaml_files"]
interpolate_display = ["leptos_i18n_macro/interpolate_display"]
track_locale_files = ["leptos_i18n_macro/track_locale_files"]
Expand All @@ -59,9 +60,10 @@ track_locale_files = ["leptos_i18n_macro/track_locale_files"]
[package.metadata.cargo-all-features]
denylist = [
# Always exclude:
"ssr", # Should always be enabled via a server integration rather than directly
"yaml_files", # See leptos_i18n_macro manifest to see why "yaml_files" and other formats are in deny list and JSON is always included
"nightly", # Requires a nightly toolchain
"ssr", # Should always be enabled via a server integration rather than directly
"yaml_files", # See leptos_i18n_macro manifest to see why "yaml_files" and other formats are in deny list and JSON is always included
"json5_files",
"nightly", # Requires a nightly toolchain

# Only passed through to `leptos_i18n_macros`, exclude to save time:
"serde",
Expand Down
6 changes: 4 additions & 2 deletions leptos_i18n_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ syn = "2.0"
toml = "0.8"
icu = "1.5"
fixed_decimal = { version = "0.5", features = ["ryu"] }
json5 = { version = "0.4", optional = true }

[features]
default = ["json_files"]
nightly = []
suppress_key_warnings = []
json_files = []
yaml_files = ["serde_yaml"]
yaml_files = ["dep:serde_yaml"]
json5_files = ["dep:json5"]
interpolate_display = []
track_locale_files = []
experimental-islands = []
show_keys_only = []

[package.metadata.cargo-all-features]
# cargo-all-features don't provide a way to always include one feature in a set, so CI will just do json...
denylist = ["nightly", "yaml_files"]
denylist = ["nightly", "yaml_files", "json5_files"]
always_include_features = ["json_files"]
53 changes: 43 additions & 10 deletions leptos_i18n_macro/src/load_locales/locale.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
collections::HashMap,
fs::File,
io::{BufReader, Read},
path::{Path, PathBuf},
rc::Rc,
};
Expand All @@ -19,20 +20,24 @@ use crate::utils::key::{Key, KeyPath, CACHED_VAR_COUNT_KEY};

macro_rules! define_by_format {
(json => $($tt:tt)*) => {
#[cfg(all(feature = "json_files", not(any(feature = "yaml_files"))))]
#[cfg(all(feature = "json_files", not(any(feature = "yaml_files", feature = "json5_files"))))]
$($tt)*
};
(yaml => $($tt:tt)*) => {
#[cfg(all(feature = "yaml_files", not(any(feature = "json_files"))))]
#[cfg(all(feature = "yaml_files", not(any(feature = "json_files", feature = "json5_files"))))]
$($tt)*
};
(json5 => $($tt:tt)*) => {
#[cfg(all(feature = "json5_files", not(any(feature = "json_files", feature = "yaml_files"))))]
$($tt)*
};
(none => $($tt:tt)*) => {
#[cfg(not(any(feature = "json_files", feature = "yaml_files")))]
#[cfg(not(any(feature = "json_files", feature = "yaml_files", feature = "json5_files")))]
$($tt)*
};
// for now use cfg(all(..)) but if any format is added found a better cfg.
// This is attrocious, found a better way fgs
(multiple => $($tt:tt)*) => {
#[cfg(all(feature = "json_files", feature = "yaml_files"))]
#[cfg(any(all(feature = "json_files", feature = "yaml_files"), all(feature = "json_files", feature = "json5_files"), all(feature = "yaml_files", feature = "json5_files")))]
$($tt)*
}
}
Expand All @@ -52,36 +57,63 @@ macro_rules! define_files_exts {
};
}

#[cfg(feature = "json5_files")]
#[derive(Debug)]
pub enum Json5Error {
Serde(json5::Error),
Io(std::io::Error),
}

#[cfg(feature = "json5_files")]
impl std::fmt::Display for Json5Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Json5Error::Serde(error) => std::fmt::Display::fmt(error, f),
Json5Error::Io(error) => std::fmt::Display::fmt(error, f),
}
}
}

define_error!(json => serde_json::Error);
define_error!(json5 => Json5Error);
define_error!(yaml => serde_yaml::Error);
define_error!(none => &'static str); // whatever impl Display
define_error!(multiple => &'static str); // whatever impl Display

define_files_exts!(json => "json");
define_files_exts!(json5 => "json5");
define_files_exts!(yaml => "yaml", "yml");
define_files_exts!(none);
define_files_exts!(multiple);

define_by_format!(json =>
fn de_inner(locale_file: File, seed: LocaleSeed) -> Result<Locale, SerdeError> {
fn de_inner<R: Read>(locale_file: R, seed: LocaleSeed) -> Result<Locale, SerdeError> {
let mut deserializer = serde_json::Deserializer::from_reader(locale_file);
serde::de::DeserializeSeed::deserialize(seed, &mut deserializer)
}
);
define_by_format!(json5 =>
fn de_inner<R: Read>(mut locale_file: R, seed: LocaleSeed) -> Result<Locale, SerdeError> {
let mut buff = String::new();
Read::read_to_string(&mut locale_file, &mut buff).map_err(Json5Error::Io)?;
let mut deserializer = json5::Deserializer::from_str(&buff).map_err(Json5Error::Serde)?;
serde::de::DeserializeSeed::deserialize(seed, &mut deserializer).map_err(Json5Error::Serde)
}
);
define_by_format!(yaml =>
fn de_inner(locale_file: File, seed: LocaleSeed) -> Result<Locale, SerdeError> {
fn de_inner<R: Read>(locale_file: R, seed: LocaleSeed) -> Result<Locale, SerdeError> {
let deserializer = serde_yaml::Deserializer::from_reader(locale_file);
serde::de::DeserializeSeed::deserialize(seed, deserializer)
}
);
define_by_format!(none =>
fn de_inner(locale_file: File, seed: LocaleSeed) -> Result<Locale, SerdeError> {
fn de_inner<R: Read>(locale_file: R, seed: LocaleSeed) -> Result<Locale, SerdeError> {
let _ = (locale_file, seed);
compile_error!("No file format has been provided for leptos_i18n, supported formats are: json and yaml")
}
);
define_by_format!(multiple =>
fn de_inner(locale_file: File, seed: LocaleSeed) -> Result<Locale, SerdeError> {
fn de_inner<R: Read>(locale_file: R, seed: LocaleSeed) -> Result<Locale, SerdeError> {
let _ = (locale_file, seed);
compile_error!("Multiple file format have been provided for leptos_i18n, choose only one, supported formats are: json and yaml")
}
Expand Down Expand Up @@ -257,7 +289,8 @@ impl Locale {
}

fn de(locale_file: File, path: &mut PathBuf, seed: LocaleSeed) -> Result<Self> {
de_inner(locale_file, seed).map_err(|err| Error::LocaleFileDeser {
let reader = BufReader::new(locale_file);
de_inner(reader, seed).map_err(|err| Error::LocaleFileDeser {
path: std::mem::take(path),
err,
})
Expand Down