Skip to content

Commit

Permalink
🚧 extras, split off from the SequenceSet PR
Browse files Browse the repository at this point in the history
  • Loading branch information
nevans committed Jun 26, 2024
1 parent d276458 commit 7d0f8e0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 62 deletions.
57 changes: 4 additions & 53 deletions lib/net/imap/command_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,60 +179,11 @@ def initialize(data)
end
end

# Replaced by SequenceSet
class MessageSet # :nodoc:
def send_data(imap, tag)
imap.__send__(:put_string, format_internal(@data))
end

def validate
validate_internal(@data)
end

private

def initialize(data)
@data = data
end

def format_internal(data)
case data
when "*"
return data
when Integer
if data == -1
return "*"
else
return data.to_s
end
when Range
return format_internal(data.first) +
":" + format_internal(data.last)
when Array
return data.collect {|i| format_internal(i)}.join(",")
when ThreadMember
return data.seqno.to_s +
":" + data.children.collect {|i| format_internal(i).join(",")}
end
end

def validate_internal(data)
case data
when "*"
when Integer
NumValidator.ensure_nz_number(data)
when Range
when Array
data.each do |i|
validate_internal(i)
end
when ThreadMember
data.children.each do |i|
validate_internal(i)
end
else
raise DataFormatError, data.inspect
end
end
def initialize(data) @seqset = SequenceSet[data] end
def send_data(imap, tag) @seqset.send_data imap, tag end
def validate; @seqset.validate end
end

class ClientID # :nodoc:
Expand Down
6 changes: 3 additions & 3 deletions lib/net/imap/data_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,23 @@ def valid_mod_sequence_value?(num)

# Ensure argument is 'number' or raise DataFormatError
def ensure_number(num)
return if valid_number?(num)
return num if valid_number?(num)

msg = "number must be unsigned 32-bit integer: #{num}"
raise DataFormatError, msg
end

# Ensure argument is 'nz_number' or raise DataFormatError
def ensure_nz_number(num)
return if valid_nz_number?(num)
return num if valid_nz_number?(num)

msg = "nz_number must be non-zero unsigned 32-bit integer: #{num}"
raise DataFormatError, msg
end

# Ensure argument is 'mod_sequence_value' or raise DataFormatError
def ensure_mod_sequence_value(num)
return if valid_mod_sequence_value?(num)
return num if valid_mod_sequence_value?(num)

msg = "mod_sequence_value must be unsigned 64-bit integer: #{num}"
raise DataFormatError, msg
Expand Down
12 changes: 12 additions & 0 deletions lib/net/imap/response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,18 @@ def sequence_set
end
end

# *note*: seq-last-command will just return the string "$".
#
# sequence-set = (seq-number / seq-range) ["," sequence-set]
# sequence-set =/ seq-last-command
# ; Allow for "result of the last command"
# ; indicator.
# seq-last-command = "$"
def sequence_set_or_atom
str = atom
Patterns::SEQUENCE_SET_STR.match?(str) ? SequenceSet.new(str) : str
end

# ASTRING-CHAR = ATOM-CHAR / resp-specials
# resp-specials = "]"
ASTRING_CHARS_TOKENS = [*ATOM_TOKENS, T_RBRA].freeze
Expand Down
21 changes: 15 additions & 6 deletions lib/net/imap/sequence_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,18 @@ def input_to_tuple(obj)
end
end

# For YAML serialization
def encode_with(coder) # :nodoc:
# we can reconstruct from the string
coder['atom'] = to_s
end

# For YAML deserialization
def init_with(coder) # :nodoc:
@tuples = []
self.atom = coder['atom']
end

def input_to_tuples(obj)
obj = input_try_convert obj
case obj
Expand Down Expand Up @@ -1407,12 +1419,9 @@ def range_gte_to(num)
end

def nz_number(num)
case num
when Integer, /\A[1-9]\d*\z/ then num = Integer(num)
else raise DataFormatError, "%p is not a valid nz-number" % [num]
end
NumValidator.ensure_nz_number(num)
num
String === num && !/\A[1-9]\d*\z/.match?(num) and
raise DataFormatError, "%p is not a valid nz-number" % [num]
NumValidator.ensure_nz_number Integer num
end

# intentionally defined after the class implementation
Expand Down

0 comments on commit 7d0f8e0

Please sign in to comment.