From d13034ba50c63c41d1e1f894894d5a7b6568fe62 Mon Sep 17 00:00:00 2001 From: nick evans Date: Mon, 27 Nov 2023 08:52:34 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20extras,=20split=20off=20from=20t?= =?UTF-8?q?he=20SequenceSet=20PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/net/imap/command_data.rb | 57 +++------------------------------ lib/net/imap/data_encoding.rb | 6 ++-- lib/net/imap/response_parser.rb | 12 +++++++ lib/net/imap/sequence_set.rb | 21 ++++++++---- 4 files changed, 34 insertions(+), 62 deletions(-) diff --git a/lib/net/imap/command_data.rb b/lib/net/imap/command_data.rb index 5739f909..58c312e3 100644 --- a/lib/net/imap/command_data.rb +++ b/lib/net/imap/command_data.rb @@ -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: diff --git a/lib/net/imap/data_encoding.rb b/lib/net/imap/data_encoding.rb index 8ced2935..fda96c91 100644 --- a/lib/net/imap/data_encoding.rb +++ b/lib/net/imap/data_encoding.rb @@ -186,7 +186,7 @@ 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 @@ -194,7 +194,7 @@ def ensure_number(num) # 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 @@ -202,7 +202,7 @@ def ensure_nz_number(num) # 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 diff --git a/lib/net/imap/response_parser.rb b/lib/net/imap/response_parser.rb index 84f6cf79..dec8391a 100644 --- a/lib/net/imap/response_parser.rb +++ b/lib/net/imap/response_parser.rb @@ -474,6 +474,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 diff --git a/lib/net/imap/sequence_set.rb b/lib/net/imap/sequence_set.rb index 948941bf..02f09902 100644 --- a/lib/net/imap/sequence_set.rb +++ b/lib/net/imap/sequence_set.rb @@ -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 @@ -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