Skip to content

Commit

Permalink
v1.0.2.3 ❤️
Browse files Browse the repository at this point in the history
  • Loading branch information
juleswhi committed Mar 21, 2024
1 parent 5c45478 commit 44b5b88
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 17 deletions.
Empty file added docs/Flags.md
Empty file.
Empty file added docs/SuccessStates.md
Empty file.
27 changes: 27 additions & 0 deletions src/ConfigurationSystem/IConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CourseworkTooling;

namespace ConfigurationSystem;
public interface IConfig
{
static string? Path { get; set; } = "config.json";

State UpdateConfig()
{
if(Path is null)
{
return FlagFailure;
}

return FileHandler.SerializeWrite(this, Path);
}

static State GetConfig<T>()
{
if(Path is null)
{
return FlagFailure;
}

return FileHandler.ReadDeserialize<T>(Path);
}
}
9 changes: 9 additions & 0 deletions src/ExceptionSystem/ToolingExceptions/UnwrapException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ExceptionSystem.ToolingExceptions;

public class UnwrapException : Exception
{
public UnwrapException(string message) : base(message)
{
// Log exception
}
}
4 changes: 4 additions & 0 deletions src/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
global using static StateSystem.StateType;
global using State = System.Collections.Generic.Dictionary<System.Collections.Generic.IEnumerable<StateSystem.StateType>, object?>;
global using Password = (string Hashed, byte[] Salt);

// Misc
global using static CourseworkTooling.MiscHelper;
global using static CourseworkTooling.ActionHelper;
8 changes: 8 additions & 0 deletions src/MiscTools/ActionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CourseworkTooling;

public static class ActionHelper
{
public static Action EmptyAction() => () => { };
public static Action<T1> EmptyAction<T1>() => (_) => { };
public static Action<T1, T2> EmptyAction<T1, T2>() => (_, _) => { };
}
64 changes: 50 additions & 14 deletions src/MiscTools/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public static class FileHandler
/// <typeparam name="T">The type of object to serialize</typeparam>
/// <param name="data">The object to be serialized</param>
/// <param name="path">The location of where the json should be written to</param>
public static void SerializeWrite<T>(this T? data, string? path = null)
public static State SerializeWrite<T>(this T? data, string? path = null)
{
JsonString json = data.Serialize();
JsonString json = data.Serialize().Unwrap<string>();

json.Write<T>();

return FlagSuccess;
}

/// <summary>
Expand All @@ -37,18 +39,21 @@ public static void SerializeWrite<T>(this T? data, string? path = null)
/// <typeparam name="T">The type of object you want from the file</typeparam>
/// <param name="path">The path to read from</param>
/// <returns>A nullable instance of the object</returns>
public static T? ReadDeserialize<T>(this string path)
public static State ReadDeserialize<T>(this string path)
{
JsonString json = path.Read();
if (path.Read().IfNull().ElseUnwrap(out JsonString? json).IsFailure())
{
return FlagFailure;
}

T? data = json.Deserialize<T>();
T? data = json!.Deserialize<T>();

if (data is T val)
{
return val;
return val.CreateSuccess();
}

return default;
return FlagFailure;
}

/// <summary>
Expand All @@ -57,9 +62,21 @@ public static void SerializeWrite<T>(this T? data, string? path = null)
/// <typeparam name="T">The type of object to serialize</typeparam>
/// <param name="data">The actual object to serialize</param>
/// <returns>The json string of the serialized object</returns>
public static JsonString Serialize<T>(this T data)
public static State Serialize<T>(this T data)
{
return JsonConvert.SerializeObject(data);
JsonString json = string.Empty;

try
{
json = JsonConvert.SerializeObject(data);
}
catch(Exception)
{
return FlagFailure;
}

return json.CreateSuccess();

}

/// <summary>
Expand All @@ -79,28 +96,47 @@ public static JsonString Serialize<T>(this T data)
/// <typeparam name="T">If the path is left null, then this generic is used to decide on the path</typeparam>
/// <param name="data">Contents of what should be printed</param>
/// <param name="path">Where the streamwriter should open</param>
public static void Write<T>(this string data, string? path = null)
public static State Write<T>(this string data, string? path = null)
{
path ??= TypeToFilePathMap.ContainsKey(typeof(T)) ?
TypeToFilePathMap[typeof(T)] : null;

if (path is null)
{
return;
return FlagFailure;
}

using StreamWriter sw = new(path, false);

sw.WriteLine(data);
try
{
sw.WriteLine(data);
}
catch(Exception)
{
return FlagFailure;
}

return FlagSuccess;
}

/// <summary>
/// Reads a path and returns the contents
/// </summary>
/// <param name="path">The location to read</param>
/// <returns>A string of the contents</returns>
public static string Read(this string path)
public static State Read(this string path)
{
return File.ReadAllText(path);
string allText = string.Empty;
try
{
allText = File.ReadAllText(path);
}
catch(Exception)
{
return FlagFailure;
}

return allText.CreateSuccess();
}
}
2 changes: 1 addition & 1 deletion src/SSDCourseworkTooling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<LangVersion>preview</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>SSD Coursework Tooling</Title>
<Version>1.0.2.2</Version>
<Version>1.0.2.3</Version>
<Authors>Jules White</Authors>
<Company>$(Authors), FSL</Company>
<Description>A collection of tools to improve the development experience of SSD Coursework </Description>
Expand Down
195 changes: 194 additions & 1 deletion src/StateSystem/StateHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FormSystem;
using ExceptionSystem.ToolingExceptions;
using FormSystem;

namespace StateSystem;

Expand Down Expand Up @@ -187,6 +188,198 @@ public static State GlobalStateRemove(State state)
return FlagFailure;
}

public static State CreateSuccess(this object data)
{
return data.GetState(FLAG_SUCCESS);
}

/// <summary>
/// This unwraps a value, and calls an action if it was null
/// </summary>
/// <typeparam name="T">The type to cast to</typeparam>
/// <param name="state">The state to check for Success</param>
/// <param name="ifNull">The action to run if the value was null</param>
/// <returns>A Success State</returns>
public static State IfNull<T>(this State state, Action? ifNull = null)
{
ifNull ??= EmptyAction();
T? val = state.GetFirst<T>(FLAG_SUCCESS);

if(val is null)
{
ifNull();
return FlagFailure;
}

return val.CreateSuccess();
}

/// <summary>
/// This checks if a value is null, if it is then run some code, if not return a state with the non null value
/// </summary>
/// <param name="state">The state to check for success</param>
/// <param name="ifNull">The action to call if the value is null</param>
/// <returns>A Success State</returns>
public static State IfNull(this State state, Action? ifNull = null)
{
ifNull ??= EmptyAction();
object? val = state.GetFirst(FLAG_SUCCESS);

if(val is null)
{
ifNull();
return FlagFailure;
}

return val.CreateSuccess().AddState(FlagNonNull);
}

/// <summary>
/// This can be chained onto the end of a IfNull. It will unwrap only if not null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="state"></param>
/// <returns></returns>
/// <exception cref="UnwrapException"></exception>
public static State ElseUnwrap<T>(this State state, out T? obj)
{
T? a = state.GetFirst<T>(FLAG_SUCCESS);

if(a is null)
{
obj = default;
return FlagFailure;
}

if(!state.Has(FLAG_NONNULL) || state.IsFailure())
{
obj = default;
return FlagFailure;
}

obj = a!;
return FlagSuccess;
}

public static State ElseUnwrap(this State state, out object? obj)
{
object? a = state.GetFirst(FLAG_SUCCESS);

if(a is null)
{
obj = default;
return FlagFailure;
}

if(!state.Has(FLAG_NONNULL) || state.IsFailure())
{
obj = default;
return FlagFailure;
}

obj = a!;
return FlagSuccess;
}

/// <summary>
/// This gains the not-null value of a State. This will error if the value is null
/// </summary>
/// <typeparam name="T">The type to grab</typeparam>
/// <param name="state">The state to check against</param>
/// <returns>The object of type T</returns>
/// <exception cref="UnwrapException"></exception>
public static T Unwrap<T>(this State state)
{
T? val = state.GetFirst<T>(FLAG_SUCCESS);

if(val is null)
{
throw new UnwrapException($"Could not unwrap null value");
}

return val!;
}

/// <summary>
/// This gains the not-null value of a State. This will error if the value is null
/// </summary>
/// <param name="state">The state to check against</param>
/// <returns>The object found</returns>
/// <exception cref="UnwrapException"></exception>
public static object Unwrap(this State state)
{
object? val = state.GetFirst(FLAG_SUCCESS);

if(val is null)
{
throw new UnwrapException($"Could not unwrap null value");
}

return val!;
}

/// <summary>
/// Unwraps all values that have success. Will error if null.
/// </summary>
/// <typeparam name="T">The type to cast the values to</typeparam>
/// <param name="state">The state to check for Success</param>
/// <returns>An IEnumerable of the type T</returns>
/// <exception cref="UnwrapException"></exception>
public static IEnumerable<T> UnwrapAll<T>(this State state)
{
IEnumerable<T?>? val = state.Get<T>(FLAG_SUCCESS);

if(val is null || val.Any(x => x is null))
{
throw new UnwrapException($"Could not unwrap null value");
}

return val!;
}

/// <summary>
/// Unwraps all values that have success. Will error if null.
/// </summary>
/// <param name="state">The state to check for success</param>
/// <returns>An IEnumerable of objects</returns>
/// <exception cref="UnwrapException"></exception>
public static IEnumerable<object> UnwrapAll(this State state)
{
IEnumerable<object?>? val = state.Get(FLAG_SUCCESS);

if(val is null || val.Any(x => x is null))
{
throw new UnwrapException($"Could not unwrap on a null value");
}

return val!;
}



/// <summary>
/// Unwraps a value, but allows null
/// </summary>
/// <param name="state">The state to look for Success</param>
/// <returns>A nullable object</returns>
public static object? NUnwrap(this State state)
{
return state.GetFirst(FLAG_SUCCESS);
}

/// <summary>
/// Unwraps a value, but allows null
/// </summary>
/// <typeparam name="T">The type to cast to</typeparam>
/// <param name="state">The state to look for Success</param>
/// <returns></returns>
public static T? NUnwrap<T>(this State state)
{
return state.GetFirst<T>(FLAG_SUCCESS);
}

public static State FlagSuccess => StateFrom(FLAG_SUCCESS);
public static State FlagFailure => StateFrom(FLAG_FAILURE);
public static State FlagNull => StateFrom(FLAG_NULL);
public static State FlagNonNull => StateFrom(FLAG_NONNULL);
}
Loading

0 comments on commit 44b5b88

Please sign in to comment.