diff options
| author | A Farzat <a@farzat.xyz> | 2026-03-06 13:00:51 +0300 |
|---|---|---|
| committer | A Farzat <a@farzat.xyz> | 2026-03-07 09:26:03 +0300 |
| commit | 8c1629670614f639df8a43d4730d025fe4dcdfb8 (patch) | |
| tree | 913846e615ea764648c9333e521b0b814c6fb1e9 | |
| parent | c98b360d611c37315c9c5330089a1d91dbb2021c (diff) | |
| download | oreilly-epub-8c1629670614f639df8a43d4730d025fe4dcdfb8.tar.gz oreilly-epub-8c1629670614f639df8a43d4730d025fe4dcdfb8.zip | |
Use xml! macro for cleaner xml handling
| -rw-r--r-- | Cargo.lock | 32 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/epub.rs | 18 |
3 files changed, 41 insertions, 10 deletions
@@ -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]] @@ -767,6 +767,15 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092" @@ -832,6 +841,26 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -855,6 +884,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "ogrim", "quick-xml", "relative-path", "reqwest", @@ -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#"<?xml version="1.0" encoding="UTF-8"?> -<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> - <rootfiles> - <rootfile full-path="{opf_full_path}" media-type="application/oebps-package+xml"/> - </rootfiles> -</container> -"# + let contents = xml!( + <?xml version="1.0" encoding="UTF-8"?> + <container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> + <rootfiles> + <rootfile full-path={opf_full_path} media-type="application/oebps-package+xml"/> + </rootfiles> + </container> ); // 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(()) } |
