Skip to content

Commit 9aaa582

Browse files
committed
✨ Convert symbols & ranges in search return opts
1 parent 78f3d6b commit 9aaa582

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/net/imap.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,9 @@ def uid_expunge(uid_set)
19961996
# ==== Argument translation
19971997
#
19981998
# [+return+ options]
1999-
# Must be an Array. Return option names are strings.
1999+
# Must be an Array. Return option names may be either strings or symbols.
2000+
# +Range+ elements which begin and end with negative integers are encoded
2001+
# for use with +PARTIAL+--any other ranges are converted to SequenceSet.
20002002
# Unlike +criteria+, other return option arguments are not automatically
20012003
# converted to SequenceSet.
20022004
#
@@ -3270,10 +3272,28 @@ def search_args(keys, charset_arg = nil, return: nil, charset: nil)
32703272
end
32713273

32723274
def convert_return_opts(unconverted)
3273-
Array.try_convert(unconverted) or
3275+
return_opts = Array.try_convert(unconverted) or
32743276
raise TypeError, "expected return options to be Array, got %s" % [
32753277
unconverted.class
32763278
]
3279+
return_opts.map {|opt|
3280+
case opt
3281+
when Symbol then opt.to_s
3282+
when Range then partial_range_last_or_seqset(opt)
3283+
else opt
3284+
end
3285+
}
3286+
end
3287+
3288+
def partial_range_last_or_seqset(range)
3289+
case [range.begin, range.end]
3290+
in [Integer => first, Integer => last] if first.negative? && last.negative?
3291+
# partial-range-last [RFC9394]
3292+
first <= last or raise DataFormatError, "empty range: %p" % [range]
3293+
"#{first}:#{last}"
3294+
else
3295+
SequenceSet[range]
3296+
end
32773297
end
32783298

32793299
def search_internal(cmd, ...)

test/net/imap/test_imap.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,18 @@ def seqset_coercible.to_sequence_set
12771277
)
12781278
cmd = server.commands.pop
12791279
assert_equal "RETURN (PARTIAL -500:-1) UID 1234:*", cmd.args
1280+
1281+
assert_equal search_result, imap.search(
1282+
["UID", 1234..], return: [:PARTIAL, "-500:-1"]
1283+
)
1284+
cmd = server.commands.pop
1285+
assert_equal "RETURN (PARTIAL -500:-1) UID 1234:*", cmd.args
1286+
1287+
assert_equal search_result, imap.search(
1288+
["UID", 1234..], return: [:PARTIAL, -500..-1, :FOO, 1..]
1289+
)
1290+
cmd = server.commands.pop
1291+
assert_equal "RETURN (PARTIAL -500:-1 FOO 1:*) UID 1234:*", cmd.args
12801292
end
12811293
end
12821294

0 commit comments

Comments
 (0)