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

Move FilesystemExporter from trait object to generic #6025

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions provider/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ serde_json = { workspace = true, features = ["std"], optional = true }
[dev-dependencies]
icu_benchmark_macros = { path = "../../tools/benchmark/macros" }
icu_locale_core = { path = "../../components/locale_core", default-features = false, features = ["serde"] }
icu_locale = { path = "../../components/locale", default-features = false, features = ["compiled_data"] }
icu_provider = { path = "../../provider/core", default-features = false, features = ["deserialize_json", "deserialize_bincode_1", "deserialize_postcard_1", "export"] }
icu_provider_export = { path = "../../provider/export", default-features = false }
writeable = { path = "../../utils/writeable" }
Expand Down
13 changes: 5 additions & 8 deletions provider/fs/src/export/fs_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,20 @@ impl From<PathBuf> for Options {
/// A data exporter that writes data to a filesystem hierarchy.
/// See the module-level docs for an example.
#[derive(Debug)]
pub struct FilesystemExporter {
pub struct FilesystemExporter<Serializer> {
root: PathBuf,
manifest: Manifest,
serializer: Box<dyn AbstractSerializer + Sync>,
serializer: Serializer,
}

impl FilesystemExporter {
impl<Serializer: AbstractSerializer> FilesystemExporter<Serializer> {
/// Creates a new [`FilesystemExporter`] with a [serializer] and [options].
///
/// See the module-level docs for an example.
///
/// [serializer]: crate::export::serializers
/// [options]: Options
pub fn try_new(
serializer: Box<dyn AbstractSerializer + Sync>,
options: Options,
) -> Result<Self, DataError> {
pub fn try_new(serializer: Serializer, options: Options) -> Result<Self, DataError> {
let result = FilesystemExporter {
root: options.root,
manifest: Manifest::for_format(serializer.get_buffer_format())?,
Expand All @@ -99,7 +96,7 @@ impl FilesystemExporter {
}
}

impl DataExporter for FilesystemExporter {
impl<Serializer: AbstractSerializer + Sync> DataExporter for FilesystemExporter<Serializer> {
fn put_payload(
&self,
marker: DataMarkerInfo,
Expand Down
2 changes: 1 addition & 1 deletion provider/fs/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//! // Set up the exporter
//! let mut options = Options::default();
//! options.root = demo_path.clone();
//! let serializer = Box::new(serializers::Json::default());
//! let serializer = serializers::Json::default();
//! let mut exporter = FilesystemExporter::try_new(serializer, options)
//! .expect("Should successfully initialize data output directory");
//!
Expand Down
2 changes: 1 addition & 1 deletion provider/fs/src/export/serializers/bincode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::io;
/// // Then pass it to a FilesystemExporter:
/// let demo_path = std::env::temp_dir().join("icu4x_bincode_serializer_demo");
/// FilesystemExporter::try_new(
/// Box::from(serializer),
/// serializer,
/// demo_path.clone().into(),
/// )
/// .unwrap();
Expand Down
2 changes: 1 addition & 1 deletion provider/fs/src/export/serializers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum StyleOption {
/// // Then pass it to a FilesystemExporter:
/// let demo_path = std::env::temp_dir().join("icu4x_json_serializer_demo");
/// FilesystemExporter::try_new(
/// Box::from(serializer),
/// serializer,
/// demo_path.clone().into(),
/// )
/// .unwrap();
Expand Down
2 changes: 1 addition & 1 deletion provider/fs/src/export/serializers/postcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::io;
/// // Then pass it to a FilesystemExporter:
/// let demo_path = std::env::temp_dir().join("icu4x_postcard_serializer_demo");
/// FilesystemExporter::try_new(
/// Box::from(serializer),
/// serializer,
/// demo_path.clone().into(),
/// )
/// .unwrap();
Expand Down
53 changes: 25 additions & 28 deletions provider/icu4x-datagen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,6 @@ impl CollationTable {
}
}

// Mirrors crate::FallbackMode
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum Fallback {
Auto,
Hybrid,
Runtime,
RuntimeManual,
Preresolved,
}

// Mirrors crate::DeduplicationStrategy
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
enum Deduplication {
Expand Down Expand Up @@ -541,26 +531,33 @@ fn main() -> eyre::Result<()> {
eyre::bail!("Exporting to an FsProvider requires the `fs_exporter` Cargo feature")
}
#[cfg(feature = "fs_exporter")]
Format::Fs => driver.export(&provider, {
Format::Fs => {
use icu_provider_export::fs_exporter::*;
let mut options = Options::default();
options.root = cli.output.unwrap_or_else(|| PathBuf::from("icu4x_data"));
if cli.overwrite {
options.overwrite = OverwriteOption::RemoveAndReplace
}

FilesystemExporter::try_new(
match cli.syntax {
Syntax::Bincode => Box::<serializers::Bincode>::default(),
Syntax::Postcard => Box::<serializers::Postcard>::default(),
Syntax::Json if cli.pretty => Box::new(serializers::Json::pretty()),
Syntax::Json => Box::<serializers::Json>::default(),
},
{
let mut options = Options::default();
options.root = cli.output.unwrap_or_else(|| PathBuf::from("icu4x_data"));
if cli.overwrite {
options.overwrite = OverwriteOption::RemoveAndReplace
}
options
},
)?
})?,
match cli.syntax {
Syntax::Bincode => driver.export(
&provider,
FilesystemExporter::try_new(serializers::Bincode::default(), options)?,
)?,
Syntax::Postcard => driver.export(
&provider,
FilesystemExporter::try_new(serializers::Postcard::default(), options)?,
)?,
Syntax::Json if cli.pretty => driver.export(
&provider,
FilesystemExporter::try_new(serializers::Json::pretty(), options)?,
)?,
Syntax::Json => driver.export(
&provider,
FilesystemExporter::try_new(serializers::Json::default(), options)?,
)?,
}
}
#[cfg(not(feature = "blob_exporter"))]
Format::Blob => {
eyre::bail!("Exporting to a BlobProvider requires the `blob_exporter` Cargo feature")
Expand Down
Loading