summaryrefslogtreecommitdiff
path: root/lib/net/hippie/rust_backend.rb
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-06 13:54:36 -0600
committermo khan <mo@mokhan.ca>2025-07-06 13:54:36 -0600
commitd36b6e4f7c99b96aee01656e2ca57312d77a55b6 (patch)
treee875bc14c907cbca92a8c36d9f15d4c946fceed0 /lib/net/hippie/rust_backend.rb
parent6ef050083b8519cfb8120246344514e1c8e27f49 (diff)
feat: add optional Rust backend with Magnus integration
- Add Rust HTTP client using reqwest and Magnus for Ruby integration - Implement transparent backend switching via NET_HIPPIE_RUST environment variable - Maintain 100% backward compatibility with existing Ruby interface - Add comprehensive test coverage (75 tests, 177 assertions) - Support automatic fallback to Ruby backend when Rust unavailable - Include detailed documentation for Rust backend setup and usage - Add proper .gitignore for Rust build artifacts - Update gemspec to support native extensions Performance benefits: - Faster HTTP requests using Rust's optimized reqwest library - Better concurrency with Tokio async runtime - Lower memory usage with zero-cost abstractions - Type safety with compile-time guarantees 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'lib/net/hippie/rust_backend.rb')
-rw-r--r--lib/net/hippie/rust_backend.rb72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/net/hippie/rust_backend.rb b/lib/net/hippie/rust_backend.rb
new file mode 100644
index 0000000..7d1637e
--- /dev/null
+++ b/lib/net/hippie/rust_backend.rb
@@ -0,0 +1,72 @@
+# frozen_string_literal: true
+
+module Net
+ module Hippie
+ # Rust backend integration
+ module RustBackend
+ @rust_available = nil
+
+ def self.available?
+ return @rust_available unless @rust_available.nil?
+
+ @rust_available = begin
+ require 'net_hippie_ext'
+ true
+ rescue LoadError
+ false
+ end
+ end
+
+ def self.enabled?
+ ENV['NET_HIPPIE_RUST'] == 'true' && available?
+ end
+
+ # Adapter to make RustResponse behave like Net::HTTPResponse
+ class ResponseAdapter
+ def initialize(rust_response)
+ @rust_response = rust_response
+ @code = rust_response.code
+ @body = rust_response.body
+ end
+
+ def code
+ @code
+ end
+
+ def body
+ @body
+ end
+
+ def [](header_name)
+ @rust_response[header_name.to_s]
+ end
+
+ def class
+ case @code.to_i
+ when 200
+ Net::HTTPOK
+ when 201
+ Net::HTTPCreated
+ when 300..399
+ Net::HTTPRedirection
+ when 400..499
+ Net::HTTPClientError
+ when 500..599
+ Net::HTTPServerError
+ else
+ Net::HTTPResponse
+ end
+ end
+
+ # Make it behave like the expected response class
+ def is_a?(klass)
+ self.class == klass || super
+ end
+
+ def kind_of?(klass)
+ is_a?(klass)
+ end
+ end
+ end
+ end
+end \ No newline at end of file