public static Errorable<Stage.Builder> FromJSON(this StageRequest stm) { // Do conversions on the strings and detect any errors: var maybetreeid = TreeID.TryParse(stm.treeid ?? String.Empty); if (maybetreeid.HasErrors) return maybetreeid.Errors; Stage.Builder st = new Stage.Builder( pName: (StageName)stm.name, pTreeID: maybetreeid.Value ); return st; }
private async Task<Errorable<Stage>> getStageByName(StageName stageName) { FileInfo fiTracker = system.getStagePathByStageName(stageName); if (!fiTracker.Exists) return new StageNameDoesNotExistError(stageName); byte[] buf; int nr = 0; using (var fs = new FileStream(fiTracker.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 16384, true)) { // TODO: implement an async buffered Stream: buf = new byte[16384]; nr = await fs.ReadAsync(buf, 0, 16384).ConfigureAwait(continueOnCapturedContext: false); if (nr >= 16384) { // My, what a large tag you have! throw new NotSupportedException(); } } Stage.Builder sb = new Stage.Builder(); // Parse the Stage: using (var ms = new MemoryStream(buf, 0, nr, false)) using (var sr = new StreamReader(ms, Encoding.UTF8)) { string line = sr.ReadLine(); // Set Name: line = sr.ReadLine(); if (line == null || !line.StartsWith("name ")) return new StageParseExpectedNameError(); sb.Name = (StageName)line.Substring("name ".Length); // Set TreeID: if (line == null || !line.StartsWith("tree ")) return new StageParseExpectedTreeError(); var ecid = TreeID.TryParse(line.Substring("tree ".Length)); if (ecid.HasErrors) return ecid.Errors; sb.TreeID = ecid.Value; return (Stage)sb; } }