Skip to content

Commit 67fad8f

Browse files
committed
🚧 extras, split off from the SequenceSet PR
1 parent b205add commit 67fad8f

File tree

4 files changed

+34
-62
lines changed

4 files changed

+34
-62
lines changed

lib/net/imap/command_data.rb

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -179,60 +179,11 @@ def initialize(data)
179179
end
180180
end
181181

182+
# Replaced by SequenceSet
182183
class MessageSet # :nodoc:
183-
def send_data(imap, tag)
184-
imap.__send__(:put_string, format_internal(@data))
185-
end
186-
187-
def validate
188-
validate_internal(@data)
189-
end
190-
191-
private
192-
193-
def initialize(data)
194-
@data = data
195-
end
196-
197-
def format_internal(data)
198-
case data
199-
when "*"
200-
return data
201-
when Integer
202-
if data == -1
203-
return "*"
204-
else
205-
return data.to_s
206-
end
207-
when Range
208-
return format_internal(data.first) +
209-
":" + format_internal(data.last)
210-
when Array
211-
return data.collect {|i| format_internal(i)}.join(",")
212-
when ThreadMember
213-
return data.seqno.to_s +
214-
":" + data.children.collect {|i| format_internal(i).join(",")}
215-
end
216-
end
217-
218-
def validate_internal(data)
219-
case data
220-
when "*"
221-
when Integer
222-
NumValidator.ensure_nz_number(data)
223-
when Range
224-
when Array
225-
data.each do |i|
226-
validate_internal(i)
227-
end
228-
when ThreadMember
229-
data.children.each do |i|
230-
validate_internal(i)
231-
end
232-
else
233-
raise DataFormatError, data.inspect
234-
end
235-
end
184+
def initialize(data) @seqset = SequenceSet[data] end
185+
def send_data(imap, tag) @seqset.send_data imap, tag end
186+
def validate; @seqset.validate end
236187
end
237188

238189
class ClientID # :nodoc:

lib/net/imap/data_encoding.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,23 @@ def valid_mod_sequence_value?(num)
186186

187187
# Ensure argument is 'number' or raise DataFormatError
188188
def ensure_number(num)
189-
return if valid_number?(num)
189+
return num if valid_number?(num)
190190

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

195195
# Ensure argument is 'nz_number' or raise DataFormatError
196196
def ensure_nz_number(num)
197-
return if valid_nz_number?(num)
197+
return num if valid_nz_number?(num)
198198

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

203203
# Ensure argument is 'mod_sequence_value' or raise DataFormatError
204204
def ensure_mod_sequence_value(num)
205-
return if valid_mod_sequence_value?(num)
205+
return num if valid_mod_sequence_value?(num)
206206

207207
msg = "mod_sequence_value must be unsigned 64-bit integer: #{num}"
208208
raise DataFormatError, msg

lib/net/imap/response_parser.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,18 @@ def sequence_set
477477
end
478478
end
479479

480+
# *note*: seq-last-command will just return the string "$".
481+
#
482+
# sequence-set = (seq-number / seq-range) ["," sequence-set]
483+
# sequence-set =/ seq-last-command
484+
# ; Allow for "result of the last command"
485+
# ; indicator.
486+
# seq-last-command = "$"
487+
def sequence_set_or_atom
488+
str = atom
489+
Patterns::SEQUENCE_SET_STR.match?(str) ? SequenceSet.new(str) : str
490+
end
491+
480492
# ASTRING-CHAR = ATOM-CHAR / resp-specials
481493
# resp-specials = "]"
482494
ASTRING_CHARS_TOKENS = [*ATOM_TOKENS, T_RBRA].freeze

lib/net/imap/sequence_set.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,18 @@ def input_to_tuple(obj)
12651265
end
12661266
end
12671267

1268+
# For YAML serialization
1269+
def encode_with(coder) # :nodoc:
1270+
# we can reconstruct from the string
1271+
coder['atom'] = to_s
1272+
end
1273+
1274+
# For YAML deserialization
1275+
def init_with(coder) # :nodoc:
1276+
@tuples = []
1277+
self.atom = coder['atom']
1278+
end
1279+
12681280
def input_to_tuples(obj)
12691281
obj = input_try_convert obj
12701282
case obj
@@ -1407,12 +1419,9 @@ def range_gte_to(num)
14071419
end
14081420

14091421
def nz_number(num)
1410-
case num
1411-
when Integer, /\A[1-9]\d*\z/ then num = Integer(num)
1412-
else raise DataFormatError, "%p is not a valid nz-number" % [num]
1413-
end
1414-
NumValidator.ensure_nz_number(num)
1415-
num
1422+
String === num && !/\A[1-9]\d*\z/.match?(num) and
1423+
raise DataFormatError, "%p is not a valid nz-number" % [num]
1424+
NumValidator.ensure_nz_number Integer num
14161425
end
14171426

14181427
# intentionally defined after the class implementation

0 commit comments

Comments
 (0)