summaryrefslogtreecommitdiff
path: root/src/bin/main.rs
blob: b678cda9e3cf6a3dad61830b391590dbd8e95e4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::env;
use std::error::Error;
use std::fs::File;
use std::io::{Read, BufWriter};
use std::path::{Path, PathBuf};
use std::process;

use typst::Typesetter;
use typst::export::pdf::PdfExporter;
use typst::toddle::query::FileSystemFontProvider;


fn main() {
    if let Err(err) = run() {
        eprintln!("error: {}", err);
        process::exit(1);
    }
}

/// The actual main function.
fn run() -> Result<(), Box<dyn Error>> {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 || args.len() > 3 {
        help_and_quit();
    }

    let source_path = Path::new(&args[1]);

    // Compute the output filename from the input filename by replacing the extension.
    let dest_path = if args.len() <= 2 {
        let stem = source_path.file_stem().ok_or_else(|| "missing destation file name")?;
        let base = source_path.parent().ok_or_else(|| "missing destation folder")?;
        base.join(format!("{}.pdf", stem.to_string_lossy()))
    } else {
        PathBuf::from(&args[2])
    };

    if dest_path == source_path {
        return Err("source and destination path are the same".into());
    }

    let mut src = String::new();
    let mut source_file = File::open(source_path).map_err(|_| "failed to open source file")?;
    source_file.read_to_string(&mut src).map_err(|_| "failed to read from source file")?;

    // Create a typesetter with a font provider that provides the default fonts.
    let mut typesetter = Typesetter::new();
    let provider = FileSystemFontProvider::from_listing("fonts/fonts.toml").unwrap();
    typesetter.add_font_provider(provider);

    // Typeset the source code.
    let document = typesetter.typeset(&src)?;

    // Export the document into a PDF file.
    let exporter = PdfExporter::new();
    let dest_file = File::create(&dest_path)?;
    exporter.export(&document, typesetter.loader(), BufWriter::new(dest_file))?;

    Ok(())
}

/// Print a usage message and quit.
fn help_and_quit() {
    let name = env::args().next().unwrap_or("typst".to_string());
    println!("usage: {} source [destination]", name);
    process::exit(0);
}