Skip to content

Commit

Permalink
add write_sort_by() and write_to_file_sort_by()
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSaya committed Nov 5, 2024
1 parent 39f9b4e commit 7c2c7fc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/po_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub use po_file_parser::{
parse, parse_from_reader, parse_from_reader_with_option, parse_with_option,
};
pub use po_file_parser::{POParseError, POParseOptions};
pub use po_file_writer::{write, write_to_file};
pub use po_file_writer::{write, write_to_file, write_sort_by, write_to_file_sort_by};
48 changes: 44 additions & 4 deletions src/po_file/po_file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use super::escape::escape;
use crate::catalog::Catalog;
use crate::message::MessageView;
use std::cmp::Ordering;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;
Expand Down Expand Up @@ -99,12 +101,24 @@ fn write_field<W: Write>(
Ok(())
}

/// Writes a catalog in PO format.
pub fn write<W: Write>(catalog: &Catalog, writer: &mut BufWriter<W>) -> Result<(), std::io::Error> {
fn write_internal<W: Write>(
catalog: &Catalog,
writer: &mut BufWriter<W>,
comparator: Option<Box<dyn FnMut(&&dyn MessageView, &&dyn MessageView) -> Ordering>>,
) -> Result<(), std::io::Error> {
writer.write_all(b"\nmsgid \"\"\n")?;
write_field(writer, "msgstr", catalog.metadata.export_for_po().as_str())?;
writer.write_all(b"\n")?;
for message in catalog.messages() {

let messages = if let Some(comparator) = comparator {
let mut sorting = catalog.messages().collect::<Vec<&dyn MessageView>>();
sorting.sort_by(comparator);
sorting
} else {
catalog.messages().collect::<Vec<&dyn MessageView>>()
};

for message in messages {
if !message.comments().is_empty() {
for line in message.comments().split('\n') {
writer.write_all(b"#. ")?;
Expand Down Expand Up @@ -144,9 +158,35 @@ pub fn write<W: Write>(catalog: &Catalog, writer: &mut BufWriter<W>) -> Result<(
Ok(())
}

/// Writes a catalog in PO format.
pub fn write<W: Write>(catalog: &Catalog, writer: &mut BufWriter<W>) -> Result<(), std::io::Error> {
write_internal(catalog, writer, None)
}

/// Writes a catalog to a PO file on disk.
pub fn write_to_file(catalog: &Catalog, path: &Path) -> Result<(), std::io::Error> {
let file = File::create(path)?;
let mut writer = BufWriter::new(file);
write(catalog, &mut writer)
write_internal(catalog, &mut writer, None)
}

/// Writes a catalog in PO format with a sorting algorithm.
pub fn write_sort_by<W: Write>(
catalog: &Catalog,
writer: &mut BufWriter<W>,
comparator: Box<dyn FnMut(&&dyn MessageView, &&dyn MessageView) -> Ordering>,
) -> Result<(), std::io::Error> {
write_internal(catalog, writer, Some(comparator))
}


/// Writes a catalog to a PO file on disk with a sorting algorithm.
pub fn write_to_file_sort_by(
catalog: &Catalog,
path: &Path,
comparator: Box<dyn FnMut(&&dyn MessageView, &&dyn MessageView) -> Ordering>,
) -> Result<(), std::io::Error> {
let file = File::create(path)?;
let mut writer = BufWriter::new(file);
write_internal(catalog, &mut writer, Some(comparator))
}
12 changes: 12 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ fn po_round_trip() {
let catalog_2 = po_file::parse_from_reader(&*po_bytes).unwrap();
validate_catalog(&catalog_2);
}

#[test]
fn po_round_trip_sort() {
let catalog = po_file::parse_from_reader(&*feed_test_po()).unwrap();
let mut writer = std::io::BufWriter::new(Vec::new());
po_file::write_sort_by(&catalog, &mut writer, Box::new(|a, b| {
a.source().cmp(b.source())
})).unwrap();
let po_bytes = writer.into_inner().unwrap();
let catalog_2 = po_file::parse_from_reader(&*po_bytes).unwrap();
validate_catalog(&catalog_2);
}

0 comments on commit 7c2c7fc

Please sign in to comment.