Files
anki-static/AnkiStatic/Verify.fs
patrick 4070001e55
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
Add app (#4)
Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk>
Reviewed-on: #4
2023-09-08 23:31:01 +00:00

65 lines
1.8 KiB
Forth

namespace AnkiStatic.App
open System
open System.IO
open System.Threading.Tasks
open Argu
open NJsonSchema
open NJsonSchema.Validation
open AnkiStatic
type VerifyArgsFragment =
| [<MainCommand>] Input of string
interface IArgParserTemplate with
member s.Usage =
match s with
| Input _ -> "path to the file to be verified, or the literal '-' to read from stdin"
type VerifyArgs =
| File of FileInfo
| Stdin
static member OfParse (parsed : ParseResults<VerifyArgsFragment>) : Result<VerifyArgs, ArguParseException> =
let input =
try
parsed.GetResult VerifyArgsFragment.Input |> Ok
with :? ArguParseException as e ->
Error e
input
|> Result.map (fun input ->
if input = "-" then
VerifyArgs.Stdin
else
VerifyArgs.File (FileInfo input)
)
[<RequireQualifiedAccess>]
module Verify =
let run (args : VerifyArgs) : Task<int> =
task {
let validator = JsonSchemaValidator ()
use schema = AnkiStatic.getSchema ()
let! ct = Async.CancellationToken
let! schema = JsonSchema.FromJsonAsync (schema, ct) |> Async.AwaitTask
use jsonStream =
match args with
| VerifyArgs.Stdin -> Console.OpenStandardInput ()
| VerifyArgs.File f -> f.OpenRead ()
let reader = new StreamReader (jsonStream)
let! json = reader.ReadToEndAsync ct |> Async.AwaitTask
let errors = validator.Validate (json, schema)
if errors.Count = 0 then
return 0
else
for error in errors do
Console.Error.WriteLine (sprintf "Error: %+A" error)
return 1
}