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))); }