using System;
using System.Collections.Generic;
namespace NcProgramManager.Cnc.Models
{
/// Result wrapper for all CNC operations.
public sealed class CncResult
{
public bool Success { get; }
public T Value { get; }
public IReadOnlyList Errors { get; }
public bool WasCancelled { get; }
private CncResult(bool success, T value, IReadOnlyList errors, bool cancelled)
{
Success = success;
Value = value;
Errors = errors ?? (IReadOnlyList)Array.Empty();
WasCancelled = cancelled;
}
public static CncResult Ok(T value) =>
new CncResult(true, value, null, false);
public static CncResult Fail(IReadOnlyList errors) =>
new CncResult(false, default(T), errors, false);
public static CncResult Fail(string source, string message) =>
Fail(new[] { new CncError(0, source, message) });
public static CncResult Cancelled() =>
new CncResult(false, default(T), null, true);
public override string ToString()
{
if (WasCancelled) return $"CncResult<{typeof(T).Name}>: Cancelled";
if (!Success) return $"CncResult<{typeof(T).Name}>: Fail ({Errors.Count} errors)";
return $"CncResult<{typeof(T).Name}>: Ok";
}
}
}