-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Moryx.AbstractionLayer.Drivers; | ||
using System.Threading.Tasks; | ||
|
||
namespace Moryx.Drivers.Camera.Interfaces | ||
{ | ||
/// <summary> | ||
/// Interface for camera devices, that provide image data | ||
/// </summary> | ||
public interface ICameraDriver<TImage> : IDriver where TImage : class | ||
{ | ||
/// <summary> | ||
/// Registers an ICameraDriverListener that should be provided | ||
/// with images. | ||
/// </summary> | ||
void Register(ICameraDriverListener<TImage> listener); | ||
|
||
/// <summary> | ||
/// Unregisters an ICameraDriverListener | ||
/// </summary> | ||
void Unregister(ICameraDriverListener<TImage> listener); | ||
|
||
/// <summary> | ||
/// Capture a single image from the camera | ||
/// </summary> | ||
/// <returns> | ||
/// The image that was captured or null in case no image | ||
/// could be retrieved | ||
/// </returns> | ||
Task<TImage?> CaptureImage(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Moryx.AbstractionLayer.Drivers; | ||
using System.Threading.Tasks; | ||
|
||
namespace Moryx.Drivers.Camera.Interfaces | ||
{ | ||
/// <summary> | ||
/// Interface for objects that register as listeners to camera drivers | ||
/// </summary> | ||
public interface ICameraDriverListener<T> where T : class | ||
{ | ||
/// <summary> | ||
/// Invoked, when a new image is received by the camera | ||
/// </summary> | ||
/// <param name="image"></param> | ||
/// <returns></returns> | ||
Task OnImage(T image); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using System; | ||
|
||
namespace Moryx.Tools.FunctionResult | ||
{ | ||
/// <summary> | ||
/// Generic type that allows functions to always return a proper result, | ||
/// that either contains a valid value or an error and helps exercising | ||
/// error handling. | ||
/// </summary> | ||
/// <typeparam name="TResult"></typeparam> | ||
public class FunctionResult<TResult> | ||
{ | ||
/// <summary> | ||
/// Result value in case of success | ||
/// </summary> | ||
public TResult? Result { get; } = default; | ||
Check warning on line 19 in src/Moryx/Tools/FunctionResult.cs GitHub Actions / Build / Build
|
||
|
||
/// <summary> | ||
/// Error in case of failure | ||
/// </summary> | ||
public FunctionResultError? Error { get; } = null; | ||
Check warning on line 24 in src/Moryx/Tools/FunctionResult.cs GitHub Actions / Build / Build
|
||
|
||
/// <summary> | ||
/// Indicates if the result contains a valid value | ||
/// or not | ||
/// </summary> | ||
public bool Success => Error == null; | ||
|
||
/// <summary> | ||
/// Creates a result with a value | ||
/// </summary> | ||
/// <param name="result"></param> | ||
public FunctionResult(TResult result) | ||
{ | ||
Result = result; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an error result with <see cref="FunctionResultError"/> | ||
/// </summary> | ||
/// <param name="error"></param> | ||
public FunctionResult(FunctionResultError error) | ||
{ | ||
Error = error; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() | ||
{ | ||
return Success | ||
? Result?.ToString() ?? "null" | ||
: Error!.ToString(); | ||
} | ||
|
||
/// <summary> | ||
/// Process result value and errors in a 'pattern matching '-like way | ||
/// </summary> | ||
/// <param name="success">Function to be excecuted in case of success</param> | ||
/// <param name="error">Function to be excecuted in case of an error</param> | ||
/// <returns><see cref="FunctionResult{TResult}" /> of the executed function</returns> | ||
public FunctionResult<TResult> Match(Func<TResult, FunctionResult<TResult>> success, Func<FunctionResultError, FunctionResult<TResult>> error) | ||
=> Success ? success(Result!) : error(Error!); | ||
|
||
/// <summary> | ||
/// Process result value and errors in a 'pattern matching '-like way | ||
/// </summary> | ||
/// <param name="success">Action to be excecuted in case of success</param> | ||
/// <param name="error">Action to be excecuted in case of an error</param> | ||
/// <returns>The current <see cref="FunctionResult{TResult}" /></returns> | ||
public FunctionResult<TResult> Match(Action<TResult> success, Action<FunctionResultError> error) | ||
=> Match( | ||
s => | ||
{ | ||
success(s); | ||
return this; | ||
}, | ||
e => | ||
{ | ||
error(e); | ||
return this; | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="FunctionResult"/> of type <see cref="Nothing"/> to be used | ||
/// for functions that would return <see cref="void"/> | ||
/// </summary> | ||
public class FunctionResult : FunctionResult<Nothing> | ||
{ | ||
/// <summary> | ||
/// Creates a `successful` <see cref="FunctionResult"/> with 'no' value | ||
/// <typeparam name="TResult"></typeparam> | ||
public FunctionResult() : base(new Nothing()) | ||
Check warning on line 96 in src/Moryx/Tools/FunctionResult.cs GitHub Actions / Build / Build
Check warning on line 96 in src/Moryx/Tools/FunctionResult.cs GitHub Actions / IntegrationTests / IntegrationTests
|
||
{ | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Creates an error result with <see cref="FunctionResultError"/> | ||
/// </summary> | ||
/// <param name="error"></param> | ||
public FunctionResult(FunctionResultError error) : base(error) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Helper to create an Ok <see cref="FunctionResult"/> in a descriptive way. | ||
/// </summary> | ||
/// <returns><see cref="FunctionResult"/></returns> | ||
public static FunctionResult Ok() | ||
=> new FunctionResult(); | ||
|
||
/// <summary> | ||
/// Helper to create a <see cref="FunctionResult"/> with an error message in a descriptive way. | ||
/// </summary> | ||
/// <returns><see cref="FunctionResult"/></returns> | ||
public static FunctionResult WithError(string message) | ||
=> new(new FunctionResultError(message)); | ||
|
||
/// <summary> | ||
/// Helper to create a <see cref="FunctionResult"/> with an <see cref="Exception"/> in a descriptive way. | ||
/// </summary> | ||
/// <returns><see cref="FunctionResult"/></returns> | ||
public static FunctionResult WithError(Exception exception) | ||
=> new(new FunctionResultError(exception)); | ||
|
||
/// <summary> | ||
/// Helper to create an Ok <see cref="FunctionResult"/> in a descriptive way. | ||
/// </summary> | ||
/// <returns><see cref="FunctionResult"/> of <see cref="TResult"/></returns> | ||
public static FunctionResult<TResult> Ok<TResult>(TResult result) | ||
=> new(result); | ||
|
||
/// <summary> | ||
/// Helper to create a <see cref="FunctionResult{TResult}"/> with an error message in a descriptive way. | ||
/// </summary> | ||
/// <returns><see cref="FunctionResult{TResult}"/></returns> | ||
public static FunctionResult<TResult> WithError<TResult>(string message) | ||
=> new(new FunctionResultError(message)); | ||
|
||
/// <summary> | ||
/// Helper to create an Ok <see cref="FunctionResult"/> in a descriptive way. | ||
/// </summary> | ||
/// <returns><see cref="FunctionResult{TResult}"/></returns> | ||
public static FunctionResult<TResult> WithError<TResult>(Exception exception) | ||
=> new(new FunctionResultError(exception)); | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Holds a description of the error and optionally an | ||
/// <see cref="Exception"/> | ||
public class FunctionResultError | ||
Check warning on line 156 in src/Moryx/Tools/FunctionResult.cs GitHub Actions / Build / Build
Check warning on line 156 in src/Moryx/Tools/FunctionResult.cs GitHub Actions / IntegrationTests / IntegrationTests
|
||
{ | ||
/// <summary> | ||
/// Error message | ||
/// </summary> | ||
public string Message { get; } | ||
|
||
/// <summary> | ||
/// Exception that might be the reason for the error | ||
/// </summary> | ||
public Exception? Exception { get; } = null; | ||
Check warning on line 166 in src/Moryx/Tools/FunctionResult.cs GitHub Actions / Build / Build
|
||
|
||
|
||
/// <summary> | ||
/// Creates an error with error message | ||
/// </summary> | ||
/// <exception cref="ArgumentNullException">In case of <paramref name="message"/> is null</exception> | ||
public FunctionResultError(string message) | ||
{ | ||
if (message is null) | ||
throw new ArgumentNullException(nameof(message)); | ||
|
||
Message = message; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an error with error message | ||
/// </summary> | ||
/// <exception cref="ArgumentNullException">In case of <paramref name="exception"/> is null</exception> | ||
public FunctionResultError(Exception exception) | ||
{ | ||
if (exception is null) | ||
throw new ArgumentNullException(nameof(exception)); | ||
|
||
Message = exception.Message; | ||
Exception = exception; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() | ||
{ | ||
return Exception != null | ||
? Exception.Message | ||
: Message; | ||
} | ||
|
||
} | ||
|
||
|
||
/// <summary> | ||
/// Placeholder type to return nothing when for example <see cref="void"/> | ||
/// would be returned | ||
/// </summary> | ||
public class Nothing | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Extensions for <see cref="FunctionResult{TResult}"/> | ||
/// </summary> | ||
public static class FunctionResultExtensions | ||
{ | ||
/// <summary> | ||
/// Executes the provided function in case of a successful result | ||
/// </summary> | ||
/// <returns><see cref=" FunctionResult{TResult}"/> returned by <paramref name="func"/></returns> | ||
public static FunctionResult<TResult> Then<TResult>(this FunctionResult<TResult> result, Func<TResult, FunctionResult<TResult>> func) | ||
=> result.Match(func, _ => result); | ||
|
||
/// <summary> | ||
/// Executes the provided function in case of an error result | ||
/// </summary> | ||
/// <returns><see cref=" FunctionResult{TResult}"/> returned by <paramref name="func"/></returns> | ||
public static FunctionResult<TResult> Catch<TResult>(this FunctionResult<TResult> result, Func<FunctionResultError, FunctionResult<TResult>> func) | ||
=> result.Match(_ => result, func); | ||
|
||
/// <summary> | ||
/// Executes the provided action in case of a successful result | ||
/// </summary> | ||
/// <returns>The underlying <see cref="FunctionResult{TResult}"/></returns> | ||
public static FunctionResult<TResult> Then<TResult>(this FunctionResult<TResult> result, Action<TResult> action) | ||
=> result.Match(action, _ => { }); | ||
|
||
/// <summary> | ||
/// Executes the provided action in case of a error result | ||
/// </summary> | ||
/// <returns>The underlying <see cref="FunctionResult{TResult}"/></returns> | ||
public static FunctionResult<TResult> Catch<TResult>(this FunctionResult<TResult> result, Action<FunctionResultError> action) | ||
=> result.Match(_ => { }, action); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG | ||
// Licensed under the Apache License, Version 2.0 | ||
|
||
using NUnit.Framework; | ||
|
||
namespace Moryx.Tests.Tools | ||
{ | ||
[TestFixture] | ||
public class FunctionResultTestsBase | ||
{ | ||
protected const string Message = "Error occured!"; | ||
protected const string ExceptionMessage = "Exception Message"; | ||
} | ||
} |