# frozen_string_literal: true

# This file is part of RASN1
# See https://codeberg.org/lemontree55/rasn1 for more informations
# Copyright (C) 2016-2024 Sylvain Daubert <sylvain.daubert@laposte.net>
# Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
# This program is published under MIT license.

module RASN1
  module Types
    # ASN.1 Null
    #
    # This type is a placeholder when a value is not mandatory (in a {Choice}, by example).
    # @author Sylvain Daubert
    class Null < Primitive
      # Null id value
      ID = 0x05

      # @param level [Integer]
      # @return [String]
      def inspect(level=0)
        str = common_inspect(level)[0..-2] # remove terminal ':'
        str << ' OPTIONAL' if optional?
        str
      end

      # Build only if not optional
      # @return [Boolean]
      def can_build?
        !optional?
      end

      # Check +der+ string
      # @param der [String]
      # @param ber [::Boolean]
      # @return [void]
      # @raise [ASN1Error] +der+ is not empty
      def der_to_value(der, ber: false) # rubocop:disable Lint/UnusedMethodArgument
        raise ASN1Error, 'NULL should not have content!' if der.length.positive?

        @no_value = true
        @value = void_value
      end

      private

      # @return [String]
      def value_to_der
        ''
      end
    end
  end
end
