Files
PowerToys/src/common/PowerToys.ModuleContracts/OperationResult.cs

31 lines
974 B
C#
Raw Normal View History

2025-11-28 15:00:04 +08:00
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
2025-11-28 00:24:00 +08:00
namespace PowerToys.ModuleContracts;
/// <summary>
/// Lightweight result type for module operations.
/// </summary>
public readonly record struct OperationResult(bool Success, string? Error = null)
{
public static OperationResult Ok() => new(true, null);
public static OperationResult Fail(string error) => new(false, error);
}
/// <summary>
/// Result type with a payload.
/// </summary>
2025-11-28 15:00:04 +08:00
public readonly record struct OperationResult<T>(bool Success, T? Value, string? Error = null);
/// <summary>
/// Factory helpers for creating operation results.
/// </summary>
public static class OperationResults
2025-11-28 00:24:00 +08:00
{
2025-11-28 15:00:04 +08:00
public static OperationResult<T> Ok<T>(T value) => new(true, value, null);
2025-11-28 00:24:00 +08:00
2025-11-28 15:00:04 +08:00
public static OperationResult<T> Fail<T>(string error) => new(false, default, error);
2025-11-28 00:24:00 +08:00
}