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
use mruby::def::EnclosingRubyScope; use mruby::file::MrbFile; use mruby::load::MrbLoadSources; use mruby::Mrb; use mruby::MrbError; use mruby_gems::Gem; use std::borrow::Cow; use std::convert::AsRef; pub fn init(interp: &Mrb) -> Result<(), MrbError> { Nemesis::init(interp) } #[derive(RustEmbed)] #[folder = "$CARGO_MANIFEST_DIR/ruby/lib"] struct Nemesis; impl Nemesis { fn contents<T: AsRef<str>>(path: T) -> Result<Vec<u8>, MrbError> { let path = path.as_ref(); Self::get(path) .map(Cow::into_owned) .ok_or_else(|| MrbError::SourceNotFound(path.to_owned())) } } impl MrbFile for Nemesis { fn require(interp: Mrb) -> Result<(), MrbError> { interp.borrow_mut().def_module::<Self>("Nemesis", None); Ok(()) } } impl Gem for Nemesis { fn init(interp: &Mrb) -> Result<(), MrbError> { for source in Self::iter() { let contents = Self::contents(&source)?; interp.def_rb_source_file(source, contents)?; } interp.def_file_for_type::<_, Self>("nemesis.rb")?; interp.def_file_for_type::<_, Response>("nemesis/response.rb")?; Ok(()) } } pub struct Response; impl MrbFile for Response { fn require(interp: Mrb) -> Result<(), MrbError> { let scope = interp .borrow() .module_spec::<Nemesis>() .map(EnclosingRubyScope::module) .ok_or_else(|| MrbError::NotDefined("Nemesis".to_owned()))?; interp .borrow_mut() .def_class::<Self>("Response", Some(scope), None); Ok(()) } }