Skip to content

♻️ Refactoring (backports) #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/net/imap/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ def self.[](config)
# Use +SASL-IR+ when it is supported by the server and the mechanism.
attr_accessor :sasl_ir, type: :boolean


# Controls the behavior of Net::IMAP#responses when called without any
# arguments (+type+ or +block+).
#
Expand All @@ -269,7 +268,7 @@ def self.[](config)
# Raise an ArgumentError with the deprecation warning.
#
# Note: #responses_without_args is an alias for #responses_without_block.
attr_accessor :responses_without_block, type: [
attr_accessor :responses_without_block, type: Enum[
:silence_deprecation_warning, :warn, :frozen_dup, :raise,
]

Expand Down Expand Up @@ -314,7 +313,7 @@ def self.[](config)
#
# [+false+ <em>(planned default for +v0.6+)</em>]
# ResponseParser _only_ uses AppendUIDData and CopyUIDData.
attr_accessor :parser_use_deprecated_uidplus_data, type: [
attr_accessor :parser_use_deprecated_uidplus_data, type: Enum[
true, :up_to_max_size, false
]

Expand Down
42 changes: 21 additions & 21 deletions lib/net/imap/config/attr_type_coercion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,34 @@ def self.included(mod)
end
private_class_method :included

def self.attr_accessor(attr, type: nil)
return unless type
if :boolean == type then boolean attr
elsif Integer == type then integer attr
elsif Array === type then enum attr, type
else raise ArgumentError, "unknown type coercion %p" % [type]
end
if defined?(Ractor.make_shareable)
def self.safe(...) Ractor.make_shareable nil.instance_eval(...).freeze end
else
def self.safe(...) nil.instance_eval(...).freeze end
end
private_class_method :safe

def self.boolean(attr)
define_method :"#{attr}=" do |val| super !!val end
define_method :"#{attr}?" do send attr end
Types = Hash.new do |h, type|
type.nil? || Proc === type or raise TypeError, "type not nil or Proc"
safe{type}
end
Types[:boolean] = Boolean = safe{-> {!!_1}}
Types[Integer] = safe{->{Integer(_1)}}

def self.integer(attr)
define_method :"#{attr}=" do |val| super Integer val end
def self.attr_accessor(attr, type: nil)
type = Types[type] or return
define_method :"#{attr}=" do |val| super type[val] end
define_method :"#{attr}?" do send attr end if type == Boolean
end

def self.enum(attr, enum)
enum = enum.dup.freeze
Enum = ->(*enum) {
enum = safe{enum}
expected = -"one of #{enum.map(&:inspect).join(", ")}"
define_method :"#{attr}=" do |val|
unless enum.include?(val)
raise ArgumentError, "expected %s, got %p" % [expected, val]
end
super val
end
end
safe{->val {
return val if enum.include?(val)
raise ArgumentError, "expected %s, got %p" % [expected, val]
}}
}

end
end
Expand Down
20 changes: 14 additions & 6 deletions lib/net/imap/response_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,34 @@ def initialize(client, sock)
end

def read_response_buffer
buff = String.new
@buff = String.new
catch :eof do
while true
read_line(buff)
break unless /\{(\d+)\}\r\n\z/n =~ buff
read_literal(buff, $1.to_i)
read_line
break unless (@literal_size = get_literal_size)
read_literal
end
end
buff
ensure
@buff = nil
end

private

def read_line(buff)
attr_reader :buff, :literal_size

def get_literal_size; /\{(\d+)\}\r\n\z/n =~ buff && $1.to_i end

def read_line
buff << (@sock.gets(CRLF) or throw :eof)
end

def read_literal(buff, literal_size)
def read_literal
literal = String.new(capacity: literal_size)
buff << (@sock.read(literal_size, literal) or throw :eof)
ensure
@literal_size = nil
end

end
Expand Down
3 changes: 3 additions & 0 deletions test/net/imap/test_response_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ def literal(str) "{#{str.bytesize}}\r\n#{str}" end
long_line = "tag ok #{aaaaaaaaa} #{aaaaaaaaa}\r\n"
literal_aaaa = "* fake #{literal aaaaaaaaa}\r\n"
literal_crlf = "tag ok #{literal many_crlfs} #{literal many_crlfs}\r\n"
zero_literal = "tag ok #{literal ""} #{literal ""}\r\n"
illegal_crs = "tag ok #{many_crs} #{many_crs}\r\n"
illegal_lfs = "tag ok #{literal "\r"}\n#{literal "\r"}\n\r\n"
io = StringIO.new([
simple,
long_line,
literal_aaaa,
literal_crlf,
zero_literal,
illegal_crs,
illegal_lfs,
simple,
Expand All @@ -40,6 +42,7 @@ def literal(str) "{#{str.bytesize}}\r\n#{str}" end
assert_equal long_line, rcvr.read_response_buffer.to_str
assert_equal literal_aaaa, rcvr.read_response_buffer.to_str
assert_equal literal_crlf, rcvr.read_response_buffer.to_str
assert_equal zero_literal, rcvr.read_response_buffer.to_str
assert_equal illegal_crs, rcvr.read_response_buffer.to_str
assert_equal illegal_lfs, rcvr.read_response_buffer.to_str
assert_equal simple, rcvr.read_response_buffer.to_str
Expand Down
Loading