Files

45 lines
1.2 KiB
C#

using System.Text.Json;
namespace gregExtractor;
public sealed class SnapshotStore
{
private readonly JsonSerializerOptions _jsonOptions = new()
{
WriteIndented = true,
};
public SnapshotStore(string stateDirectory)
{
StateDirectory = stateDirectory;
Directory.CreateDirectory(StateDirectory);
SnapshotPath = Path.Combine(StateDirectory, "source-snapshot.json");
ReportPath = Path.Combine(StateDirectory, "last-change-report.json");
}
public string StateDirectory { get; }
public string SnapshotPath { get; }
public string ReportPath { get; }
public SourceSnapshot? TryLoadSnapshot()
{
if (!File.Exists(SnapshotPath))
return null;
string json = File.ReadAllText(SnapshotPath);
return JsonSerializer.Deserialize<SourceSnapshot>(json, _jsonOptions);
}
public void SaveSnapshot(SourceSnapshot snapshot)
{
string json = JsonSerializer.Serialize(snapshot, _jsonOptions);
File.WriteAllText(SnapshotPath, json);
}
public void SaveReport(ChangeReport report)
{
string json = JsonSerializer.Serialize(report, _jsonOptions);
File.WriteAllText(ReportPath, json);
}
}