1#![feature(once_cell_try)]
2#![doc = include_str!("../readme.md")]
3
4pub mod formats;
5pub mod helpers;
6pub mod program;
7
8#[cfg(feature = "wat")]
10pub use formats::wat;
11use gaia_types::GaiaError;
12pub use program::{
13 WasiAlias, WasiAliasTarget, WasiCanonicalOperation, WasiComponentItem, WasiCoreFunc, WasiCoreInstance, WasiCoreModule,
14 WasiCoreType, WasiCustomSection, WasiDataSegment, WasiElementSegment, WasiExport, WasiFunction, WasiFunctionType,
15 WasiGlobal, WasiImport, WasiInstance, WasiInstanceArg, WasiInstanceType, WasiInstruction, WasiMemory, WasiPrimitiveType,
16 WasiProgram, WasiProgramBuilder, WasiProgramType, WasiRecordField, WasiResourceMethod, WasiResourceType, WasiSymbol,
17 WasiSymbolType, WasiTable, WasiType, WasiTypeDefinition, WasiVariantCase, WasmExportType, WasmGlobalType, WasmImportType,
18 WasmInfo, WasmLocal, WasmMemoryType, WasmReferenceType, WasmTableType, WasmValueType,
19};
20
21#[derive(Debug)]
23pub enum WasiError {
24 ParseError(String),
26 ValidationError(String),
28 CompilationError(String),
30 Other(String),
32}
33
34impl std::fmt::Display for WasiError {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 match self {
37 WasiError::ParseError(msg) => write!(f, "Parse error: {}", msg),
38 WasiError::ValidationError(msg) => write!(f, "Validation error: {}", msg),
39 WasiError::CompilationError(msg) => write!(f, "Compilation error: {}", msg),
40 WasiError::Other(msg) => write!(f, "Other error: {}", msg),
41 }
42 }
43}
44
45impl std::error::Error for WasiError {}
46
47impl From<GaiaError> for WasiError {
48 fn from(error: GaiaError) -> Self {
49 WasiError::Other(error.to_string())
50 }
51}
52
53pub struct WasiAssembler {
55 target: String,
56}
57
58impl WasiAssembler {
59 pub fn new() -> Self {
61 Self { target: "wasm32-wasi".to_string() }
62 }
63
64 pub fn set_target(&mut self, target: &str) {
66 self.target = target.to_string();
67 }
68
69 pub fn assemble_from_str(&self, _source: &str) -> std::result::Result<Vec<u8>, WasiError> {
71 Ok(vec![0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00])
73 }
74
75 pub fn assemble_from_file(&self, path: impl AsRef<std::path::Path>) -> std::result::Result<Vec<u8>, WasiError> {
77 let source = std::fs::read_to_string(path).map_err(|e| WasiError::Other(e.to_string()))?;
78 self.assemble_from_str(&source)
79 }
80}
81
82impl Default for WasiAssembler {
83 fn default() -> Self {
84 Self::new()
85 }
86}