rustla/
rustla_options.rs

1/*!
2This submodule contains the type definition and implementation of ruSTLaOptions.
3
4Copyright © 2020 Santtu Söderholm
5*/
6
7/// A container for the flags and settings of the ruSTLa transpiler at a type level.
8/// The options include
9/// 1. the output stream (stdout or file), set with the `--to-stdout` and `--to-file` flags.
10/// 2. whether ruSTLa should surround its object code with the LaTeX `document` environment. Set with the `--full-doc` flag.
11/// 3. whether the `aplus.cls` file should be generated next to the source file with the `--aplus-cls` flag.
12#[allow(non_camel_case_types)]
13#[derive(Debug)]
14pub struct ruSTLaOptions {
15    /// Choose between standard output and a file next to the original one.
16    output_stream: OutputStream,
17
18    /// Whether to add "\(begin&&end){document}" to the output file.
19    print_full_document: bool,
20
21    /// A flag that specifies whether the A+ class file should be written next to the source file.
22    generate_class_file: bool
23}
24
25impl ruSTLaOptions {
26
27    /// The ruSTLaOptions constructor. Receives the command line arguments in a vector of strings,
28    /// and constructs the option object from them.
29    pub fn new(args: &Vec<String>) -> Self {
30        let mut arg_index = 0usize;
31        let args_len = args.len();
32
33        let mut options = Self {
34            output_stream: OutputStream::StdOut,
35            print_full_document: false,
36            generate_class_file: false
37        };
38
39        while arg_index < args_len {
40            let arg = if let Some(arg) = args.get(arg_index) {
41                arg
42            } else {
43                break;
44            };
45
46            match arg.as_str() {
47                "--to-stdout"   => options.output_stream = OutputStream::StdOut,
48                "--to-file"     => options.output_stream = OutputStream::File,
49                "--full-doc"    => options.print_full_document = true,
50                "--aplus-cls"   => options.generate_class_file = true,
51                _ => {}
52            }
53
54            arg_index += 1;
55        }
56
57        options
58    }
59
60    /// Returns a shared reference to the chosen output stream: `stdout` or `file`.
61    pub fn shared_out_stream(&self) -> &OutputStream {
62        &self.output_stream
63    }
64
65    /// Returns a copy of the boolean indicating whether the document is to be directly compilable with pdflatex or not.
66    pub fn is_full_document(&self) -> bool {
67        self.print_full_document
68    }
69
70    /// Returns a copy of the flag which determines whether a class file should be written next to the source file.
71    pub fn create_class_file (&self) -> bool {
72        self.generate_class_file
73    }
74}
75#[derive(Debug)]
76/// An enumeration of the different output streams of ruSTLa.
77/// These can be set with the `--to-stdout` and `--to-file` command line flags.
78pub enum OutputStream {
79    /// As the name implies, this sets has ruSTLa print the object code to its stdout.
80    StdOut,
81    /// With this set, ruSTLa will generate a file with the same stem as the source file,
82    /// next to the source file.
83    File,
84}