Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

♻️ Refactor internal command data classes #358

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 21 additions & 51 deletions lib/net/imap/command_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "date"

require_relative "errors"
require_relative "data_lite"

module Net
class IMAP < Protocol
Expand Down Expand Up @@ -119,80 +120,53 @@ def send_symbol_data(symbol)
put_string("\\" + symbol.to_s)
end

class RawData # :nodoc:
CommandData = Data.define(:data) do # :nodoc:
def send_data(imap, tag)
imap.__send__(:put_string, @data)
raise NoMethodError, "#{self.class} must implement #{__method__}"
end

def validate
end

private

def initialize(data)
@data = data
end
end

class Atom # :nodoc:
class RawData < CommandData # :nodoc:
def send_data(imap, tag)
imap.__send__(:put_string, @data)
end

def validate
end

private

def initialize(data)
@data = data
imap.__send__(:put_string, data)
end
end

class QuotedString # :nodoc:
class Atom < CommandData # :nodoc:
def send_data(imap, tag)
imap.__send__(:send_quoted_string, @data)
end

def validate
end

private

def initialize(data)
@data = data
imap.__send__(:put_string, data)
end
end

class Literal # :nodoc:
class QuotedString < CommandData # :nodoc:
def send_data(imap, tag)
imap.__send__(:send_literal, @data, tag)
end

def validate
imap.__send__(:send_quoted_string, data)
end
end

private

def initialize(data)
@data = data
class Literal < CommandData # :nodoc:
def send_data(imap, tag)
imap.__send__(:send_literal, data, tag)
end
end

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

def validate
validate_internal(@data)
validate_internal(data)
end

private

def initialize(data)
@data = data
def initialize(data:)
super
warn("DEPRECATED: #{MessageSet} should be replaced with #{SequenceSet}.",
uplevel: 1, category: :deprecated)
begin
Expand Down Expand Up @@ -246,22 +220,18 @@ def validate_internal(data)
end
end

class ClientID # :nodoc:
class ClientID < CommandData # :nodoc:

def send_data(imap, tag)
imap.__send__(:send_data, format_internal(@data), tag)
imap.__send__(:send_data, format_internal(data), tag)
end

def validate
validate_internal(@data)
validate_internal(data)
end

private

def initialize(data)
@data = data
end

def validate_internal(client_id)
client_id.to_h.each do |k,v|
unless StringFormatter.valid_string?(k)
Expand Down
Loading