module Lutaml::Model::Serialize::ClassMethods

def apply_mappings(doc, format)

def apply_mappings(doc, format)
  return apply_xml_mapping(doc) if format == :xml
  mappings = mappings_for(format).mappings
  mappings.each_with_object(Lutaml::Model::MappingHash.new) do |rule, hash|
    attr = if rule.delegate
             attributes[rule.delegate].type.attributes[rule.to]
           else
             attributes[rule.to]
           end
    raise "Attribute '#{rule.to}' not found in #{self}" unless attr
    value = if rule.custom_methods[:from]
              new.send(rule.custom_methods[:from], hash, doc)
            elsif doc.key?(rule.name) || doc.key?(rule.name.to_sym)
              doc[rule.name] || doc[rule.name.to_sym]
            else
              attr.default
            end
    value = apply_child_mappings(value, rule.child_mappings)
    if attr.collection?
      value = (value || []).map do |v|
        attr.type <= Serialize ? attr.type.apply_mappings(v, format) : v
      end
    elsif value.is_a?(Hash) && attr.type != Lutaml::Model::Type::Hash
      value = attr.type.apply_mappings(value, format)
    end
    if rule.delegate
      hash[rule.delegate] ||= {}
      hash[rule.delegate][rule.to] = value
    else
      hash[rule.to] = value
    end
  end
end