예제 #1
0
        public static Result <SisLine, Error> DeserializeSisLine(string line)
        {
            var cells = line.Split('\t');

            if (cells.Length < 5)
            {
                return(new Error($"Invalid tab count, cannot parse as SisLine: {line}"));
            }

            var pos = DiagramPosition.TryFrom(cells[0]);

            if (!int.TryParse(cells[1], out int qty))
            {
                return(new Error($"Could not parse line quantity: {line}"));
            }

            bool purch = cells[2].Contains("P");
            bool gp    = cells[2].Contains("G");
            bool ccr   = cells[2].Contains("C");

            var pn = PartNumber.Parse(cells[3]);

            var desc = PartDescription.Parse(cells[4]);

            return(pn
                   .CombineWith(desc)
                   .CombineWith(pos)
                   .Match(
                       ok => new SisLine(ok.Item1, ok.Item2, ok.Item3, qty, purch, gp, ccr),
                       err => Result.Error <SisLine, Error>(err)));
        }
예제 #2
0
        public static Result <SisGroup, Error> DeserializeSisGroup(string text)
        {
            var lines = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            if (lines.Length < 1)
            {
                return(new Error());
            }
            var firstLine = lines[0].Split('\t');

            if (firstLine.Length < 3)
            {
                return(new Error("Could not separate group description from group number."));
            }

            var groupNumber = PartNumber.Parse(firstLine[1]);
            var groupDesc   = PartDescription.Parse(firstLine[2]);

            var lineResults = lines
                              .Skip(1) // skip header line with group information
                              .Select(txt => DeserializeSisLine(txt))
                              .ToList();

            // fail if any lines could not be deserialized
            if (lineResults.Any(ln => ln.IsError))
            {
                return(lineResults.First(ln => ln.IsError).ErrorOrThrow());
            }

            var sisLines = lineResults
                           .Select(ln => ln.ValueOrThrow()) // all results verified
                           .ToList();

            return(groupNumber
                   .CombineWith(groupDesc)
                   .Map(t => new SisGroup(t.Item1, t.Item2, sisLines)));
        }