factorio_exporter/lib.rs
1//! A library and binary for exporting object prototype definitions from
2//! [Factorio](https://www.factorio.com) in JSON or YAML.
3//!
4//! Usage:
5//! ```no_run
6//! use factorio_exporter::{ load_api, FactorioExporter, FactorioExporterError, Result };
7//! use std::path::PathBuf;
8//!
9//! let api_spec = PathBuf::from("/home/user/factorio/doc-html/runtime-api.json");
10//! let factorio_binary = PathBuf::from("/home/user/factorio/bin/x64/factorio");
11//!
12//! let api = load_api(&api_spec)?;
13//! let exporter = FactorioExporter::new(&factorio_binary, &api, "en", true)?;
14//!
15//! let result: serde_yaml::Value = exporter.export()?;
16//!
17//! # Ok::<(), FactorioExporterError>(())
18//! ```
19//!
20//! The result is returned as a [`serde_yaml::Value`] object, which can easily
21//! deserialized of serialized into other data types further. See this [example]
22//! to see the structure that the data has.
23//!
24//! [example]:
25//! https://raw.githubusercontent.com/MForster/factorio-rust-tools/main/crates/factorio-exporter/data/vanilla.json
26#![deny(unused_must_use)]
27use std::path::PathBuf;
28
29use thiserror::Error;
30
31pub use api::load_api;
32pub use exporter::FactorioExporter;
33
34mod api;
35mod exporter;
36mod internal;
37
38/// Main result type used throughout factorio-explorer
39pub type Result<T> = std::result::Result<T, FactorioExporterError>;
40
41/// Main error type used throughout factorio-explorer
42#[derive(Error, Debug)]
43pub enum FactorioExporterError {
44 /// Error that is raised if Factorio could not be started to execute the
45 /// exporter mods. The process output to stdout and stderr is saved in the
46 /// error object.
47 #[error("error while executing Factorio")]
48 FactorioExecutionError { stdout: String, stderr: String },
49
50 /// Error that is raised if Factorio's output could not be parsed. This can
51 /// have all kinds of root causes, but the underlying reason should normally
52 /// be apparent from the process output stored in this error object.
53 #[error("failed to parse Factorio output")]
54 FactorioOutputError { message: String, output: String },
55
56 /// Error that is raised if a file couldn't be found, for example the API
57 /// spec or the Factorio binary. This is usually a user error.
58 #[error("{file} does not exist or isn't a file")]
59 FileNotFoundError { file: PathBuf },
60
61 /// Error that is raised if the user specified conflicting or incomplete
62 /// command line arguments.
63 #[error("{0}")]
64 InvocationError(String),
65
66 /// Error that is raised if a file system operation failed unexpectedly.
67 #[error("I/O error")]
68 IoError(#[from] std::io::Error),
69
70 /// Error that is raised if deserialization from JSON failed.
71 #[error("failed to parse JSON")]
72 JsonParsingError(#[from] serde_json::Error),
73
74 /// Error that is raised if deserialization from JSON failed.
75 #[error("failed to parse YAML")]
76 YamlParsingError(#[from] serde_yaml::Error),
77}