chore: initial commit

This commit is contained in:
Stevan Freeborn
2026-03-30 12:51:19 -05:00
commit 1012decef0
26 changed files with 2619 additions and 0 deletions
+390
View File
@@ -0,0 +1,390 @@
root = true
# All files
[*]
indent_style = space
indent_size = 2
# Xml files
[*.xml]
indent_size = 2
# Xml project files
[*.{csproj,fsproj,vbproj,proj,slnx}]
indent_size = 2
# Xml config files
[*.{props,targets,config,nuspec}]
indent_size = 2
[*.json]
indent_size = 2
# C# files
[*.cs]
#### Core EditorConfig Options ####
# Indentation and spacing
indent_size = 2
tab_width = 2
# New line preferences
insert_final_newline = false
#### .NET Coding Conventions ####
[*.{cs,vb}]
# Organize usings
dotnet_separate_import_directive_groups = true
dotnet_sort_system_directives_first = true
file_header_template = unset
# this. and Me. preferences
dotnet_style_qualification_for_event = false:silent
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_method = false:silent
dotnet_style_qualification_for_property = false:silent
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
# Expression-level preferences
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_object_initializer = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_collection_expression = when_types_loosely_match:suggestion
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = false:silent
dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
# Field preferences
dotnet_style_readonly_field = true:warning
# Parameter preferences
dotnet_code_quality_unused_parameters = all:suggestion
# Suppression preferences
dotnet_remove_unnecessary_suppression_exclusions = none
#### C# Coding Conventions ####
[*.cs]
# var preferences
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
# Expression-bodied members
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_lambdas = true:suggestion
csharp_style_expression_bodied_local_functions = false:silent
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
# Pattern matching preferences
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_prefer_extended_property_pattern = true:suggestion
csharp_style_prefer_not_pattern = true:suggestion
csharp_style_prefer_pattern_matching = true:silent
csharp_style_prefer_switch_expression = true:suggestion
# Null-checking preferences
csharp_style_conditional_delegate_call = true:suggestion
# Modifier preferences
csharp_prefer_static_anonymous_function = true:suggestion
csharp_prefer_static_local_function = true:warning
csharp_preferred_modifier_order = public,private,protected,internal,file,const,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:suggestion
csharp_style_prefer_readonly_struct = true:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion
# Code-block preferences
csharp_prefer_braces = true:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = file_scoped:suggestion
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_prefer_top_level_statements = true:silent
# Expression-level preferences
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_local_over_anonymous_function = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
# 'using' directive preferences
csharp_using_directive_placement = outside_namespace:silent
#### C# Formatting Rules ####
# New line preferences
csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_open_brace = all
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = true
csharp_indent_labels = one_less_than_current
csharp_indent_switch_labels = true
# Space preferences
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
#### Naming styles ####
[*.{cs,vb}]
# Naming rules
dotnet_naming_rule.types_and_namespaces_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.types_and_namespaces_should_be_pascalcase.symbols = types_and_namespaces
dotnet_naming_rule.types_and_namespaces_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.interfaces_should_be_ipascalcase.severity = suggestion
dotnet_naming_rule.interfaces_should_be_ipascalcase.symbols = interfaces
dotnet_naming_rule.interfaces_should_be_ipascalcase.style = ipascalcase
dotnet_naming_rule.type_parameters_should_be_tpascalcase.severity = suggestion
dotnet_naming_rule.type_parameters_should_be_tpascalcase.symbols = type_parameters
dotnet_naming_rule.type_parameters_should_be_tpascalcase.style = tpascalcase
dotnet_naming_rule.methods_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.methods_should_be_pascalcase.symbols = methods
dotnet_naming_rule.methods_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.properties_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.properties_should_be_pascalcase.symbols = properties
dotnet_naming_rule.properties_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.events_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.events_should_be_pascalcase.symbols = events
dotnet_naming_rule.events_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.local_variables_should_be_camelcase.severity = suggestion
dotnet_naming_rule.local_variables_should_be_camelcase.symbols = local_variables
dotnet_naming_rule.local_variables_should_be_camelcase.style = camelcase
dotnet_naming_rule.local_constants_should_be_camelcase.severity = suggestion
dotnet_naming_rule.local_constants_should_be_camelcase.symbols = local_constants
dotnet_naming_rule.local_constants_should_be_camelcase.style = camelcase
dotnet_naming_rule.parameters_should_be_camelcase.severity = suggestion
dotnet_naming_rule.parameters_should_be_camelcase.symbols = parameters
dotnet_naming_rule.parameters_should_be_camelcase.style = camelcase
dotnet_naming_rule.public_fields_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.public_fields_should_be_pascalcase.symbols = public_fields
dotnet_naming_rule.public_fields_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.private_fields_should_be__camelcase.severity = suggestion
dotnet_naming_rule.private_fields_should_be__camelcase.symbols = private_fields
dotnet_naming_rule.private_fields_should_be__camelcase.style = _camelcase
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.severity = suggestion
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.symbols = private_static_fields
dotnet_naming_rule.private_static_fields_should_be_s_camelcase.style = s_camelcase
dotnet_naming_rule.public_constant_fields_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.public_constant_fields_should_be_pascalcase.symbols = public_constant_fields
dotnet_naming_rule.public_constant_fields_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.private_constant_fields_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.private_constant_fields_should_be_pascalcase.symbols = private_constant_fields
dotnet_naming_rule.private_constant_fields_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.symbols = public_static_readonly_fields
dotnet_naming_rule.public_static_readonly_fields_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.symbols = private_static_readonly_fields
dotnet_naming_rule.private_static_readonly_fields_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.enums_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.enums_should_be_pascalcase.symbols = enums
dotnet_naming_rule.enums_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.local_functions_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.local_functions_should_be_pascalcase.symbols = local_functions
dotnet_naming_rule.local_functions_should_be_pascalcase.style = pascalcase
dotnet_naming_rule.non_field_members_should_be_pascalcase.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascalcase.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascalcase.style = pascalcase
# Symbol specifications
dotnet_naming_symbols.interfaces.applicable_kinds = interface
dotnet_naming_symbols.interfaces.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interfaces.required_modifiers =
dotnet_naming_symbols.enums.applicable_kinds = enum
dotnet_naming_symbols.enums.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.enums.required_modifiers =
dotnet_naming_symbols.events.applicable_kinds = event
dotnet_naming_symbols.events.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.events.required_modifiers =
dotnet_naming_symbols.methods.applicable_kinds = method
dotnet_naming_symbols.methods.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.methods.required_modifiers =
dotnet_naming_symbols.properties.applicable_kinds = property
dotnet_naming_symbols.properties.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.properties.required_modifiers =
dotnet_naming_symbols.public_fields.applicable_kinds = field
dotnet_naming_symbols.public_fields.applicable_accessibilities = public, internal
dotnet_naming_symbols.public_fields.required_modifiers =
dotnet_naming_symbols.private_fields.applicable_kinds = field
dotnet_naming_symbols.private_fields.applicable_accessibilities = private, protected, protected_internal, private_protected
dotnet_naming_symbols.private_fields.required_modifiers =
dotnet_naming_symbols.private_static_fields.applicable_kinds = field
dotnet_naming_symbols.private_static_fields.applicable_accessibilities = private, protected, protected_internal, private_protected
dotnet_naming_symbols.private_static_fields.required_modifiers = static
dotnet_naming_symbols.types_and_namespaces.applicable_kinds = namespace, class, struct, interface, enum
dotnet_naming_symbols.types_and_namespaces.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types_and_namespaces.required_modifiers =
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =
dotnet_naming_symbols.type_parameters.applicable_kinds = namespace
dotnet_naming_symbols.type_parameters.applicable_accessibilities = *
dotnet_naming_symbols.type_parameters.required_modifiers =
dotnet_naming_symbols.private_constant_fields.applicable_kinds = field
dotnet_naming_symbols.private_constant_fields.applicable_accessibilities = private, protected, protected_internal, private_protected
dotnet_naming_symbols.private_constant_fields.required_modifiers = const
dotnet_naming_symbols.local_variables.applicable_kinds = local
dotnet_naming_symbols.local_variables.applicable_accessibilities = local
dotnet_naming_symbols.local_variables.required_modifiers =
dotnet_naming_symbols.local_constants.applicable_kinds = local
dotnet_naming_symbols.local_constants.applicable_accessibilities = local
dotnet_naming_symbols.local_constants.required_modifiers = const
dotnet_naming_symbols.parameters.applicable_kinds = parameter
dotnet_naming_symbols.parameters.applicable_accessibilities = *
dotnet_naming_symbols.parameters.required_modifiers =
dotnet_naming_symbols.public_constant_fields.applicable_kinds = field
dotnet_naming_symbols.public_constant_fields.applicable_accessibilities = public, internal
dotnet_naming_symbols.public_constant_fields.required_modifiers = const
dotnet_naming_symbols.public_static_readonly_fields.applicable_kinds = field
dotnet_naming_symbols.public_static_readonly_fields.applicable_accessibilities = public, internal
dotnet_naming_symbols.public_static_readonly_fields.required_modifiers = readonly, static
dotnet_naming_symbols.private_static_readonly_fields.applicable_kinds = field
dotnet_naming_symbols.private_static_readonly_fields.applicable_accessibilities = private, protected, protected_internal, private_protected
dotnet_naming_symbols.private_static_readonly_fields.required_modifiers = readonly, static
dotnet_naming_symbols.local_functions.applicable_kinds = local_function
dotnet_naming_symbols.local_functions.applicable_accessibilities = *
dotnet_naming_symbols.local_functions.required_modifiers =
# Naming styles
dotnet_naming_style.pascalcase.required_prefix =
dotnet_naming_style.pascalcase.required_suffix =
dotnet_naming_style.pascalcase.word_separator =
dotnet_naming_style.pascalcase.capitalization = pascal_case
dotnet_naming_style.ipascalcase.required_prefix = I
dotnet_naming_style.ipascalcase.required_suffix =
dotnet_naming_style.ipascalcase.word_separator =
dotnet_naming_style.ipascalcase.capitalization = pascal_case
dotnet_naming_style.tpascalcase.required_prefix = T
dotnet_naming_style.tpascalcase.required_suffix =
dotnet_naming_style.tpascalcase.word_separator =
dotnet_naming_style.tpascalcase.capitalization = pascal_case
dotnet_naming_style._camelcase.required_prefix = _
dotnet_naming_style._camelcase.required_suffix =
dotnet_naming_style._camelcase.word_separator =
dotnet_naming_style._camelcase.capitalization = camel_case
dotnet_naming_style.camelcase.required_prefix =
dotnet_naming_style.camelcase.required_suffix =
dotnet_naming_style.camelcase.word_separator =
dotnet_naming_style.camelcase.capitalization = camel_case
dotnet_naming_style.s_camelcase.required_prefix = s_
dotnet_naming_style.s_camelcase.required_suffix =
dotnet_naming_style.s_camelcase.word_separator =
dotnet_naming_style.s_camelcase.capitalization = camel_case
+23
View File
@@ -0,0 +1,23 @@
name: CI
on:
pull_request:
branches: [main]
jobs:
ci:
name: Build, Format, and Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Check formatting
run: dotnet format --verify-no-changes
- name: Run tests
run: dotnet test --no-build --verbosity normal
+122
View File
@@ -0,0 +1,122 @@
name: Release
on:
push:
branches: [main]
jobs:
release:
name: Version, Build, and Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10.0.x
- name: Determine version
id: version
run: |
$commits = git log main..HEAD --format="%s"
$commitList = $commits -split "`n"
$major = 0
$minor = 0
$patch = 0
foreach ($message in $commitList) {
if ($message -match " BREAKING CHANGE|!:") {
$major = 1
} elseif ($message -match "^feat(\(.*\))?:") {
$minor = 1
} elseif ($message -match "^fix(\(.*\))?:") {
$patch = 1
}
}
if ($major -eq 1) {
$minor = 0
$patch = 0
} elseif ($minor -eq 1) {
$patch = 0
} elseif ($patch -eq 0 -and $commitList.Count -gt 0 -and $commitList[0]) {
$patch = 1
}
$currentVersion = "0.0.0"
$tag = git describe --tags --abbrev=0 2>$null
$hasExistingTag = $false
if ($tag) {
$hasExistingTag = $true
$currentVersion = $tag -replace "^v", ""
}
$parts = $currentVersion.Split(".")
$majorCurrent = [int]$parts[0]
$minorCurrent = [int]$parts[1]
$patchCurrent = [int]$parts[2]
$majorNew = $majorCurrent + $major
$minorNew = if ($major -eq 1) { 0 } else { $minorCurrent + $minor }
$patchNew = if ($major -eq 1 -or $minor -eq 1) { 0 } else { $patchCurrent + $patch }
if ($majorNew -eq $majorCurrent -and $minorNew -eq $minorCurrent -and $patchNew -eq $patchCurrent) {
if (-not $hasExistingTag) {
$version = "0.0.0"
echo "VERSION=$version" >> $env:GITHUB_OUTPUT
Write-Host "First release: publishing version 0.0.0"
} else {
Write-Host "No version bump needed, skipping release"
exit 0
}
} else {
$version = "$majorNew.$minorNew.$patchNew"
echo "VERSION=$version" >> $env:GITHUB_OUTPUT
}
shell: pwsh
- name: Update version in csproj
if: steps.version.outputs.VERSION
run: |
$version = "${{ steps.version.outputs.VERSION }}"
$csprojPath = "src/StevanFreeborn.Results/StevanFreeborn.Results.csproj"
$content = Get-Content $csprojPath -Raw
if ($content -match "<Version>.*?</Version>") {
$content = $content -replace "<Version>.*?</Version>", "<Version>$version</Version>"
} elseif ($content -match "<PropertyGroup>") {
$content = $content -replace "<PropertyGroup>", "<PropertyGroup>`n <Version>$version</Version>"
}
Set-Content -Path $csprojPath -Value $content
- name: Create commit
if: steps.version.outputs.VERSION
run: |
git config user.name "gitea-actions[bot]"
git config user.email "gitea-actions[bot]@users.noreply.gitea.io"
git add -A
git commit -m "Bump version to ${{ steps.version.outputs.VERSION }}" || echo "No changes to commit"
- name: Create tag
if: steps.version.outputs.VERSION
run: |
git tag "v${{ steps.version.outputs.VERSION }}"
- name: Push changes and tags
if: steps.version.outputs.VERSION
run: |
git push origin main
git push origin v${{ steps.version.outputs.VERSION }}
- name: Restore dependencies
if: steps.version.outputs.VERSION
run: dotnet restore
- name: Build
if: steps.version.outputs.VERSION
run: dotnet build --configuration Release --no-restore
- name: Run tests
if: steps.version.outputs.VERSION
run: dotnet test --configuration Release --no-build --verbosity normal
- name: Pack
if: steps.version.outputs.VERSION
run: dotnet pack --configuration Release --no-build --output ./artifacts /p:PackageVersion=${{ steps.version.outputs.VERSION }}
- name: Push to NuGet
if: steps.version.outputs.VERSION
run: dotnet nuget push ./artifacts/*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_API_KEY }}
+482
View File
@@ -0,0 +1,482 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from `dotnet new gitignore`
# dotenv files
.env
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET
project.lock.json
project.fragment.lock.json
artifacts/
# Tye
.tye/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
# but not Directory.Build.rsp, as it configures directory-level build defaults
!Directory.Build.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
.idea/
##
## Visual studio for Mac
##
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# Vim temporary swap files
*.swp
+9
View File
@@ -0,0 +1,9 @@
{
"cSpell.words": [
"accessibilities",
"ipascalcase",
"pascalcase",
"slnx",
"tpascalcase"
]
}
+12
View File
@@ -0,0 +1,12 @@
<Project>
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>All</AnalysisMode>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
</Project>
+19
View File
@@ -0,0 +1,19 @@
# Copyright (c) 2026 Stevan Freeborn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+284
View File
@@ -0,0 +1,284 @@
# StevanFreeborn.Results
[![NuGet](https://img.shields.io/nuget/v/StevanFreeborn.Results.svg)](https://www.nuget.org/packages/StevanFreeborn.Results)
[![NuGet](https://img.shields.io/nuget/dt/StevanFreeborn.Results.svg)](https://www.nuget.org/packages/StevanFreeborn.Results)
[![Build](https://github.com/StevanFreeborn/stevanfreeborn.results/actions/workflows/ci.yaml/badge.svg)](https://github.com/StevanFreeborn/stevanfreeborn.results/actions/workflows/ci.yaml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
A minimalistic, AOT-compatible Result type library with support for custom error types, for railway-oriented programming and functional error handling in .NET.
## Features
- **Railway-Oriented Programming**: Chain operations that can fail without nested try-catch blocks
- **Custom Error Types**: Define your own error types that implement `IError`
- **AOT Compatible**: Works with Native AOT and trimming
- **Nullable Reference Types**: Full support for nullable reference types
- **No External Dependencies**: Lightweight with zero dependencies
- **Comprehensive XML Documentation**: Full IntelliSense support
## Installation
Install via NuGet:
```bash
dotnet add package StevanFreeborn.Results
```
## Quick Start
```csharp
using StevanFreeborn.Results;
// Create a successful result
Result<Unit, Error> ok = Result<Unit, Error>.Ok(default);
// Create a failed result
Result<Unit, Error> fail = Result<Unit, Error>.Fail(new Error("NotFound", "User not found"));
// Work with results that have values
Result<int, Error> divisionResult = Divide(10, 2);
if (divisionResult.IsSuccess)
{
Console.WriteLine(divisionResult.Value); // 5
}
// Railway-oriented programming with Bind
Result<User, Error> GetUser(int id) => ...;
Result<Order, Error> GetOrder(int orderId) => ...;
// Chain operations without exceptions
Result<Order, Error> GetUserOrder(int userId, int orderId)
{
return GetUser(userId)
.Bind(user => GetOrder(user.OrderId))
.Map(order => order.WithUserDetails(user));
}
```
## Custom Error Types
One of the key features is the ability to define your own error types:
```csharp
// Define a custom error
public record DomainError(string Code, string Message) : IError;
// Use with Result
Result<User, DomainError> GetUser(int id)
{
if (id <= 0)
{
return Result<User, DomainError>.Fail(new DomainError("InvalidId", "User ID must be positive"));
}
// ... fetch user
return Result<User, DomainError>.Ok(user);
}
// Chain with custom errors
Result<Order, DomainError> result = GetUser(1)
.Bind(user => GetOrder(user.OrderId))
.MapError(e => new DomainError(e.Code, $"Failed to get order: {e.Message}"));
```
## IError Interface
The `IError` interface is the foundation for custom error types:
```csharp
public interface IError
{
string Code { get; }
string Message { get; }
}
```
Any type implementing `IError` can be used with `Result<T, TError>`.
## Built-in Error Class
The library includes a built-in `Error` class that implements `IError`:
```csharp
// Create an error
var error = new Error("NotFound", "User not found");
// With metadata
var errorWithMetadata = new Error(
"ValidationFailed", "Invalid input",
new Dictionary<string, object> { { "Field", "email" } }
);
```
## Unit Type
The `Unit` struct represents a void-like type for use when no value is needed:
```csharp
// For operations that only succeed or fail without a value
Result<Unit, Error> operation = DoSomething();
```
## Result Types
### `Result<T, TError>`
The main Result type with generic type parameters for both value and error:
```csharp
// Creation
Result<int, Error> ok = Result<int, Error>.Ok(42);
Result<int, Error> fail = Result<int, Error>.Fail(new Error("Invalid", "Invalid input"));
// Check status
if (result.IsSuccess) { /* ... */ }
if (result.IsFailure) { /* ... */ }
// Access value (throws on failure)
int value = result.Value;
// Access error (throws on success)
Error error = result.Error;
```
### Result without value
For operations that don't return a value, use `Unit`:
```csharp
Result<Unit, Error> ok = Result<Unit, Error>.Ok(default);
Result<Unit, Error> fail = Result<Unit, Error>.Fail(new Error("Failed", "Something went wrong"));
```
## Functional Operations
### Map
Transforms the value if success, propagates the error if failure.
```csharp
Result<int, Error> ok = Result<int, Error>.Ok(5);
Result<string, Error> mapped = ok.Map(x => x.ToString()); // Result<string, Error>.Ok("5")
```
### MapError
Transforms the error if failure, propagates the value if success.
```csharp
Result<int, Error> fail = Result<int, Error>.Fail(new Error("NotFound", "Not found"));
Result<int, Error> mapped = fail.MapError(e => new Error("Unexpected", e.Message));
```
### Bind
Chains another operation that returns a Result.
```csharp
Result<User, Error> GetUser(int id) => ...;
Result<Order, Error> GetOrder(int userId) => ...;
Result<Order, Error> GetUserOrder(int userId)
{
return GetUser(userId)
.Bind(user => GetOrder(user.Id));
}
```
### Match
Executes different functions based on success or failure.
```csharp
Result<int, Error> result = Divide(10, 2);
// Get a value
string message = result.Match(
onSuccess: value => $"Result: {value}",
onFailure: error => $"Error: {error.Message}"
);
// Execute actions
result.Match(
onSuccess: value => Console.WriteLine(value),
onFailure: error => Console.WriteLine(error.Message)
);
```
### Try
Wraps a function that may throw an exception in a Result.
```csharp
// Simple usage with default error handler
Result<string, Error> result = Result<string, Error>.Try(() => File.ReadAllText("file.txt"));
// Custom error handler
Result<string, Error> result = Result<string, Error>.Try(
() => File.ReadAllText("file.txt"),
ex => new Error("ReadError", ex.Message)
);
```
## Async Operations
The library provides async extension methods for all functional operations.
```csharp
// Async Map
Result<User, Error> user = await GetUserAsync(id);
Result<string, Error> userName = await user.MapAsync(u => GetNameAsync(u));
// Async Bind
Result<Order, Error> order = await GetUserAsync(id)
.BindAsync(user => GetOrderAsync(user.OrderId));
// Async Match
string result = await result.MatchAsync(
onSuccess: async value => await ProcessAsync(value),
onFailure: async error => await HandleErrorAsync(error)
);
// Async Try
Result<string, Error> result = await ResultAsyncExtensions.TryAsync(
() => HttpClient.GetStringAsync("https://api.example.com")
);
```
## Extension Methods
### Result<T, TError> Methods
| Method | Description |
|--------------------------------------------------|------------------------------------|
| `Map(Func<T, TNew>)` | Transforms the value |
| `Map(Func<T, TNew>, Func<TError, TNewError>)` | Transforms the value or error |
| `MapError(Func<TError, TNewError>)` | Transforms the error |
| `Bind(Func<T, Result<TNew, TError>>)` | Chains a new Result |
| `Match(Func<T, TResult>, Func<TError, TResult>)` | Pattern matching returning a value |
| `Match(Action<T>, Action<TError>)` | Pattern matching executing actions |
| `Try(Func<T>)` | Wraps a function that may throw |
### Async Extensions
| Method | Description |
|-----------------|---------------------------|
| `MapAsync` | Async version of Map |
| `MapErrorAsync` | Async version of MapError |
| `BindAsync` | Async version of Bind |
| `MatchAsync` | Async version of Match |
| `TryAsync` | Async version of Try |
## Requirements
- .NET Standard 2.1
- .NET 10.0+
## License
MIT License - see [LICENSE.md](LICENSE.md) for details.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
+8
View File
@@ -0,0 +1,8 @@
<Solution>
<Folder Name="/src/">
<Project Path="src/StevanFreeborn.Results/StevanFreeborn.Results.csproj" />
</Folder>
<Folder Name="/tests/">
<Project Path="tests/StevanFreeborn.Results.Tests/StevanFreeborn.Results.Tests.csproj" />
</Folder>
</Solution>
+9
View File
@@ -0,0 +1,9 @@
{
"sdk": {
"version": "10.0.201",
"rollForward": "latestPatch"
},
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
+6
View File
@@ -0,0 +1,6 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<ItemGroup>
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
</ItemGroup>
</Project>
+8
View File
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="10.0.201" />
</ItemGroup>
</Project>
+6
View File
@@ -0,0 +1,6 @@
[*.cs]
dotnet_diagnostic.CA1000.severity = none
dotnet_diagnostic.CA1031.severity = none
dotnet_diagnostic.CA1510.severity = none
dotnet_diagnostic.CA2225.severity = none
+40
View File
@@ -0,0 +1,40 @@
namespace StevanFreeborn.Results
{
/// <summary>
/// Represents an error with a code, message, and optional metadata.
/// </summary>
#pragma warning disable CA1716 // Identifiers should not match keywords
public sealed record Error : IError
#pragma warning restore CA1716 // Identifiers should not match keywords
{
/// <summary>
/// Gets the error code.
/// </summary>
public string Code { get; }
/// <summary>
/// Gets the error message.
/// </summary>
public string Message { get; }
/// <summary>
/// Gets optional metadata associated with the error.
/// </summary>
public IReadOnlyDictionary<string, object>? Metadata { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Error"/> class with the specified code, message, and optional metadata.
/// </summary>
/// <param name="code">The error code.</param>
/// <param name="message">The error message.</param>
/// <param name="metadata">Optional metadata associated with the error.</param>
public Error(string code, string message, IReadOnlyDictionary<string, object>? metadata = null)
{
Code = code;
Message = message;
Metadata = metadata;
}
}
}
+20
View File
@@ -0,0 +1,20 @@
namespace StevanFreeborn.Results
{
/// <summary>
/// Represents an error with a code and message.
/// </summary>
public interface IError
{
/// <summary>
/// Gets the error code.
/// </summary>
string Code { get; }
/// <summary>
/// Gets the error message.
/// </summary>
string Message { get; }
}
}
+264
View File
@@ -0,0 +1,264 @@
namespace StevanFreeborn.Results
{
/// <summary>
/// Represents a result with a value and error type, used for operations that either succeed with a value or fail with an error.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <typeparam name="TError">The type of the error.</typeparam>
public sealed class Result<T, TError> where TError : IError
{
/// <summary>
/// Gets a value indicating whether the result is successful.
/// </summary>
public bool IsSuccess { get; }
/// <summary>
/// Gets a value indicating whether the result is a failure.
/// </summary>
public bool IsFailure => !IsSuccess;
/// <summary>
/// Gets the value of the result.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when accessing Value on a failed result.</exception>
public T Value => IsSuccess
? _value
: throw new InvalidOperationException($"Cannot access Value on a failed result. Error: [{Error.Code}] {Error.Message}");
/// <summary>
/// Gets the error associated with the result.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when accessing Error on a successful result.</exception>
public TError Error => _error ?? throw new InvalidOperationException("Cannot access Error on a successful result.");
private readonly T _value;
private readonly TError? _error;
internal Result(bool isSuccess, T value, TError? error)
{
IsSuccess = isSuccess;
_value = value;
_error = error;
}
/// <summary>
/// Creates a successful result with the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A successful <see cref="Result{T, TError}"/>.</returns>
public static Result<T, TError> Ok(T value)
{
return new Result<T, TError>(true, value, default);
}
/// <summary>
/// Creates a failed result with the specified error.
/// </summary>
/// <param name="error">The error.</param>
/// <returns>A failed <see cref="Result{T, TError}"/>.</returns>
public static Result<T, TError> Fail(TError error)
{
return new Result<T, TError>(false, default!, error);
}
/// <summary>
/// Implicitly converts a value to a successful <see cref="Result{T, TError}"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator Result<T, TError>(T value)
{
return Ok(value);
}
/// <summary>
/// Implicitly converts an <see cref="IError"/> to a failed <see cref="Result{T, TError}"/>.
/// </summary>
/// <param name="error">The error to convert.</param>
public static implicit operator Result<T, TError>(TError error)
{
return Fail(error);
}
/// <summary>
/// Maps the value to a new type if the result is successful.
/// </summary>
/// <typeparam name="TNew">The new type.</typeparam>
/// <param name="mapper">The function to map the value.</param>
/// <returns>A new <see cref="Result{TNew, TError}"/> with the mapped value if successful, otherwise the current error.</returns>
/// <exception cref="ArgumentNullException">Thrown when mapper is null.</exception>
public Result<TNew, TError> Map<TNew>(Func<T, TNew> mapper)
{
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
return IsSuccess ? Result<TNew, TError>.Ok(mapper(Value)) : Result<TNew, TError>.Fail(Error);
}
/// <summary>
/// Maps the error to a new error if the result is a failure.
/// </summary>
/// <param name="mapper">The function to map the error.</param>
/// <returns>A new <see cref="Result{T, TNewError}"/> with the mapped error if failed, otherwise the current result.</returns>
/// <exception cref="ArgumentNullException">Thrown when mapper is null.</exception>
public Result<T, TNewError> MapError<TNewError>(Func<TError, TNewError> mapper) where TNewError : IError
{
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
return IsFailure ? Result<T, TNewError>.Fail(mapper(Error)) : Result<T, TNewError>.Ok(Value);
}
/// <summary>
/// Binds to a new result if the current result is successful.
/// </summary>
/// <typeparam name="TNew">The new result type.</typeparam>
/// <param name="binder">The function to bind to on success.</param>
/// <returns>The result of the binder function if successful, otherwise the current error.</returns>
/// <exception cref="ArgumentNullException">Thrown when binder is null.</exception>
public Result<TNew, TError> Bind<TNew>(Func<T, Result<TNew, TError>> binder)
{
if (binder is null)
{
throw new ArgumentNullException(nameof(binder));
}
return IsSuccess ? binder(Value) : Result<TNew, TError>.Fail(Error);
}
/// <summary>
/// Matches the result and returns a value based on success or failure.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="onSuccess">The function to execute on success.</param>
/// <param name="onFailure">The function to execute on failure.</param>
/// <returns>The result of the appropriate function.</returns>
/// <exception cref="ArgumentNullException">Thrown when onSuccess or onFailure is null.</exception>
public TResult Match<TResult>(Func<T, TResult> onSuccess, Func<TError, TResult> onFailure)
{
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
return IsSuccess ? onSuccess(Value) : onFailure(Error);
}
/// <summary>
/// Matches the result and executes the appropriate action.
/// </summary>
/// <param name="onSuccess">The action to execute on success.</param>
/// <param name="onFailure">The action to execute on failure.</param>
/// <exception cref="ArgumentNullException">Thrown when onSuccess or onFailure is null.</exception>
public void Match(Action<T> onSuccess, Action<TError> onFailure)
{
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
if (IsSuccess)
{
onSuccess(Value);
}
else
{
onFailure(Error);
}
}
/// <summary>
/// Executes the specified action if the result is successful, and returns the current result.
/// </summary>
/// <param name="action">The action to execute on success.</param>
/// <returns>The current result.</returns>
/// <exception cref="ArgumentNullException">Thrown when action is null.</exception>
public Result<T, TError> Map(Action<T> action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
if (IsSuccess)
{
action(Value);
}
return this;
}
/// <summary>
/// Executes the specified action if the result is successful, and returns the current result.
/// </summary>
/// <param name="action">The action to execute on success.</param>
/// <returns>The current result.</returns>
/// <exception cref="ArgumentNullException">Thrown when action is null.</exception>
public Result<T, TError> Map(Action action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
if (IsSuccess)
{
action();
}
return this;
}
/// <summary>
/// Executes the specified function and wraps the result in a <see cref="Result{T, TError}"/>.
/// </summary>
/// <param name="func">The function to execute.</param>
/// <returns>A successful result with the return value if no exception is thrown; otherwise, a failed result.</returns>
public static Result<T, TError> Try(Func<T> func)
{
return Try(func, ex => (TError)(IError)new Error("UnexpectedError", ex.Message));
}
/// <summary>
/// Executes the specified function and wraps the result in a <see cref="Result{T, TError}"/> using the specified error handler.
/// </summary>
/// <param name="func">The function to execute.</param>
/// <param name="errorHandler">The function to convert exceptions to errors.</param>
/// <returns>A successful result with the return value if no exception is thrown; otherwise, a failed result.</returns>
/// <exception cref="ArgumentNullException">Thrown when func or errorHandler is null.</exception>
public static Result<T, TError> Try(Func<T> func, Func<Exception, TError> errorHandler)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
try
{
return Ok(func());
}
catch (Exception ex)
{
return Fail(errorHandler(ex));
}
}
}
}
@@ -0,0 +1,290 @@
namespace StevanFreeborn.Results
{
/// <summary>
/// Provides async extension methods for <see cref="Result{Unit, Error}"/> and <see cref="Result{T, Error}"/>.
/// </summary>
public static class ResultAsyncExtensions
{
/// <summary>
/// Maps the value to a new type asynchronously if the result is successful.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <typeparam name="TNew">The new type.</typeparam>
/// <typeparam name="TError">The type of the error.</typeparam>
/// <param name="result">The result.</param>
/// <param name="mapper">The async function to map the value.</param>
/// <returns>A task containing a new <see cref="Result{TNew, TError}"/> with the mapped value if successful, otherwise the current error.</returns>
/// <exception cref="ArgumentNullException">Thrown when result or mapper is null.</exception>
public static async Task<Result<TNew, TError>> MapAsync<T, TNew, TError>(this Result<T, TError> result, Func<T, Task<TNew>> mapper)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
return result.IsSuccess ? await mapper(result.Value).ConfigureAwait(false) : result.Error;
}
/// <summary>
/// Maps the error to a new error asynchronously if the result is a failure.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <typeparam name="TError">The type of the error.</typeparam>
/// <typeparam name="TNewError">The new error type.</typeparam>
/// <param name="result">The result.</param>
/// <param name="mapper">The async function to map the error.</param>
/// <returns>A task containing a new <see cref="Result{T, TNewError}"/> with the mapped error if failed, otherwise the current result.</returns>
/// <exception cref="ArgumentNullException">Thrown when result or mapper is null.</exception>
public static async Task<Result<T, TNewError>> MapErrorAsync<T, TError, TNewError>(this Result<T, TError> result, Func<TError, Task<TNewError>> mapper)
where TError : IError
where TNewError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (mapper is null)
{
throw new ArgumentNullException(nameof(mapper));
}
return result.IsFailure ? await mapper(result.Error).ConfigureAwait(false) : Result<T, TNewError>.Ok(result.Value);
}
/// <summary>
/// Executes the specified async action if the result is successful, and returns the current result.
/// </summary>
/// <typeparam name="TError">The type of the error.</typeparam>
/// <param name="result">The result.</param>
/// <param name="onSuccess">The async action to execute on success.</param>
/// <returns>A task containing the result.</returns>
/// <exception cref="ArgumentNullException">Thrown when result or onSuccess is null.</exception>
public static async Task<Result<Unit, TError>> MapAsync<TError>(this Result<Unit, TError> result, Func<Unit, Task> onSuccess)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
if (result.IsSuccess)
{
await onSuccess(result.Value).ConfigureAwait(false);
}
return result;
}
/// <summary>
/// Binds to a new async result if the current result is successful.
/// </summary>
/// <typeparam name="TError">The type of the error.</typeparam>
/// <param name="result">The result.</param>
/// <param name="onSuccess">The async function to execute and bind to on success.</param>
/// <returns>A task containing the result of the binder function if successful, otherwise the current result.</returns>
/// <exception cref="ArgumentNullException">Thrown when result or onSuccess is null.</exception>
public static async Task<Result<Unit, TError>> BindAsync<TError>(this Result<Unit, TError> result, Func<Unit, Task<Result<Unit, TError>>> onSuccess)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
return result.IsSuccess ? await onSuccess(result.Value).ConfigureAwait(false) : result;
}
/// <summary>
/// Matches the result asynchronously and returns a value based on success or failure.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <typeparam name="TError">The type of the error.</typeparam>
/// <param name="result">The result.</param>
/// <param name="onSuccess">The async function to execute on success.</param>
/// <param name="onFailure">The async function to execute on failure.</param>
/// <returns>A task containing the result of the appropriate function.</returns>
/// <exception cref="ArgumentNullException">Thrown when result, onSuccess, or onFailure is null.</exception>
public static async Task<TResult> MatchAsync<TResult, TError>(this Result<Unit, TError> result, Func<Unit, Task<TResult>> onSuccess, Func<TError, Task<TResult>> onFailure)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
return result.IsSuccess
? await onSuccess(result.Value).ConfigureAwait(false)
: await onFailure(result.Error).ConfigureAwait(false);
}
/// <summary>
/// Executes the specified async action and wraps the result in a <see cref="Result{Unit, Error}"/>.
/// </summary>
/// <param name="action">The async action to execute.</param>
/// <returns>A task containing a successful result if no exception is thrown; otherwise, a failed result.</returns>
public static Task<Result<Unit, Error>> TryAsync(Func<Task> action)
{
return TryAsync(action, ex => new Error("UnexpectedError", ex.Message));
}
/// <summary>
/// Executes the specified async action and wraps the result in a <see cref="Result{Unit, Error}"/> using the specified error handler.
/// </summary>
/// <param name="action">The async action to execute.</param>
/// <param name="errorHandler">The function to convert exceptions to errors.</param>
/// <returns>A task containing a successful result if no exception is thrown; otherwise, a failed result.</returns>
/// <exception cref="ArgumentNullException">Thrown when action or errorHandler is null.</exception>
public static async Task<Result<Unit, Error>> TryAsync(Func<Task> action, Func<Exception, Error> errorHandler)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
try
{
await action().ConfigureAwait(false);
return Result<Unit, Error>.Ok(default);
}
catch (Exception ex)
{
return Result<Unit, Error>.Fail(errorHandler(ex));
}
}
/// <summary>
/// Binds to a new async result if the current result is successful.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <typeparam name="TNew">The new result type.</typeparam>
/// <typeparam name="TError">The type of the error.</typeparam>
/// <param name="result">The result.</param>
/// <param name="binder">The async function to bind to on success.</param>
/// <returns>A task containing the result of the binder function if successful, otherwise the current error.</returns>
/// <exception cref="ArgumentNullException">Thrown when result or binder is null.</exception>
public static async Task<Result<TNew, TError>> BindAsync<T, TNew, TError>(this Result<T, TError> result, Func<T, Task<Result<TNew, TError>>> binder)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (binder is null)
{
throw new ArgumentNullException(nameof(binder));
}
return result.IsSuccess ? await binder(result.Value).ConfigureAwait(false) : Result<TNew, TError>.Fail(result.Error);
}
/// <summary>
/// Matches the result asynchronously and returns a value based on success or failure.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <typeparam name="TError">The type of the error.</typeparam>
/// <param name="result">The result.</param>
/// <param name="onSuccess">The async function to execute on success.</param>
/// <param name="onFailure">The async function to execute on failure.</param>
/// <returns>A task containing the result of the appropriate function.</returns>
/// <exception cref="ArgumentNullException">Thrown when result, onSuccess, or onFailure is null.</exception>
public static async Task<TResult> MatchAsync<T, TResult, TError>(this Result<T, TError> result, Func<T, Task<TResult>> onSuccess, Func<TError, Task<TResult>> onFailure)
where TError : IError
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (onSuccess is null)
{
throw new ArgumentNullException(nameof(onSuccess));
}
if (onFailure is null)
{
throw new ArgumentNullException(nameof(onFailure));
}
return result.IsSuccess
? await onSuccess(result.Value).ConfigureAwait(false)
: await onFailure(result.Error).ConfigureAwait(false);
}
/// <summary>
/// Executes the specified async function and wraps the result in a <see cref="Result{T, Error}"/>.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="func">The async function to execute.</param>
/// <returns>A task containing a successful result with the return value if no exception is thrown; otherwise, a failed result.</returns>
public static Task<Result<T, Error>> TryAsync<T>(Func<Task<T>> func)
{
return TryAsync(func, ex => new Error("UnexpectedError", ex.Message));
}
/// <summary>
/// Executes the specified async function and wraps the result in a <see cref="Result{T, Error}"/> using the specified error handler.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="func">The async function to execute.</param>
/// <param name="errorHandler">The function to convert exceptions to errors.</param>
/// <returns>A task containing a successful result with the return value if no exception is thrown; otherwise, a failed result.</returns>
/// <exception cref="ArgumentNullException">Thrown when func or errorHandler is null.</exception>
public static async Task<Result<T, Error>> TryAsync<T>(Func<Task<T>> func, Func<Exception, Error> errorHandler)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
if (errorHandler is null)
{
throw new ArgumentNullException(nameof(errorHandler));
}
try
{
return Result<T, Error>.Ok(await func().ConfigureAwait(false));
}
catch (Exception ex)
{
return Result<T, Error>.Fail(errorHandler(ex));
}
}
}
}
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;net10.0</TargetFrameworks>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
<VerifyReferenceAotCompatibility>true</VerifyReferenceAotCompatibility>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup>
<PackageId>StevanFreeborn.Results</PackageId>
<Version>0.0.0</Version>
<Authors>StevanFreeborn</Authors>
<Description>A minimalistic, AOT-compatible Result type library for railway-oriented programming and functional error handling.</Description>
<PackageTags>result;railway;functional;error-handling;discriminated-union</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/StevanFreeborn/stevanfreeborn.results</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md" Link="README.md" />
<None Include="..\..\LICENSE.md" Link="LICENSE.md" />
</ItemGroup>
</Project>
+11
View File
@@ -0,0 +1,11 @@
namespace StevanFreeborn.Results
{
/// <summary>
/// Represents a void-like type for use with Result when no value is needed.
/// </summary>
public readonly struct Unit
{
}
}
+6
View File
@@ -0,0 +1,6 @@
[*.cs]
dotnet_diagnostic.CA1515.severity = none
dotnet_diagnostic.CA2007.severity = none
dotnet_diagnostic.CA2201.severity = none
dotnet_diagnostic.CA1707.severity = none
+8
View File
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="TUnit" Version="0.73.0" />
</ItemGroup>
</Project>
@@ -0,0 +1,57 @@
namespace StevanFreeborn.Results.Tests;
public class ErrorTests
{
[Test]
public async Task Constructor_WhenCalled_ItShouldSetCode()
{
var error = new Error("code", "message");
await Assert.That(error.Code).IsEqualTo("code");
}
[Test]
public async Task Constructor_WhenCalled_ItShouldSetMessage()
{
var error = new Error("code", "message");
await Assert.That(error.Message).IsEqualTo("message");
}
[Test]
public async Task Constructor_WhenCalledWithMetadata_ItShouldSetMetadata()
{
var metadata = new Dictionary<string, object> { { "key", "value" } };
var error = new Error("code", "message", metadata);
await Assert.That(error.Metadata!.Count).IsEqualTo(1);
await Assert.That(error.Metadata["key"]).IsEqualTo("value");
}
[Test]
public async Task Constructor_WhenCalledWithNullMetadata_ItShouldSetMetadataToNull()
{
var error = new Error("code", "message", null);
await Assert.That(error.Metadata!).IsNull();
}
[Test]
public async Task Equals_WhenErrorsHaveSameCodeAndMessage_ItShouldReturnTrue()
{
var error1 = new Error("code", "message");
var error2 = new Error("code", "message");
await Assert.That(error1).IsEqualTo(error2);
}
[Test]
public async Task Equals_WhenErrorsHaveDifferentCode_ItShouldReturnFalse()
{
var error1 = new Error("code1", "message");
var error2 = new Error("code2", "message");
await Assert.That(error1 == error2).IsFalse();
}
[Test]
public async Task Error_WhenCreated_ItShouldImplementIError()
{
var error = new Error("code", "message");
await Assert.That(error).IsAssignableTo<IError>();
}
}
@@ -0,0 +1,174 @@
using System.Globalization;
namespace StevanFreeborn.Results.Tests;
public class ResultAsyncExtensionsTests
{
[Test]
public async Task MapAsync_WhenResultIsSuccess_ItShouldInvokeAsyncAction()
{
var result = Result<Unit, Error>.Ok(default);
var invoked = false;
await result.MapAsync(_ => { invoked = true; return Task.CompletedTask; });
await Assert.That(invoked).IsTrue();
}
[Test]
public async Task MapAsync_WhenResultIsFailure_ItShouldSkipAsyncAction()
{
var result = Result<Unit, Error>.Fail(new Error("code", "message"));
var invoked = false;
await result.MapAsync(_ => { invoked = true; return Task.CompletedTask; });
await Assert.That(invoked).IsFalse();
}
[Test]
public async Task MapAsync_WhenCalled_ItShouldReturnOriginalResult()
{
var result = Result<Unit, Error>.Ok(default);
var mapped = await result.MapAsync(_ => Task.CompletedTask);
await Assert.That(mapped.IsSuccess).IsTrue();
}
[Test]
public async Task BindAsync_WhenResultIsSuccess_ItShouldInvokeBinderAndReturnResult()
{
var result = Result<Unit, Error>.Ok(default);
var bound = await result.BindAsync(_ => Task.FromResult(Result<Unit, Error>.Fail(new Error("new_error", "new message"))));
await Assert.That(bound.IsFailure).IsTrue();
}
[Test]
public async Task BindAsync_WhenResultIsFailure_ItShouldReturnOriginalResult()
{
var result = Result<Unit, Error>.Fail(new Error("code", "message"));
var bound = await result.BindAsync(_ => Task.FromResult(Result<Unit, Error>.Ok(default)));
await Assert.That(bound.IsFailure).IsTrue();
await Assert.That(bound.Error.Code).IsEqualTo("code");
}
[Test]
public async Task MatchAsync_WhenResultIsSuccess_ItShouldReturnOnSuccessValue()
{
var result = Result<Unit, Error>.Ok(default);
var matched = await result.MatchAsync(_ => Task.FromResult("success"), _ => Task.FromResult("failure"));
await Assert.That(matched).IsEqualTo("success");
}
[Test]
public async Task MatchAsync_WhenResultIsFailure_ItShouldReturnOnFailureValue()
{
var result = Result<Unit, Error>.Fail(new Error("code", "message"));
var matched = await result.MatchAsync(_ => Task.FromResult("success"), e => Task.FromResult(e.Code));
await Assert.That(matched).IsEqualTo("code");
}
[Test]
public async Task TryAsync_WhenActionSucceeds_ItShouldReturnOk()
{
var result = await ResultAsyncExtensions.TryAsync(() => Task.CompletedTask);
await Assert.That(result.IsSuccess).IsTrue();
}
[Test]
public async Task TryAsync_WhenActionThrowsException_ItShouldReturnFailWithUnexpectedError()
{
var result = await ResultAsyncExtensions.TryAsync(() => throw new InvalidOperationException("test error"));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("UnexpectedError");
}
[Test]
public async Task TryAsync_WhenActionThrowsException_ItShouldUseCustomError()
{
var result = await ResultAsyncExtensions.TryAsync(
() => throw new InvalidOperationException("test"),
ex => new Error("custom", ex.Message));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("custom");
}
[Test]
public async Task MapAsync_WhenResultIsSuccess_ItShouldTransformValue()
{
var result = Result<int, Error>.Ok(5);
var mapped = await result.MapAsync(async n => (await Task.FromResult(n)).ToString(CultureInfo.InvariantCulture));
await Assert.That(mapped.IsSuccess).IsTrue();
await Assert.That(mapped.Value).IsEqualTo("5");
}
[Test]
public async Task MapAsync_WhenResultIsFailure_ItShouldPropagateError()
{
var error = new Error("code", "message");
var result = Result<int, Error>.Fail(error);
var mapped = await result.MapAsync(async n => (await Task.FromResult(n)).ToString(CultureInfo.InvariantCulture));
await Assert.That(mapped.IsFailure).IsTrue();
await Assert.That(mapped.Error).IsEqualTo(error);
}
[Test]
public async Task BindAsync_WhenResultIsSuccess_ItShouldChainToBinderResult()
{
var result = Result<int, Error>.Ok(5);
var bound = await result.BindAsync(async n => await Task.FromResult(Result<string, Error>.Ok(n.ToString(CultureInfo.InvariantCulture))));
await Assert.That(bound.IsSuccess).IsTrue();
await Assert.That(bound.Value).IsEqualTo("5");
}
[Test]
public async Task BindAsync_WhenResultIsFailure_ItShouldPropagateError()
{
var error = new Error("code", "message");
var result = Result<int, Error>.Fail(error);
var bound = await result.BindAsync(async n => await Task.FromResult(Result<string, Error>.Ok(n.ToString(CultureInfo.InvariantCulture))));
await Assert.That(bound.IsFailure).IsTrue();
await Assert.That(bound.Error).IsEqualTo(error);
}
[Test]
public async Task MatchAsyncGeneric_WhenResultIsSuccess_ItShouldReturnOnSuccessValue()
{
var result = Result<string, Error>.Ok("test");
var matched = await result.MatchAsync(
v => Task.FromResult(v.Length),
_ => Task.FromResult(0));
await Assert.That(matched).IsEqualTo(4);
}
[Test]
public async Task MatchAsyncGeneric_WhenResultIsFailure_ItShouldReturnOnFailureValue()
{
var result = Result<string, Error>.Fail(new Error("code", "message"));
var matched = await result.MatchAsync(
v => Task.FromResult(v.Length),
_ => Task.FromResult(-1));
await Assert.That(matched).IsEqualTo(-1);
}
[Test]
public async Task TryAsync_WhenFuncSucceeds_ItShouldReturnOkWithValue()
{
var result = await ResultAsyncExtensions.TryAsync(async () => await Task.FromResult(42));
await Assert.That(result.IsSuccess).IsTrue();
await Assert.That(result.Value).IsEqualTo(42);
}
[Test]
public async Task TryAsync_WhenFuncThrowsException_ItShouldReturnFailWithUnexpectedError()
{
var result = await ResultAsyncExtensions.TryAsync<int>(() => throw new InvalidOperationException("test error"));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("UnexpectedError");
}
[Test]
public async Task TryAsync_WhenFuncThrowsException_ItShouldUseCustomError()
{
var result = await ResultAsyncExtensions.TryAsync<int>(
() => throw new InvalidOperationException("test"),
ex => new Error("custom", ex.Message));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("custom");
}
}
@@ -0,0 +1,183 @@
using System.Globalization;
namespace StevanFreeborn.Results.Tests;
public class ResultTTests
{
[Test]
public async Task Ok_WhenCalledWithValue_ItShouldReturnSuccessResult()
{
var result = Result<string, Error>.Ok("test");
await Assert.That(result.IsSuccess).IsTrue();
await Assert.That(result.IsFailure).IsFalse();
await Assert.That(result.Value).IsEqualTo("test");
}
[Test]
public async Task Fail_WhenCalled_ItShouldReturnFailureResult()
{
var error = new Error("code", "message");
var result = Result<string, Error>.Fail(error);
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.IsSuccess).IsFalse();
await Assert.That(result.Error).IsEqualTo(error);
}
[Test]
public async Task Value_WhenResultIsFailure_ItShouldThrowInvalidOperationException()
{
var result = Result<string, Error>.Fail(new Error("code", "message"));
Assert.Throws<InvalidOperationException>(() => _ = result.Value);
}
[Test]
public async Task Error_WhenResultIsSuccess_ItShouldThrowInvalidOperationException()
{
var result = Result<string, Error>.Ok("test");
Assert.Throws<InvalidOperationException>(() => _ = result.Error);
}
[Test]
public async Task ImplicitConversion_FromValue_ItShouldReturnSuccessResult()
{
Result<string, Error> result = "test";
await Assert.That(result.IsSuccess).IsTrue();
await Assert.That(result.Value).IsEqualTo("test");
}
[Test]
public async Task ImplicitConversion_FromError_ItShouldReturnFailureResult()
{
var error = new Error("code", "message");
Result<string, Error> result = error;
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error).IsEqualTo(error);
}
[Test]
public async Task Map_WhenResultIsSuccess_ItShouldTransformValue()
{
var result = Result<int, Error>.Ok(5);
var mapped = result.Map(n => n.ToString(CultureInfo.InvariantCulture));
await Assert.That(mapped.IsSuccess).IsTrue();
await Assert.That(mapped.Value).IsEqualTo("5");
}
[Test]
public async Task Map_WhenResultIsFailure_ItShouldPropagateError()
{
var error = new Error("code", "message");
var result = Result<int, Error>.Fail(error);
var mapped = result.Map(n => n.ToString(CultureInfo.InvariantCulture));
await Assert.That(mapped.IsFailure).IsTrue();
await Assert.That(mapped.Error).IsEqualTo(error);
}
[Test]
public async Task MapError_WhenResultIsFailure_ItShouldTransformError()
{
var originalError = new Error("original", "original message");
var result = Result<int, Error>.Fail(originalError);
var transformed = result.MapError(e => new Error("transformed", e.Message));
await Assert.That(transformed.IsFailure).IsTrue();
await Assert.That(transformed.Error.Code).IsEqualTo("transformed");
}
[Test]
public async Task MapError_WhenResultIsSuccess_ItShouldReturnUnchanged()
{
var result = Result<int, Error>.Ok(5);
var mapped = result.MapError(e => new Error("transformed", e.Message));
await Assert.That(mapped.IsSuccess).IsTrue();
await Assert.That(mapped.Value).IsEqualTo(5);
}
[Test]
public async Task Bind_WhenResultIsSuccess_ItShouldChainToBinderResult()
{
var result = Result<int, Error>.Ok(5);
var bound = result.Bind(n => Result<string, Error>.Ok(n.ToString(CultureInfo.InvariantCulture)));
await Assert.That(bound.IsSuccess).IsTrue();
await Assert.That(bound.Value).IsEqualTo("5");
}
[Test]
public async Task Bind_WhenResultIsFailure_ItShouldPropagateError()
{
var error = new Error("code", "message");
var result = Result<int, Error>.Fail(error);
var bound = result.Bind(n => Result<string, Error>.Ok(n.ToString(CultureInfo.InvariantCulture)));
await Assert.That(bound.IsFailure).IsTrue();
await Assert.That(bound.Error).IsEqualTo(error);
}
[Test]
public async Task Bind_WhenResultIsSuccess_ItShouldReturnBinderFailureResult()
{
var result = Result<int, Error>.Ok(5);
var bound = result.Bind(_ => Result<string, Error>.Fail(new Error("bind_error", "bound failed")));
await Assert.That(bound.IsFailure).IsTrue();
await Assert.That(bound.Error.Code).IsEqualTo("bind_error");
}
[Test]
public async Task Match_WhenResultIsSuccess_ItShouldReturnOnSuccessValue()
{
var result = Result<string, Error>.Ok("test");
var matched = result.Match(v => v.Length, _ => 0);
await Assert.That(matched).IsEqualTo(4);
}
[Test]
public async Task Match_WhenResultIsFailure_ItShouldReturnOnFailureValue()
{
var result = Result<string, Error>.Fail(new Error("code", "message"));
var matched = result.Match(v => v.Length, e => -1);
await Assert.That(matched).IsEqualTo(-1);
}
[Test]
public async Task Match_WhenResultIsSuccess_ItShouldInvokeOnSuccess()
{
var result = Result<string, Error>.Ok("test");
var invoked = false;
result.Match(v => { invoked = true; }, _ => { });
await Assert.That(invoked).IsTrue();
}
[Test]
public async Task Match_WhenResultIsFailure_ItShouldInvokeOnFailure()
{
var error = new Error("code", "message");
var result = Result<string, Error>.Fail(error);
var invoked = false;
result.Match(_ => { }, e => { invoked = true; });
await Assert.That(invoked).IsTrue();
}
[Test]
public async Task Try_WhenFuncSucceeds_ItShouldReturnOkWithValue()
{
var result = Result<int, Error>.Try(() => 42);
await Assert.That(result.IsSuccess).IsTrue();
await Assert.That(result.Value).IsEqualTo(42);
}
[Test]
public async Task Try_WhenFuncThrowsException_ItShouldReturnFailWithUnexpectedError()
{
var result = Result<int, Error>.Try(() => throw new InvalidOperationException("test error"));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("UnexpectedError");
}
[Test]
public async Task Try_WhenFuncThrowsException_ItShouldUseCustomError()
{
var result = Result<int, Error>.Try(() => throw new InvalidOperationException("test"), ex => new Error("custom", ex.Message));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("custom");
}
}
@@ -0,0 +1,146 @@
namespace StevanFreeborn.Results.Tests;
public class ResultTests
{
[Test]
public async Task Ok_WhenCalled_ItShouldReturnSuccessResult()
{
var result = Result<Unit, Error>.Ok(default);
await Assert.That(result.IsSuccess).IsTrue();
await Assert.That(result.IsFailure).IsFalse();
}
[Test]
public async Task Fail_WhenCalled_ItShouldReturnFailureResult()
{
var error = new Error("code", "message");
var result = Result<Unit, Error>.Fail(error);
await Assert.That(result.IsSuccess).IsFalse();
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error).IsEqualTo(error);
}
[Test]
public async Task Error_WhenResultIsSuccess_ItShouldThrowInvalidOperationException()
{
Assert.Throws<InvalidOperationException>(() => _ = Result<Unit, Error>.Ok(default).Error);
}
[Test]
public async Task ImplicitConversion_FromError_ItShouldReturnFailureResult()
{
var error = new Error("code", "message");
Result<Unit, Error> result = error;
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error).IsEqualTo(error);
}
[Test]
public async Task Map_WhenResultIsSuccess_ItShouldInvokeAction()
{
var result = Result<Unit, Error>.Ok(default);
var invoked = false;
result.Map(_ => invoked = true);
await Assert.That(invoked).IsTrue();
}
[Test]
public async Task Map_WhenResultIsFailure_ItShouldSkipAction()
{
var result = Result<Unit, Error>.Fail(new Error("code", "message"));
var invoked = false;
result.Map(_ => invoked = true);
await Assert.That(invoked).IsFalse();
}
[Test]
public async Task Map_WhenCalled_ItShouldReturnOriginalResult()
{
var result = Result<Unit, Error>.Ok(default);
var mapped = result.Map(_ => { });
await Assert.That(mapped.IsSuccess).IsTrue();
}
[Test]
public async Task Bind_WhenResultIsSuccess_ItShouldInvokeBinder()
{
var result = Result<Unit, Error>.Ok(default);
var bound = result.Bind(_ => Result<Unit, Error>.Ok(default));
await Assert.That(bound.IsSuccess).IsTrue();
}
[Test]
public async Task Bind_WhenResultIsFailure_ItShouldReturnOriginalResult()
{
var result = Result<Unit, Error>.Fail(new Error("code", "message"));
var bound = result.Bind(_ => Result<Unit, Error>.Ok(default));
await Assert.That(bound.IsFailure).IsTrue();
await Assert.That(bound.Error).IsEqualTo(result.Error);
}
[Test]
public async Task Bind_WhenResultIsSuccess_ItShouldReturnBinderResult()
{
var result = Result<Unit, Error>.Ok(default);
var bound = result.Bind(_ => Result<Unit, Error>.Fail(new Error("new_error", "new message")));
await Assert.That(bound.IsFailure).IsTrue();
}
[Test]
public async Task Match_WhenResultIsSuccess_ItShouldReturnOnSuccessValue()
{
var result = Result<Unit, Error>.Ok(default);
var matched = result.Match(_ => "success", _ => "failure");
await Assert.That(matched).IsEqualTo("success");
}
[Test]
public async Task Match_WhenResultIsFailure_ItShouldReturnOnFailureValue()
{
var result = Result<Unit, Error>.Fail(new Error("code", "message"));
var matched = result.Match(_ => "success", e => e.Code);
await Assert.That(matched).IsEqualTo("code");
}
[Test]
public async Task Match_WhenResultIsSuccess_ItShouldInvokeOnSuccess()
{
var result = Result<Unit, Error>.Ok(default);
var invoked = false;
result.Match(_ => invoked = true, _ => { });
await Assert.That(invoked).IsTrue();
}
[Test]
public async Task Match_WhenResultIsFailure_ItShouldInvokeOnFailure()
{
var error = new Error("code", "message");
var result = Result<Unit, Error>.Fail(error);
var invoked = false;
result.Match(_ => { }, e => invoked = true);
await Assert.That(invoked).IsTrue();
}
[Test]
public async Task Try_WhenActionSucceeds_ItShouldReturnOk()
{
var result = Result<Unit, Error>.Try(() => default(Unit));
await Assert.That(result.IsSuccess).IsTrue();
}
[Test]
public async Task Try_WhenActionThrowsException_ItShouldReturnFailWithUnexpectedError()
{
var result = Result<Unit, Error>.Try(() => throw new Exception("test error"));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("UnexpectedError");
}
[Test]
public async Task Try_WhenActionThrowsException_ItShouldUseCustomError()
{
var result = Result<Unit, Error>.Try(() => throw new Exception("test"), ex => new Error("custom", ex.Message));
await Assert.That(result.IsFailure).IsTrue();
await Assert.That(result.Error.Code).IsEqualTo("custom");
}
}
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>StevanFreeborn.Results.Tests</RootNamespace>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TUnit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\StevanFreeborn.Results\StevanFreeborn.Results.csproj" />
</ItemGroup>
</Project>