Skip to content

Commit b1413c6

Browse files
committed
✨ Customize SequenceSet YAML serialization
We can perfectly reconstruct the SequenceSet from the string. The string is the canonical IMAP representation. And, even if the tuples representation uses less memory, the string representation will always be more compact than the naive YAML serialization of the tuples. So only serialize the string.
1 parent d839268 commit b1413c6

File tree

5 files changed

+13
-124
lines changed

5 files changed

+13
-124
lines changed

lib/net/imap/sequence_set.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,18 @@ def send_data(imap, tag) # :nodoc:
13691369
imap.__send__(:put_string, valid_string)
13701370
end
13711371

1372+
# For YAML serialization
1373+
def encode_with(coder) # :nodoc:
1374+
# we can perfectly reconstruct from the string
1375+
coder['string'] = to_s
1376+
end
1377+
1378+
# For YAML deserialization
1379+
def init_with(coder) # :nodoc:
1380+
@tuples = []
1381+
self.string = coder['string']
1382+
end
1383+
13721384
protected
13731385

13741386
attr_reader :tuples # :nodoc:

test/net/imap/fixtures/response_parser/esearch_responses.yml

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
- - ALL
2626
- !ruby/object:Net::IMAP::SequenceSet
2727
string: 2,10:11
28-
tuples:
29-
- - 2
30-
- 2
31-
- - 10
32-
- 11
3328
raw_data: "* ESEARCH (TAG \"A283\") ALL 2,10:11\r\n"
3429

3530
rfc9051_6.4.4_ESEARCH_example_3:
@@ -53,9 +48,6 @@
5348
- - ALL
5449
- !ruby/object:Net::IMAP::SequenceSet
5550
string: '43'
56-
tuples:
57-
- - 43
58-
- 43
5951
raw_data: "* ESEARCH (TAG \"A285\") ALL 43\r\n"
6052

6153
rfc9051_6.4.4_ESEARCH_example_5:
@@ -107,11 +99,6 @@
10799
- - ALL
108100
- !ruby/object:Net::IMAP::SequenceSet
109101
string: '17,900,901'
110-
tuples:
111-
- - 17
112-
- 17
113-
- - 900
114-
- 901
115102
raw_data: "* ESEARCH (TAG \"A301\") UID ALL 17,900,901\r\n"
116103

117104
rfc9051_6.4.4.4_ESEARCH_example_2:
@@ -125,15 +112,6 @@
125112
- - ALL
126113
- !ruby/object:Net::IMAP::SequenceSet
127114
string: 882,1102,3003,3005:3006
128-
tuples:
129-
- - 882
130-
- 882
131-
- - 1102
132-
- 1102
133-
- - 3003
134-
- 3003
135-
- - 3005
136-
- 3006
137115
raw_data: "* ESEARCH (TAG \"P283\") ALL 882,1102,3003,3005:3006\r\n"
138116

139117
rfc9051_6.4.4.4_ESEARCH_example_3:
@@ -147,13 +125,6 @@
147125
- - ALL
148126
- !ruby/object:Net::IMAP::SequenceSet
149127
string: 3:15,27,29:103
150-
tuples:
151-
- - 3
152-
- 15
153-
- - 27
154-
- 27
155-
- - 29
156-
- 103
157128
raw_data: "* ESEARCH (TAG \"G283\") ALL 3:15,27,29:103\r\n"
158129

159130
rfc9051_6.4.4.4_ESEARCH_example_4:
@@ -167,13 +138,6 @@
167138
- - ALL
168139
- !ruby/object:Net::IMAP::SequenceSet
169140
string: 2,10:15,21
170-
tuples:
171-
- - 2
172-
- 2
173-
- - 10
174-
- 15
175-
- - 21
176-
- 21
177141
raw_data: "* ESEARCH (TAG \"C283\") ALL 2,10:15,21\r\n"
178142

179143
rfc9051_6.4.4.4_ESEARCH_example_5:
@@ -231,13 +195,6 @@
231195
- - ALL
232196
- !ruby/object:Net::IMAP::SequenceSet
233197
string: 2,10:15,21
234-
tuples:
235-
- - 2
236-
- 2
237-
- - 10
238-
- 15
239-
- - 21
240-
- 21
241198
raw_data: "* ESEARCH (TAG \"C286\") MIN 2 ALL 2,10:15,21\r\n"
242199

243200
rfc9051_7.1_ESEARCH_example_1:
@@ -251,19 +208,6 @@
251208
- - ALL
252209
- !ruby/object:Net::IMAP::SequenceSet
253210
string: 1:3,5,8,13,21,42
254-
tuples:
255-
- - 1
256-
- 3
257-
- - 5
258-
- 5
259-
- - 8
260-
- 8
261-
- - 13
262-
- 13
263-
- - 21
264-
- 21
265-
- - 42
266-
- 42
267211
raw_data: "* ESEARCH (TAG \"h\") ALL 1:3,5,8,13,21,42\r\n"
268212

269213
rfc9051_7.3.4_ESEARCH_example_1:
@@ -279,13 +223,6 @@
279223
- - ALL
280224
- !ruby/object:Net::IMAP::SequenceSet
281225
string: 4:18,21,28
282-
tuples:
283-
- - 4
284-
- 18
285-
- - 21
286-
- 21
287-
- - 28
288-
- 28
289226
raw_data: "* ESEARCH UID COUNT 17 ALL 4:18,21,28\r\n"
290227

291228
rfc9051_7.3.4_ESEARCH_example_2:
@@ -301,13 +238,6 @@
301238
- - ALL
302239
- !ruby/object:Net::IMAP::SequenceSet
303240
string: 4:18,21,28
304-
tuples:
305-
- - 4
306-
- 18
307-
- - 21
308-
- 21
309-
- - 28
310-
- 28
311241
raw_data: "* ESEARCH (TAG \"a567\") UID COUNT 17 ALL 4:18,21,28\r\n"
312242

313243
rfc9051_7.3.4_ESEARCH_example_3:
@@ -323,9 +253,4 @@
323253
- - ALL
324254
- !ruby/object:Net::IMAP::SequenceSet
325255
string: 1:17,21
326-
tuples:
327-
- - 1
328-
- 17
329-
- - 21
330-
- 21
331256
raw_data: "* ESEARCH COUNT 18 ALL 1:17,21\r\n"

test/net/imap/fixtures/response_parser/rfc7162_condstore_qresync_responses.yml

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@
5555
code: !ruby/struct:Net::IMAP::ResponseCode
5656
name: MODIFIED
5757
data: !ruby/object:Net::IMAP::SequenceSet
58-
str: '7,9'
59-
tuples:
60-
- - 7
61-
- 7
62-
- - 9
63-
- 9
58+
string: '7,9'
6459
text: Conditional STORE failed
6560
raw_data: "d105 OK [MODIFIED 7,9] Conditional STORE failed\r\n"
6661

@@ -109,11 +104,6 @@
109104
- - ALL
110105
- !ruby/object:Net::IMAP::SequenceSet
111106
string: 1:3,5
112-
tuples:
113-
- - 1
114-
- 3
115-
- - 5
116-
- 5
117107
- - MODSEQ
118108
- 1236
119109
raw_data: "* ESEARCH (TAG \"a\") ALL 1:3,5 MODSEQ 1236\r\n"
@@ -129,11 +119,6 @@
129119
- - ALL
130120
- !ruby/object:Net::IMAP::SequenceSet
131121
string: '5,3,2,1'
132-
tuples:
133-
- - 1
134-
- 3
135-
- - 5
136-
- 5
137122
- - MODSEQ
138123
- 1236
139124
raw_data: "* ESEARCH (TAG \"a\") ALL 5,3,2,1 MODSEQ 1236\r\n"
@@ -145,17 +130,6 @@
145130
data: !ruby/object:Net::IMAP::VanishedData
146131
uids: !ruby/object:Net::IMAP::SequenceSet
147132
string: 41,43:116,118,120:211,214:540
148-
tuples:
149-
- - 41
150-
- 41
151-
- - 43
152-
- 116
153-
- - 118
154-
- 118
155-
- - 120
156-
- 211
157-
- - 214
158-
- 540
159133
earlier: true
160134
raw_data: "* VANISHED (EARLIER) 41,43:116,118,120:211,214:540\r\n"
161135

@@ -166,14 +140,5 @@
166140
data: !ruby/object:Net::IMAP::VanishedData
167141
uids: !ruby/object:Net::IMAP::SequenceSet
168142
string: '405,407,410,425'
169-
tuples:
170-
- - 405
171-
- 405
172-
- - 407
173-
- 407
174-
- - 410
175-
- 410
176-
- - 425
177-
- 425
178143
earlier: false
179144
raw_data: "* VANISHED 405,407,410,425\r\n"

test/net/imap/fixtures/response_parser/rfc9394_partial.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
excl: false
2121
results: !ruby/object:Net::IMAP::SequenceSet
2222
string: 200:250,252:300
23-
tuples:
24-
- - 200
25-
- 250
26-
- - 252
27-
- 300
2823
raw_data: "* ESEARCH (TAG \"A01\") UID PARTIAL (-1:-100 200:250,252:300)\r\n"
2924

3025
"RFC9394 PARTIAL 3.1. example 2":
@@ -43,9 +38,6 @@
4338
excl: false
4439
results: !ruby/object:Net::IMAP::SequenceSet
4540
string: 55500:56000
46-
tuples:
47-
- - 55500
48-
- 56000
4941
raw_data: "* ESEARCH (TAG \"A02\") UID PARTIAL (23500:24000 55500:56000)\r\n"
5042

5143
"RFC9394 PARTIAL 3.1. example 3":

test/net/imap/fixtures/response_parser/status_responses.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@
5252
SEQ: !ruby/struct:Net::IMAP::ExtensionData
5353
data: !ruby/object:Net::IMAP::SequenceSet
5454
string: 1234:5,*:789654
55-
tuples:
56-
- - 5
57-
- 1234
58-
- - 789654
59-
- 4294967296
6055
COMP-EMPTY: !ruby/struct:Net::IMAP::ExtensionData
6156
data: []
6257
COMP-QUOTED: !ruby/struct:Net::IMAP::ExtensionData

0 commit comments

Comments
 (0)