Code/AutoRig/Vast/VastSettings.cs

Data class for user-adjustable 'Vast' cloud-rigging settings. Stores port, disk size, docker image and timeouts, can serialize/deserialize to JSON and applies values to VastProtocol statics with clamping.

using System.Text.Json;

namespace AutoRig.Vast;

/// <summary>
/// User-adjustable cloud-rigging settings (the editor's gear dialog persists
/// them as JSON). Pure data + parsing here so it stays unit-testable; the
/// editor side owns file IO.
/// </summary>
public sealed class VastSettings
{
    public int ServerPort { get; set; } = 8188;
    public int DiskGb { get; set; } = 32;
    public string DockerImage { get; set; } = "pytorch/pytorch:2.4.0-cuda12.1-cudnn9-runtime";
    public int BootTimeoutMinutes { get; set; } = 12;
    public int RigTimeoutMinutes { get; set; } = 30;

    /// <summary>Pushes these values into the protocol statics the session uses.</summary>
    public void Apply()
    {
        VastProtocol.ServerPort = Math.Clamp( ServerPort, 1024, 65535 );
        VastProtocol.DiskGb = Math.Clamp( DiskGb, 16, 512 );
        if ( !string.IsNullOrWhiteSpace( DockerImage ) )
            VastProtocol.DockerImage = DockerImage.Trim();
    }

    public string Serialize() => JsonSerializer.Serialize( this,
        new JsonSerializerOptions { WriteIndented = true } );

    /// <summary>Defaults when the JSON is blank or malformed — settings must
    /// never brick the dialog.</summary>
    public static VastSettings Parse( string json )
    {
        if ( string.IsNullOrWhiteSpace( json ) )
            return new VastSettings();
        try
        {
            return JsonSerializer.Deserialize<VastSettings>( json ) ?? new VastSettings();
        }
        catch ( JsonException )
        {
            return new VastSettings();
        }
    }
}