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
use mruby::load::MrbLoadSources;
use mruby::Mrb;
use mruby::MrbError;
use std::borrow::Cow;
use std::convert::AsRef;

use crate::Gem;

/// Load the [`Mustermann`] gem into an interpreter.
pub fn init(interp: &Mrb) -> Result<(), MrbError> {
    Mustermann::init(interp)
}

/// Gem
#[derive(RustEmbed)]
#[folder = "$CARGO_MANIFEST_DIR/vendor/ruby/2.6.0/gems/mustermann-1.0.3/lib"]
struct Mustermann;

impl Mustermann {
    fn contents<T: AsRef<str>>(path: T) -> Result<Vec<u8>, MrbError> {
        let path = path.as_ref();
        let contents = Self::get(path)
            .map(Cow::into_owned)
            .ok_or_else(|| MrbError::SourceNotFound(path.to_owned()))?;
        // patches
        if path == "mustermann.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("defined?(Pry) or defined?(IRB)", "false");
            string = string.replace(
                "defined? ::Sinatra::Base",
                "Object.const_defined?(:Sinatra)",
            );
            Ok(string.into_bytes())
        } else if path == "mustermann/error.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace(
                "defined?(Mustermann::Error)",
                "Mustermann.const_defined?(:Error)",
            );
            Ok(string.into_bytes())
        } else if path == "mustermann/pattern.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("private_constant", "# private_constant");
            Ok(string.into_bytes())
        } else if path == "mustermann/ast/boundaries.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace(
                "def self.set_boundaries(ast, string: nil, start: 0, stop: string.length)",
                "def self.set_boundaries(ast, string: nil, start: 0, stop: nil); stop = string&.length if stop.nil?"
            );
            Ok(string.into_bytes())
        } else if path == "mustermann/ast/compiler.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("private_constant", "# private_constant");
            Ok(string.into_bytes())
        } else if path == "mustermann/ast/node.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace(
                "Object.const_get(const_name) if Object.const_defined?(const_name)",
                "Object.const_get(const_name) rescue NameError nil",
            );
            Ok(string.into_bytes())
        } else if path == "mustermann/ast/translator.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace(
                "Class.new(self, &block).new",
                "translator = Class.new(self); translator.class_eval(&block); translator.new",
            );
            string = string.replace("private_constant", "# private_constant");
            string = string.replace(
                "def escape(char, parser: URI::DEFAULT_PARSER, escape: parser.regexp[:UNSAFE], also_escape: nil)",
                "def escape(char, parser: URI::DEFAULT_PARSER, escape: NOT_SET, also_escape: nil); escape = parser.regexp[:UNSAFE] if escape.not_set?",
            );
            Ok(string.into_bytes())
        } else if path == "mustermann/equality_map.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("defined?(ObjectSpace::WeakMap)", "false");
            Ok(string.into_bytes())
        } else if path == "mustermann/regexp_based.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("super", "super(string, **options)");
            Ok(string.into_bytes())
        } else if path == "mustermann/regular.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace(
                r#"string = $1 if string.to_s =~ /\A\(\?\-mix\:(.*)\)\Z/ && string.inspect == "/#$1/""#,
                r#"match = /\A\(\?\-mix\:(.*)\)\Z/.match(string.to_s); string = match[1] if match && string.inspect == "/#{match[1]}/""#
            );
            Ok(string.into_bytes())
        } else if path == "mustermann/sinatra/parser.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("private_constant", "# private_constant");
            Ok(string.into_bytes())
        } else if path == "mustermann/sinatra/safe_renderer.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("private_constant", "# private_constant");
            Ok(string.into_bytes())
        } else if path == "mustermann/sinatra/try_convert.rb" {
            let mut string = String::from_utf8(contents)
                .map_err(|_| MrbError::SourceNotFound(path.to_owned()))?;
            string = string.replace("private_constant", "# private_constant");
            Ok(string.into_bytes())
        } else {
            Ok(contents)
        }
    }
}

impl Gem for Mustermann {
    fn init(interp: &Mrb) -> Result<(), MrbError> {
        for source in Self::iter() {
            let contents = Self::contents(&source)?;
            interp.def_rb_source_file(source, contents)?;
        }
        Ok(())
    }
}