summaryrefslogtreecommitdiff
path: root/lib/net/hippie/rust_connection.rb
blob: 7c37350239b26870129e9a86cab81f675e037ff4 (plain)
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
# frozen_string_literal: true

require_relative 'rust_backend'

module Net
  module Hippie
    # Rust-powered connection that mimics the Ruby Connection interface
    class RustConnection
      def initialize(scheme, host, port, options = {})
        @scheme = scheme
        @host = host
        @port = port
        @options = options
        
        # Create the Rust client (simplified version for now)
        @rust_client = Net::Hippie::RustClient.new
      end

      def run(request)
        url = build_url_for(request.path)
        headers = {} # Simplified for now
        body = request.body || ''
        method = extract_method(request)

        begin
          rust_response = @rust_client.public_send(method.downcase, url, headers, body)
          RustBackend::ResponseAdapter.new(rust_response)
        rescue => e
          # Map Rust errors to Ruby equivalents
          raise map_rust_error(e)
        end
      end

      def build_url_for(path)
        return path if path.start_with?('http')

        port_suffix = (@port == 80 && @scheme == 'http') || (@port == 443 && @scheme == 'https') ? '' : ":#{@port}"
        "#{@scheme}://#{@host}#{port_suffix}#{path}"
      end

      private

      def extract_headers(request)
        headers = {}
        request.each_header do |key, value|
          headers[key] = value
        end
        headers
      end

      def extract_method(request)
        request.class.name.split('::').last.sub('HTTP', '').downcase
      end

      def map_rust_error(error)
        case error.message
        when /Net::ReadTimeout/
          Net::ReadTimeout.new
        when /Net::OpenTimeout/
          Net::OpenTimeout.new
        when /Errno::ECONNREFUSED/
          Errno::ECONNREFUSED.new
        when /Errno::ECONNRESET/
          Errno::ECONNRESET.new
        when /timeout/i
          Net::ReadTimeout.new
        else
          error
        end
      end
    end
  end
end