Skip to content

Commit 65bdb3d

Browse files
Model enums as SymbolEnums
1 parent 5fc7dc5 commit 65bdb3d

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

ext/src/ruby_api/wasi_config.rs

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use super::root;
22
use crate::error;
33
use crate::helpers::OutputLimitedBuffer;
4+
use crate::ruby_api::convert::ToValType;
5+
use crate::{define_rb_intern, helpers::SymbolEnum};
6+
use lazy_static::lazy_static;
47
use magnus::{
58
class, function, gc::Marker, method, typed_data::Obj, value::Opaque, DataTypeFunctions, Error,
6-
Module, Object, RArray, RHash, RString, Ruby, Symbol, TryConvert, TypedData,
9+
IntoValue, Module, Object, RArray, RHash, RString, Ruby, Symbol, TryConvert, TypedData, Value,
710
};
811
use rb_sys::ruby_rarray_flags::RARRAY_EMBED_FLAG;
912
use std::cell::RefCell;
13+
use std::convert::TryFrom;
1014
use std::fs;
1115
use std::path::Path;
1216
use std::{fs::File, path::PathBuf};
@@ -15,6 +19,37 @@ use wasmtime_wasi::p2::{OutputFile, WasiCtx, WasiCtxBuilder};
1519
use wasmtime_wasi::preview1::WasiP1Ctx;
1620
use wasmtime_wasi::{DirPerms, FilePerms};
1721

22+
define_rb_intern!(
23+
READ => "read",
24+
WRITE => "write",
25+
MUTATE => "mutate",
26+
ALL => "all",
27+
);
28+
29+
lazy_static! {
30+
static ref FILE_PERMS_MAPPING: SymbolEnum<'static, FilePerms> = {
31+
let mapping = vec![
32+
(*READ, FilePerms::READ),
33+
(*WRITE, FilePerms::WRITE),
34+
(*ALL, FilePerms::all()),
35+
];
36+
37+
SymbolEnum::new(":file_perms", mapping)
38+
};
39+
static ref DIR_PERMS_MAPPING: SymbolEnum<'static, DirPerms> = {
40+
let mapping = vec![
41+
(*READ, DirPerms::READ),
42+
(*MUTATE, DirPerms::MUTATE),
43+
(*ALL, DirPerms::all()),
44+
];
45+
46+
SymbolEnum::new(":dir_perms", mapping)
47+
};
48+
}
49+
50+
struct PermsSymbolEnum(Symbol);
51+
52+
/// A Rust struct to represent a mapped directory configuration
1853
#[derive(Clone)]
1954
struct MappedDirectory {
2055
host_path: String,
@@ -85,6 +120,20 @@ impl WasiConfigInner {
85120
}
86121
}
87122

123+
impl TryFrom<PermsSymbolEnum> for DirPerms {
124+
type Error = magnus::Error;
125+
fn try_from(value: PermsSymbolEnum) -> Result<Self, Error> {
126+
DIR_PERMS_MAPPING.get(value.0.into_value())
127+
}
128+
}
129+
130+
impl TryFrom<PermsSymbolEnum> for FilePerms {
131+
type Error = magnus::Error;
132+
fn try_from(value: PermsSymbolEnum) -> Result<Self, Error> {
133+
FILE_PERMS_MAPPING.get(value.0.into_value())
134+
}
135+
}
136+
88137
/// @yard
89138
/// WASI config to be sent as {Store#new}’s +wasi_config+ keyword argument.
90139
///
@@ -262,32 +311,8 @@ impl WasiConfig {
262311
) -> Result<RbSelf, Error> {
263312
let host_path = host_path.to_string().unwrap();
264313
let guest_path = guest_path.to_string().unwrap();
265-
let dir_perms_str = dir_perms.name().unwrap().to_string();
266-
let file_perms_str = file_perms.name().unwrap().to_string();
267-
268-
let dir_perms = match dir_perms_str.as_str() {
269-
"read" => DirPerms::READ,
270-
"mutate" => DirPerms::MUTATE,
271-
"all" => DirPerms::all(),
272-
_ => {
273-
return Err(error!(
274-
"Invalid dir_perms: {}. Use one of :read, :mutate, or :all",
275-
dir_perms_str
276-
))
277-
}
278-
};
279-
280-
let file_perms = match file_perms_str.as_str() {
281-
"read" => FilePerms::READ,
282-
"write" => FilePerms::WRITE,
283-
"all" => FilePerms::all(),
284-
_ => {
285-
return Err(error!(
286-
"Invalid file_perms: {}. Use one of :read, :write, or :all",
287-
file_perms_str
288-
))
289-
}
290-
};
314+
let dir_perms: DirPerms = PermsSymbolEnum(dir_perms).try_into()?;
315+
let file_perms: FilePerms = PermsSymbolEnum(file_perms).try_into()?;
291316

292317
let mapped_dir = MappedDirectory {
293318
host_path,

spec/unit/wasi_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ module Wasmtime
320320
WasiConfig.new
321321
.set_mapped_directory(tempfile_path("tmp"), "/tmp", :mutate, :invalid_permission)
322322
}.to raise_error do |error|
323-
expect(error).to be_a(Wasmtime::Error)
324-
expect(error.message).to match(/Invalid file_perms: invalid_permission. Use one of :read, :write, or :all/)
323+
expect(error).to be_a(ArgumentError)
324+
expect(error.message).to match(/invalid :file_perms, expected one of \[:read, :write, :all\], got :invalid_permission/)
325325
end
326326
end
327327
end

0 commit comments

Comments
 (0)