1use std::ffi::OsStr;
5use std::fmt::Display;
6
7use serde_json::Value;
8
9use crate::args::{Book, Books, join};
10use crate::client::Postext;
11use crate::command::{Command, Kind, OutMode, ReadsBook, TakesFonts, WritesFile, sealed::Sealed};
12use crate::report::{
13 CheckReport, DesignReport, ImageReport, ImportDocxReport, ImportIdmlReport, InfoReport, Report, UnpackReport,
14};
15
16pub mod kind {
18 macro_rules! kinds {
19 ($($(#[$m:meta])* $name:ident;)*) => {$(
20 $(#[$m])*
21 #[derive(Debug, Clone, Copy)]
22 pub struct $name;
23 )*};
24 }
25
26 kinds! {
27 Pdf;
29 Html;
31 Epub;
33 Image;
35 Images;
37 Docx;
39 Pack;
41 Design;
43 Idml;
45 Info;
47 Check;
49 Build;
51 ImportDocx;
53 Unpack;
55 ImportIdml;
57 }
58}
59
60macro_rules! kind {
61 ($name:ident, $command:literal, $report:ty, $out:ident $(, input = $input:literal)? $(, font_defaults = $fd:literal)?; $($marker:ident),*) => {
62 impl Sealed for kind::$name {}
63 impl Kind for kind::$name {
64 const COMMAND: &'static str = $command;
65 $(const INPUT: &'static str = $input;)?
66 const OUT: OutMode = OutMode::$out;
67 $(const FONT_DEFAULTS: bool = $fd;)?
68 type Report = $report;
69 }
70 $(impl $marker for kind::$name {})*
71 };
72}
73
74kind!(Pdf, "pdf", Report, File; ReadsBook, TakesFonts, WritesFile);
75kind!(Html, "html", Report, File; ReadsBook, TakesFonts, WritesFile);
76kind!(Epub, "epub", Report, File; ReadsBook, TakesFonts, WritesFile);
77kind!(Image, "image", ImageReport, File; ReadsBook, TakesFonts, WritesFile);
78kind!(Images, "images", Report, Required; ReadsBook, TakesFonts);
79kind!(Docx, "docx", Report, File; ReadsBook, TakesFonts, WritesFile);
80kind!(Pack, "pack", Report, File; ReadsBook, TakesFonts, WritesFile);
81kind!(Design, "design", DesignReport, File; ReadsBook, TakesFonts, WritesFile);
82kind!(Idml, "idml", Report, File; ReadsBook, WritesFile);
83kind!(Info, "info", InfoReport, Never; ReadsBook, TakesFonts);
84kind!(Check, "check", CheckReport, Never; ReadsBook, TakesFonts);
85kind!(Build, "build", Report, Never; ReadsBook, TakesFonts);
86kind!(ImportDocx, "import-docx", ImportDocxReport, Required, input = "book.docx", font_defaults = false;);
87kind!(Unpack, "unpack", UnpackReport, Required, font_defaults = false;);
88kind!(ImportIdml, "import-idml", ImportIdmlReport, Required, input = "document.idml";);
89
90macro_rules! options {
91 ($kind:ident { $($(#[$m:meta])* $fn:ident: $how:ident $flag:literal;)* }) => {
92 impl Command<'_, kind::$kind> {
93 $(options!(@one $(#[$m])* $fn $how $flag);)*
94 }
95 };
96 (@one $(#[$m:meta])* $fn:ident text $flag:literal) => {
97 $(#[$m])*
98 pub fn $fn(self, value: impl AsRef<OsStr>) -> Self {
99 self.opt($flag, value)
100 }
101 };
102 (@one $(#[$m:meta])* $fn:ident switch $flag:literal) => {
103 $(#[$m])*
104 pub fn $fn(self, on: bool) -> Self {
105 self.flag($flag, on)
106 }
107 };
108 (@one $(#[$m:meta])* $fn:ident count $flag:literal) => {
109 $(#[$m])*
110 pub fn $fn(self, value: u32) -> Self {
111 self.opt($flag, value.to_string())
112 }
113 };
114 (@one $(#[$m:meta])* $fn:ident real $flag:literal) => {
115 $(#[$m])*
116 pub fn $fn(self, value: impl Into<f64>) -> Self {
117 self.opt($flag, value.into().to_string())
118 }
119 };
120}
121
122options!(Pdf {
123 accessible: switch "accessible";
125 color_space: text "color-space";
127 pdfx: text "pdfx";
129 profile: text "profile";
131 outlines: switch "outlines";
133 page_negative: switch "page-negative";
135});
136
137options!(Html {
138 mode: text "mode";
140 assets: text "assets";
142});
143
144options!(Epub {
145 layout: text "layout";
147 cover: text "cover";
149});
150
151options!(Image {
152 format: text "format";
154 dpi: real "dpi";
156 quality: count "quality";
158 print_preview: switch "print-preview";
160});
161
162options!(Images {
163 pages: text "pages";
165 pattern: text "pattern";
167 jobs: count "jobs";
169 format: text "format";
171 dpi: real "dpi";
173 quality: count "quality";
175 print_preview: switch "print-preview";
177});
178
179options!(Docx {
180 variant: text "variant";
183 embed_fonts: switch "embed-fonts";
185});
186
187options!(Pack {
188 id: text "id";
190 description: text "description";
192 thumbnail: text "thumbnail";
194 auto_thumbnail: switch "auto-thumbnail";
196});
197
198options!(Design {
199 design_name: text "design-name";
201 id: text "id";
203 description: text "description";
205});
206
207options!(Idml {
208 images: text "images";
210 lines: text "lines";
213 spreads: text "spreads";
215 fonts: switch "fonts";
218});
219
220options!(Info {
221 pages: switch "pages";
223});
224
225options!(Check {
226 strict: switch "strict";
228 preflight: switch "preflight";
230});
231
232options!(Build {
233 pdf: text "pdf";
235 html: text "html";
237 epub: text "epub";
239 images: text "images";
241 docx: text "docx";
243 docx_variant: text "docx-variant";
245 docx_fonts: switch "docx-fonts";
247 idml: text "idml";
249 idml_images: text "idml-images";
251 idml_lines: text "idml-lines";
253 idml_spreads: text "idml-spreads";
255 idml_fonts: switch "idml-fonts";
257 pages: text "pages";
259 pattern: text "pattern";
261 jobs: count "jobs";
263 accessible: switch "accessible";
265 color_space: text "color-space";
267 pdfx: text "pdfx";
269 profile: text "profile";
271 outlines: switch "outlines";
273 page_negative: switch "page-negative";
275 mode: text "mode";
277 assets: text "assets";
279 layout: text "layout";
281 cover: text "cover";
283 format: text "format";
285 dpi: real "dpi";
287 quality: count "quality";
289 print_preview: switch "print-preview";
291});
292
293options!(ImportDocx {
294 dry_run: switch "dry-run";
296 resources: text "resources";
298 template: text "template";
300 split: switch "split";
302 locale: text "locale";
304 config: text "config";
306 name: text "name";
308 quality_report: switch "report";
310 auto_thumbnail: switch "auto-thumbnail";
312});
313
314options!(Unpack {
315 force: switch "force";
317});
318
319options!(ImportIdml {
320 document: text "document";
322 layout: text "layout";
325 links: text "links";
327 name: text "name";
329 locale: text "locale";
331 hidden: switch "hidden";
333 pasteboard: switch "pasteboard";
335 non_printing: switch "non-printing";
337 conditions: text "conditions";
339 changes: text "changes";
341 masters: switch "masters";
343 quality_report: switch "report";
345 auto_thumbnail: switch "auto-thumbnail";
347 cache_dir: text "cache-dir";
349});
350
351impl Command<'_, kind::Design> {
352 pub fn parts<I, S>(self, parts: I) -> Self
354 where
355 I: IntoIterator<Item = S>,
356 S: AsRef<str>,
357 {
358 self.opt("parts", join(parts))
359 }
360}
361
362impl Command<'_, kind::Check> {
363 pub fn allow_problems(self) -> Self {
367 self.problems_allowed()
368 }
369}
370
371impl Command<'_, kind::ImportDocx> {
372 pub fn into_book(self, book: impl AsRef<OsStr>) -> Self {
375 self.input(book)
376 }
377
378 pub fn config_json(self, config: &Value) -> Self {
380 self.json_opt("config", config, "config.json")
381 }
382
383 pub fn allow_problems(self) -> Self {
386 self.problems_allowed()
387 }
388}
389
390impl Command<'_, kind::ImportIdml> {
391 pub fn font_dir(self, dir: impl AsRef<OsStr>) -> Self {
395 self.add_font_dir(dir)
396 }
397
398 pub fn offline(self, offline: bool) -> Self {
400 self.set_offline(offline)
401 }
402}
403
404impl Postext {
405 pub fn pdf(&self, book: impl Into<Books>) -> Command<'_, kind::Pdf> {
407 Command::new(self, book.into())
408 }
409
410 pub fn html(&self, book: impl Into<Books>) -> Command<'_, kind::Html> {
413 Command::new(self, book.into())
414 }
415
416 pub fn epub(&self, book: impl Into<Books>) -> Command<'_, kind::Epub> {
418 Command::new(self, book.into())
419 }
420
421 pub fn image(&self, book: impl Into<Books>, page: impl Display) -> Command<'_, kind::Image> {
424 Command::new(self, book.into()).opt("page", page.to_string())
425 }
426
427 pub fn images(&self, book: impl Into<Books>) -> Command<'_, kind::Images> {
430 Command::new(self, book.into())
431 }
432
433 pub fn docx(&self, book: impl Into<Books>) -> Command<'_, kind::Docx> {
435 Command::new(self, book.into())
436 }
437
438 pub fn pack(&self, book: impl Into<Books>) -> Command<'_, kind::Pack> {
441 Command::new(self, book.into())
442 }
443
444 pub fn design(&self, book: impl Into<Books>) -> Command<'_, kind::Design> {
449 Command::new(self, book.into())
450 }
451
452 pub fn idml(&self, book: impl Into<Books>) -> Command<'_, kind::Idml> {
454 Command::new(self, book.into())
455 }
456
457 pub fn info(&self, book: impl Into<Books>) -> Command<'_, kind::Info> {
459 Command::new(self, book.into())
460 }
461
462 pub fn check(&self, book: impl Into<Books>) -> Command<'_, kind::Check> {
467 Command::new(self, book.into())
468 }
469
470 pub fn build(&self, book: impl Into<Books>) -> Command<'_, kind::Build> {
473 Command::new(self, book.into())
474 }
475
476 pub fn import_docx(&self, docx: impl Into<Book>) -> Command<'_, kind::ImportDocx> {
481 Command::new(self, Books(vec![docx.into()]))
482 }
483
484 pub fn unpack(&self, bundle: impl Into<Book>) -> Command<'_, kind::Unpack> {
486 Command::new(self, Books(vec![bundle.into()]))
487 }
488
489 pub fn import_idml(&self, idml: impl Into<Book>) -> Command<'_, kind::ImportIdml> {
494 Command::new(self, Books(vec![idml.into()]))
495 }
496}