Exemple #1
0
        internal static List <BaseField> ParseFields(string value, Hl7Encoding encoding)
        {
            string name = GetSegmentName(value);
            IEnumerable <string> allFields = EncodingHelper.Split(value, encoding.FieldDelimiter);

            return(ParseFields(allFields, encoding, name == MSH));
        }
Exemple #2
0
 internal static BaseComponent CreateComponent(Hl7Encoding encoding, string value)
 {
     if (value.Contains(encoding.SubComponentDelimiter.ToString()))
     {
         return(new ComplexComponent(EncodingHelper.Split(value, encoding.SubComponentDelimiter).ConvertAll(s => new SubComponent(encoding.Decode(s), encoding)), encoding));
     }
     return(new SimpleComponent(value, encoding));
 }
Exemple #3
0
 internal static BaseField CreateField(Hl7Encoding encoding, string value)
 {
     if (value.Contains(encoding.RepeatDelimiter))
     {
         return(new RepetitionField(EncodingHelper.Split(value, encoding.RepeatDelimiter).ConvertAll(f => CreateField(encoding, f)), encoding));
     }
     if (value.Contains(encoding.ComponentDelimiter))
     {
         return(new ComponentField(EncodingHelper.Split(value, encoding.ComponentDelimiter).ConvertAll(c => CreateComponent(encoding, c)), encoding));
     }
     return(new ValueField(EncodingHelper.Decode(encoding, value), encoding));
 }
Exemple #4
0
        internal static Result <ElementIndex> TryParseIndexesFromValueFormat(string format)
        {
            BaseSpecificElementIndex?fieldIndex = null, subFieldIndex = null, componentIndex = null;
            int?          subComponentIndex = null;
            List <string> allComponents     = EncodingHelper.Split(format, '.');

            if (allComponents.Count > 4 || allComponents.Count == 0)
            {
                return(ErrorReturn <ElementIndex>($"Expected 1-4 values in the valueFormat field of ParseIndexesFromValueFormat, expected: SegmentName.FieldIndex.ComponentIndex.SubComponentIndex found {format}."));
            }
            if (!GetSegmentIndex(allComponents[0]).DecomposeResult(out var segmentIndex, out string?error))
            {
                return(ErrorReturn <ElementIndex>(error ?? "unknown error parsing segment name"));
            }
            if (allComponents.Count > 1)
            {
                string field = allComponents[1];
                if (field.Contains('-'))
                {
                    List <string> fieldParts = EncodingHelper.Split(field, '-');
                    if (fieldParts.Count != 2)
                    {
                        return(ErrorReturn <ElementIndex>($"Expected exactly 2 ints seperated by a dash in the field position found {field} instead"));
                    }
                    field = fieldParts[0];
                    if (!GetElementIndex(fieldParts[1]).DecomposeResult(out subFieldIndex, out error))
                    {
                        return(ErrorReturn <ElementIndex>(error ?? "unknown error parsing indexes for subfield"));
                    }
                }
                if (!GetElementIndex(field).DecomposeResult(out fieldIndex, out error))
                {
                    return(ErrorReturn <ElementIndex>(error ?? "unknown error parsing indexes for field"));
                }
                if (allComponents.Count > 2)
                {
                    if (!GetElementIndex(allComponents[2]).DecomposeResult(out componentIndex, out error))
                    {
                        return(ErrorReturn <ElementIndex>(error ?? "unknown error parsing indexes for component"));
                    }
                    subComponentIndex = ParseIntFromStringList(allComponents, 3);
                }
            }
            return(new Result <ElementIndex>(new ElementIndex(segmentIndex, fieldIndex, subFieldIndex, componentIndex, subComponentIndex), null));
        }
Exemple #5
0
        internal static Result <string> TryGetMessageFormat(this Message message)
        {
            if (message == null)
            {
                return(ErrorReturn <string>("Attempted to get MSH segment, but message is null"));
            }
            string?messageStructure = GetMessageType(message);

            if (string.IsNullOrWhiteSpace(messageStructure))
            {
                return(ErrorReturn <string>("MSH 9.3 not found"));
            }
            List <string> structure = EncodingHelper.Split(messageStructure, message.Encoding.ComponentDelimiter);

            if (structure.Count >= 3)
            {
                messageStructure = structure[2];
            }
            else if (structure[0] == MessageHelper.ACK)
            {
                messageStructure = structure[0];
            }
            else if (structure.Count == 2)
            {
                messageStructure = structure[0] + "_" + structure[1];
            }
            else
            {
                return(ErrorReturn <string>("Message Type & Trigger Event value not found in message"));
            }
            if (string.IsNullOrWhiteSpace(messageStructure))
            {
                return(ErrorReturn <string>("MSH 9.3 not properly formatted"));
            }
            return(new Result <string>(messageStructure !, null));
        }
Exemple #6
0
        internal static Result <BaseSegmentIndex> GetSegmentIndex(string nameIn)
        {
            string?segIndex = null, colIndexStr = null;

            if (nameIn.Contains('-'))
            {
                List <string> segmentParts = EncodingHelper.Split(nameIn, '-');
                if (segmentParts.Count != 2)
                {
                    return(ErrorReturn <BaseSegmentIndex>($"Expected only one dash in index similiar to x-y or x:y-z in the segmentName position found {nameIn} instead"));
                }
                segIndex = segmentParts[1];
            }
            List <string> multiSegmentParts = nameIn.Split(':', '-').ToList();

            if (nameIn.Contains(':'))
            {
                if (nameIn.Contains('-') && multiSegmentParts.Count != 3)
                {
                    return(ErrorReturn <BaseSegmentIndex>($"Expected exactly 3 values in the format x:y-z in the segementname position, found {nameIn} instead"));
                }
                if (!nameIn.Contains('-') && multiSegmentParts.Count != 2)
                {
                    return(ErrorReturn <BaseSegmentIndex>($"Expected exactly 2 values in the format x:y in the segementname position, found {nameIn} instead"));
                }
                colIndexStr = multiSegmentParts[1];
            }
            int?colIndex = null;

            if (!string.IsNullOrWhiteSpace(colIndexStr))
            {
                if (int.TryParse(colIndexStr, out int colIndexInt))
                {
                    colIndex = colIndexInt;
                }
                else
                {
                    return(ErrorReturn <BaseSegmentIndex>($"Could not parse segment collection index as int value {colIndexStr} from {nameIn}"));
                }
            }
            if (int.TryParse(multiSegmentParts[0], out int segIntIndex))
            {
                if (segIndex != null)
                {
                    return(ErrorReturn <BaseSegmentIndex>($"attempted to use a segment indexer ({segIndex}), with a segment ordinal ({segIntIndex}), you can't use a segment ordinal with a segment index"));
                }
                if (colIndexStr != null)
                {
                    return(ErrorReturn <BaseSegmentIndex>($"attempted to use a multisegment ordinal ({segIndex}), with a segment ordinal ({segIntIndex}), you can't use a multisegment ordinal with a segment index"));
                }
                return(new Result <BaseSegmentIndex>(new SegmentOrdinalIndex(segIntIndex)));
            }
            if (segIndex != null)
            {
                if (int.TryParse(segIndex, out int index))
                {
                    return(new Result <BaseSegmentIndex>(new SegmentNameAndOrdinalIndex(multiSegmentParts[0], index, colIndex)));
                }
                else
                {
                    return(new Result <BaseSegmentIndex>(new SegmentNameAndIdIndex(multiSegmentParts[0], segIndex, colIndex)));
                }
            }
            return(new Result <BaseSegmentIndex>(new SegmentNamedIndex(multiSegmentParts[0], colIndex)));
        }