Module: RASN2::ValueNotation
- Defined in:
- lib/rasn2/value_notation.rb
Overview
Support for ASN.1 value notation (text representation of ASN.1 data instances).
ASN.1 value notation allows defining values for ASN.1 types in a human-readable text format. For example:
myPerson PersonnelRecord ::= {
name "John Doe",
title "Engineer",
age 30,
employed TRUE
}
This module provides methods to parse such text into Model instances and to generate such text from Model instances.
Defined Under Namespace
Classes: Error
Class Method Summary collapse
-
.emit(model, name: nil, type_name: nil) ⇒ String
Emit ASN.1 value notation text from a model instance.
-
.parse(text, models:) ⇒ Model
Parse an ASN.1 value notation string and populate a model instance.
-
.parse_file(filename, models:) ⇒ Model
Parse an ASN.1 value notation file.
Class Method Details
.emit(model, name: nil, type_name: nil) ⇒ String
Emit ASN.1 value notation text from a model instance.
76 77 78 79 80 81 |
# File 'lib/rasn2/value_notation.rb', line 76 def emit(model, name: nil, type_name: nil) name ||= model.respond_to?(:value_name) ? model.value_name : 'value' type_name ||= model.respond_to?(:type_name) ? model.type_name : model.class.type body = emit_constructed(model, indent_level: 1) "#{name} #{type_name} ::= {\n#{body}\n}\n" end |
.parse(text, models:) ⇒ Model
Parse an ASN.1 value notation string and populate a model instance.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rasn2/value_notation.rb', line 42 def parse(text, models:) text = text.strip match = text.match(/\A(\w+)\s+(\w+)\s*::=\s*(.*)\z/m) raise Error, 'Invalid value notation format' unless match value_name = match[1] type_name = match[2] body = match[3].strip model_class = models[type_name] raise Error, "Unknown type: #{type_name}" unless model_class values = parse_constructed_value(body) instance = model_class.new(values) instance.define_singleton_method(:value_name) { value_name } instance.define_singleton_method(:type_name) { type_name } instance end |
.parse_file(filename, models:) ⇒ Model
Parse an ASN.1 value notation file.
66 67 68 |
# File 'lib/rasn2/value_notation.rb', line 66 def parse_file(filename, models:) parse(File.read(filename), models: models) end |