From 8c1629670614f639df8a43d4730d025fe4dcdfb8 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Fri, 6 Mar 2026 13:00:51 +0300 Subject: Use xml! macro for cleaner xml handling --- Cargo.lock | 32 +++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/epub.rs | 18 +++++++++--------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e403d0..d56d0fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,7 +301,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61" dependencies = [ - "litrs", + "litrs 1.0.0", ] [[package]] @@ -765,6 +765,15 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" +[[package]] +name = "litrs" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5e54036fe321fd421e10d732f155734c4e4afd610dd556d9a82833ab3ee0bed" +dependencies = [ + "proc-macro2", +] + [[package]] name = "litrs" version = "1.0.0" @@ -831,6 +840,26 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf97ec579c3c42f953ef76dbf8d55ac91fb219dde70e49aa4a6b7d74e9919050" +[[package]] +name = "ogrim" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9a9f13f20f041c757ade45879cb9c8a2f7fa069f7f106c1683436ace5f975b4" +dependencies = [ + "ogrim-macros", +] + +[[package]] +name = "ogrim-macros" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c178ca94f950c8be8f915ebe59c797e2bd855e21e848d4afe6caf0115f3903" +dependencies = [ + "litrs 0.4.2", + "proc-macro2", + "quote", +] + [[package]] name = "once_cell" version = "1.21.3" @@ -855,6 +884,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "ogrim", "quick-xml", "relative-path", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 50927f1..970f928 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2024" anyhow = "1.0.102" clap = { version = "4.5.60", features = ["derive"] } quick-xml = "0.39.2" +ogrim = "0.1.1" relative-path = { version = "2.0.1", features = ["serde"] } reqwest = { version = "0.13.2", features = ["cookies", "json"] } serde = { version = "1.0.228", features = ["derive"] } diff --git a/src/epub.rs b/src/epub.rs index 5383251..1d4676d 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -3,6 +3,7 @@ use crate::{ xml::build_epub_chapter, }; use anyhow::{Context, Result}; +use ogrim::xml; use relative_path::{RelativePath, RelativePathBuf}; use reqwest::Client; use std::{ @@ -22,21 +23,20 @@ fn write_container_xml_to_zip( opf_full_path: &RelativePathBuf, ) -> Result<()> { // Prepare file contents. - let contents = format!( - r#" - - - - - -"# + let contents = xml!( + + + + + + ); // Write down the file. let options: FileOptions<()> = FileOptions::default().compression_method(CompressionMethod::Deflated); zip.start_file("META-INF/container.xml", options)?; - zip.write_all(contents.as_bytes())?; + zip.write_all(contents.as_str().as_bytes())?; Ok(()) } -- cgit v1.2.3-70-g09d2