Exemple #1
0
 /// <summary>
 /// Converts the value of <see cref="Nullable{T}"/> to the <see cref="Result{T}"/> type.
 /// If the value is null, the Error case is yielded.
 /// Otherwise Ok case with provided value is returned.
 /// </summary>
 /// <typeparam name="T">Type of the encapsulated value.</typeparam>
 /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
 /// <param name="error">Details of the error if the value is null.</param>
 public static Result <T> OfNullable <T>(T?value, ErrorDetails error) where T : struct =>
 value.HasValue ? Ok(value.Value) : Error <T>(error);
Exemple #2
0
        /* Converters */

        /// <summary>
        /// Converts the value of class T to the <see cref="Result{T}"/> type.
        /// If the value is null, the Error case is yielded.
        /// Otherwise Ok case with provided value is returned.
        /// </summary>
        /// <typeparam name="T">Type of the encapsulated value.</typeparam>
        /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
        /// <param name="error">Details of the error if the value is null.</param>
        public static Result <T> OfObject <T>(T value, ErrorDetails error) where T : class =>
        value != null?Ok(value) : Error <T>(error);
Exemple #3
0
 /// <summary>
 /// Converts the value of class T to the <see cref="Result{T}"/> type.
 /// If the value is null, the Error case is yielded.
 /// Otherwise Ok case with provided value is returned.
 /// </summary>
 /// <remarks>Extension method variant of <see cref="Result.OfObject{T}(T, ErrorDetails)"/></remarks>
 /// <typeparam name="T">Type of the encapsulated value.</typeparam>
 /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
 /// <param name="error">Details of the error if the value is null.</param>
 public static Result <T> ToResult <T>(this T value, ErrorDetails error) where T : class => OfObject(value, error);
Exemple #4
0
 /// <summary>
 /// Tries to get the elements with the given <paramref name="key"/> from the <paramref name="lookup"/>.
 /// If any value is found, returns Ok case of the <see cref="Result{T}"/> type with the values from the lookup.
 /// Otherwise returns Error case of the <see cref="Result{T}"/> type.
 /// </summary>
 /// <typeparam name="TKey">Type of the key in the lookup.</typeparam>
 /// <typeparam name="TValue">Type of the value in the lookup.</typeparam>
 /// <param name="lookup">The lookup to search in.</param>
 /// <param name="key">The key to look for.</param>
 /// <param name="error">Details of the error if the key is not found.</param>
 public static Result <IEnumerable <TValue> > TryGetResult <TKey, TValue>(this ILookup <TKey, TValue> lookup, TKey key, ErrorDetails error) =>
 lookup.Contains(key) ? Ok(lookup[key]) : Error <IEnumerable <TValue> >(error);
Exemple #5
0
 /// <summary>
 /// Creates the Error case instance of the <see cref="Result{T}"/> type, containing error instead of value.
 /// </summary>
 /// <typeparam name="T">Desired type parameter for <see cref="Result{T}"/> type.</typeparam>
 /// <param name="error">Details of the error.</param>
 public static Result <T> Error <T>(ErrorDetails error) => new Result <T>(error);
Exemple #6
0
 /// <summary>
 /// Converts the value of <see cref="Option{T}"/> to the <see cref="Result{T}"/> type.
 /// If the value is None case, the Error case is yielded.
 /// Otherwise Ok case with provided value is returned.
 /// </summary>
 /// <remarks>Extension method variant of <see cref="Result.OfOption{T}(Option{T}, ErrorDetails)"/></remarks>
 /// <typeparam name="T">Type of the encapsulated value.</typeparam>
 /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
 /// <param name="error">Details of the error if the value is None.</param>
 public static Result <T> ToResult <T>(this Option <T> value, ErrorDetails error) where T : struct => OfOption(value, error);
Exemple #7
0
        /* TryGetResult */

        /// <summary>
        /// Tries to get the element with the given <paramref name="key"/> from the <paramref name="dictionary"/>.
        /// If the value is found, returns Ok case of the <see cref="Result{T}"/> type with the value from the dictionary.
        /// Otherwise returns Error case of the <see cref="Result{T}"/> type.
        /// </summary>
        /// <typeparam name="TKey">Type of the key in the dictionary.</typeparam>
        /// <typeparam name="TValue">Type of the value in the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to search in.</param>
        /// <param name="key">The key to look for.</param>
        /// <param name="error">Details of the error if the key is not found.</param>
        public static Result <TValue> TryGetResult <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key, ErrorDetails error) =>
        dictionary.TryGetValue(key, out TValue value) ? Ok(value) : Error <TValue>(error);
Exemple #8
0
 /// <summary>
 /// Converts the value of <see cref="Option{T}"/> to the <see cref="Result{T}"/> type.
 /// If the value is None case, the Error case is yielded.
 /// Otherwise Ok case with provided value is returned.
 /// </summary>
 /// <typeparam name="T">Type of the encapsulated value.</typeparam>
 /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
 /// <param name="error">Details of the error if the value is None.</param>
 public static Result <T> OfOption <T>(Option <T> value, ErrorDetails error) where T : struct =>
 value.IsSome ? Ok(value.Value) : Error <T>(error);
Exemple #9
0
 /// <summary>
 /// Converts the string value to the <see cref="Result{T}"/> type.
 /// If the value is null or empty string, the Error case is yielded.
 /// Otherwise Ok case with provided value is returned.
 /// </summary>
 /// <remarks>Extension method variant of <see cref="Result.OfString(string, ErrorDetails)"/></remarks>
 /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
 /// <param name="error">Details of the error if the value is null or empty.</param>
 public static Result <string> ToResult(this string value, ErrorDetails error) => OfString(value, error);
Exemple #10
0
 /// <summary>
 /// Converts the string value to the <see cref="Result{T}"/> type.
 /// If the value is null or empty string, the Error case is yielded.
 /// Otherwise Ok case with provided value is returned.
 /// </summary>
 /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
 /// <param name="error">Details of the error if the value is null or empty.</param>
 public static Result <string> OfString(string value, ErrorDetails error) =>
 string.IsNullOrEmpty(value) ? Error <string>(error) : Ok(value);
Exemple #11
0
 /// <summary>
 /// Converts the value of <see cref="Nullable{T}"/> to the <see cref="Result{T}"/> type.
 /// If the value is null, the Error case is yielded.
 /// Otherwise Ok case with provided value is returned.
 /// </summary>
 /// <remarks>Extension method variant of <see cref="Result.OfNullable{T}(T?, ErrorDetails)"/></remarks>
 /// <typeparam name="T">Type of the encapsulated value.</typeparam>
 /// <param name="value">The value to convert to <see cref="Result{T}"/>.</param>
 /// <param name="error">Details of the error if the value is null.</param>
 public static Result <T> ToResult <T>(this T?value, ErrorDetails error) where T : struct => OfNullable(value, error);
Exemple #12
0
 internal Result(ErrorDetails error)
 {
     Value = default(T);
     Error = error;
     IsOk  = false;
 }