Module: Leptris::XML::Serialization

Defined in:
lib/leptris/xml/serialization.rb

Overview

Owns the SerializeOptions lifecycle and the owned-string read for document and subtree serialization/canonicalization. Document and Element expose one-line methods over this module — the options struct, the encoding anchor, the C call, and the read-and-free all live here.

Class Method Summary collapse

Class Method Details

.build_options(indent: 0, no_decl: false, encoding: nil) ⇒ Object

Builds a SerializeOptions struct. Returns [opts, encoding_anchor]; the anchor must stay referenced while opts is in an FFI call.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/leptris/xml/serialization.rb', line 48

def self.build_options(indent: 0, no_decl: false, encoding: nil)
  opts = Leptris::XML::FFI::SerializeOptions.new
  opts[:indent] = indent.to_i
  opts[:xml_declaration] = no_decl ? 0 : 1
  encoding_anchor = nil
  if encoding
    encoding_anchor = ::FFI::MemoryPointer.from_string(encoding.to_s)
    opts[:encoding] = encoding_anchor
  end
  [opts, encoding_anchor]
end

.canonicalize(ffi_function, c_ptr, version:, mode:, inclusive_namespaces: nil, with_comments: false) ⇒ Object

ffi_function is a bound FFI function taking (c_ptr, version, mode, ns_ptr, flags): leptris_c14n_canonicalize_ex or leptris_c14n_canonicalize_subtree_ex.



39
40
41
42
43
44
# File 'lib/leptris/xml/serialization.rb', line 39

def self.canonicalize(ffi_function, c_ptr, version:, mode:,
                      inclusive_namespaces: nil, with_comments: false)
  ns_ptr, _ns_anchor = Leptris::XML::CStringArray.to_c(inclusive_namespaces)
  str_ptr = ffi_function.call(c_ptr, version, mode, ns_ptr, with_comments ? 1 : 0)
  Leptris::XML::FFI.read_owned_string(str_ptr)
end

.to_xml(ffi_function, c_ptr, indent: 0, no_decl: false, encoding: nil) ⇒ Object

ffi_function is a bound _serialize_into function. The buffer cycle lives at the FFI seam (FFI.serialize_into_string); this module owns only options selection and construction.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/leptris/xml/serialization.rb', line 24

def self.to_xml(ffi_function, c_ptr, indent: 0, no_decl: false, encoding: nil)
  opts =
    if indent.to_i.zero? && !no_decl && encoding.nil?
      DEFAULT_OPTIONS
    else
      opts, _encoding_anchor = build_options(
        indent: indent, no_decl: no_decl, encoding: encoding)
      opts
    end
  Leptris::XML::FFI.serialize_into_string(ffi_function, c_ptr, opts.pointer)
end