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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::{
    collections::HashMap,
    ffi::OsString,
    path::{Path, PathBuf},
};
use texlang::traits::*;
use texlang::*;
use texlang_common as common;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Component {
    #[cfg_attr(feature = "serde", serde(skip))]
    job_name: Option<PathBuf>,
    default_job_name: PathBuf,
    dump_format: i32,
    dump_validate: i32,
    #[cfg_attr(feature = "serde", serde(skip))]
    num_dumps: usize,
}

impl Default for Component {
    fn default() -> Self {
        Self {
            job_name: None,
            default_job_name: "jobname".into(),
            dump_format: 0,
            dump_validate: 0,
            num_dumps: 0,
        }
    }
}

impl Component {
    pub fn job_name(&self) -> &Path {
        match &self.job_name {
            None => &self.default_job_name,
            Some(job_name) => job_name,
        }
    }

    // TODO: this should be called after the first \input.
    // Probably should add a hook to the VM for this case.
    // TODO: maybe this should be bundled with \input
    /// Set the job name based on the provided file path.
    pub fn set_job_name(&mut self, file_path: &Path) {
        if let Some(file_stem) = file_path.file_stem() {
            self.job_name = Some(file_stem.into())
        }
    }
}

/// Get the `\jobname` primitive.
pub fn get_jobname<S: HasComponent<Component>>() -> command::BuiltIn<S> {
    command::BuiltIn::new_expansion(|token, input| {
        let job_name: String = input
            .state()
            .component()
            .job_name()
            .to_string_lossy()
            .into();
        input.push_string_tokens(token, &job_name);
        Ok(())
    })
}

/// Get the `\dump` primitive.
#[cfg(feature = "serde")]
pub fn get_dump<
    S: HasComponent<Component>
        + serde::Serialize
        + serde::de::DeserializeOwned
        + common::HasFileSystem,
>() -> command::BuiltIn<S> {
    command::BuiltIn::new_execution(dump_primitive_fn)
}

#[cfg(feature = "serde")]
fn dump_primitive_fn<
    S: HasComponent<Component>
        + serde::Serialize
        + serde::de::DeserializeOwned
        + common::HasFileSystem,
>(
    _: token::Token,
    input: &mut vm::ExecutionInput<S>,
) -> command::Result<()> {
    let component = input.state().component();
    let mut job_name: OsString = component.job_name().into();
    let num_dumps = component.num_dumps;
    if num_dumps > 0 {
        job_name.push(format!["-{}", num_dumps + 1]);
    }
    let mut output_file: PathBuf = job_name.into();
    output_file.set_extension(match component.dump_format {
        1 => "fmt.json",
        2 => "fmt.bincode",
        _ => "fmt",
    });

    // TODO: error handle all these serialization errors.
    let serialized = match component.dump_format {
        0 => rmp_serde::encode::to_vec(input.vm()).unwrap(),
        1 => serde_json::to_vec_pretty(input.vm()).unwrap(),
        2 => bincode::serde::encode_to_vec(input.vm(), bincode::config::standard()).unwrap(),
        i => {
            return Err(error::SimpleFailedPreconditionError::new(format![
                r"\dumpFormat has invalid value {i}",
            ])
            .with_note(r"\dumpFormat must be either 0 (message pack), 1 (json) or 2 (bincode)")
            .into())
        }
    };

    if component.dump_validate != 0 {
        let built_in_commands: HashMap<&str, command::BuiltIn<S>> = input
            .vm()
            .commands_map
            .built_in_commands()
            .iter()
            .map(|(cs_name, command)| {
                (
                    input.vm().cs_name_interner().resolve(*cs_name).unwrap(),
                    command.clone(),
                )
            })
            .collect();
        match component.dump_format {
            0 => {
                let mut deserializer = rmp_serde::decode::Deserializer::from_read_ref(&serialized);
                // TODO: this shouldn't panic if the deserialization fails!
                vm::VM::<S>::deserialize_with_built_in_commands(
                    &mut deserializer,
                    built_in_commands,
                )
                .unwrap();
            }
            1 => {
                let mut deserializer = serde_json::Deserializer::from_slice(&serialized);
                vm::VM::<S>::deserialize_with_built_in_commands(
                    &mut deserializer,
                    built_in_commands,
                )
                .unwrap();
            }
            2 => {
                let deserialized: Box<vm::serde::DeserializedVM<S>> =
                    bincode::serde::decode_from_slice(&serialized, bincode::config::standard())
                        .unwrap()
                        .0;
                vm::serde::finish_deserialization(deserialized, built_in_commands);
            }
            _ => unreachable!(),
        };
    }

    // TODO: error handle file write
    input
        .state()
        .file_system()
        .borrow_mut()
        .write_bytes(&output_file, &serialized)
        .unwrap();
    input.state_mut().component_mut().num_dumps += 1;
    Ok(())
}

pub fn get_dumpformat<S: HasComponent<Component>>() -> command::BuiltIn<S> {
    variable::Command::new_singleton(
        |state: &S, _| &state.component().dump_format,
        |state: &mut S, _| &mut state.component_mut().dump_format,
    )
    .into()
}

pub fn get_dumpvalidate<S: HasComponent<Component>>() -> command::BuiltIn<S> {
    variable::Command::new_singleton(
        |state: &S, _| &state.component().dump_validate,
        |state: &mut S, _| &mut state.component_mut().dump_validate,
    )
    .into()
}