<%
def make_new_type(it)
  case it[:type]
  when "integer" then
    "usize"
  when "enum" then
    "HashSet<#{it[:name].ucc}>"
  when "enum_single" then
    "#{it[:name].ucc}"
  when "date" then
    "DateTime<Utc>"
  when "bool" then
    "bool"
  else
    "&str"
  end
end

def required_new(it)
  case it[:type]
  when "string" then
    "#{it[:name].make_name.make_field}: #{it[:name].make_field}.to_owned()"    
  else
    "#{it[:name].make_name.make_field}"
  end
end

def make_open_or_all(it, open_flag)
  value = it[:name].ucc
  if open_flag && ["media.fields", "tweet.fields"].include?(it[:name])
    value = value + "::open()"
  else
    if it[:name] == "tweet.fields"
      value = value + "::organic()"
    else
      value = value + "::all()"
    end
  end
  unless it[:required]
    value = "Some(#{value})"
  end
  "#{it[:name].make_name.make_field}: #{value},"
end

def make_max(it)
  value = it[:max].to_s
  unless it[:required]
    value = "Some(#{value})"
  end
  "#{it[:name].make_name.make_field}: #{value},"
end

open_flag = queries.filter{|it| ["media.fields", "tweet.fields"].include?(it[:name])}.present?
enum_list = queries.filter{|it| it[:type] == "enum"}
max_list = queries.filter{|it| it[:max].present? }
default_flag = non_required_queries.size > (enum_list.size + max_list.size)

new_array = self_required.map{|it| "#{it[:name].make_name.make_field}: #{make_new_type(it)}"}
new_array << "body: Body" if bodies.present?
%>pub fn new(<%= new_array.join(", ") %>) -> Self {
    Self {<% self_required.each do |it| %>
        <%= required_new(it) %>,<% end %><% if bodies.present? %>
        body,<% end %>
        ..Default::default()
    }
}<% if enum_list.present? %>

pub fn all(<%= new_array.join(", ") %>) -> Self {
    Self {<% self_required.each do |it| %>
        <%= required_new(it) %>,<% end %><% if bodies.present? %>
        body,<% end %><% enum_list.each do |it| %>
        <%= make_open_or_all(it, false) %><% end %><% max_list.each do |it| %>
        <%= make_max(it) %><% end %>
        ..Default::default()
    }
}<% end %><% if open_flag %>

pub fn open(<%= new_array.join(", ") %>) -> Self {
    Self {<% self_required.each do |it| %>
        <%= required_new(it) %>,<% end %><% if bodies.present? %>
        body,<% end %><% enum_list.each do |it| %>
        <%= make_open_or_all(it, true) %><% end %><% max_list.each do |it| %>
        <%= make_max(it) %><% end %>
        ..Default::default()
    }
}<% end %>