Skip to content

Commit

Permalink
allow message type to be exception
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Feb 15, 2023
1 parent ec31b63 commit fa28800
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 19 deletions.
10 changes: 5 additions & 5 deletions src/FsSpreadsheet/DSL/CellBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type CellBuilder() =

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (sprintf "In Cell: %s")
|> List.map (fun m -> m.MapText (sprintf "In Cell: %s"))

member inline this.Yield(n: RequiredSource<unit>) =
n
Expand Down Expand Up @@ -66,12 +66,12 @@ type CellBuilder() =
member inline this.Yield(s : string option) : SheetEntity<Value list> =
match s with
| Option.Some s -> this.Yield s
| None -> NoneRequired ["Value is missing"]
| None -> NoneRequired [message "Value is missing"]

member inline this.Yield(n: 'a option when 'a :> System.IFormattable) =
match n with
| Option.Some s -> this.Yield s
| None -> NoneRequired ["Value is missing"]
| None -> NoneRequired [message "Value is missing"]

member inline this.YieldFrom(ns: SheetEntity<Value list> seq) =
ns
Expand Down Expand Up @@ -154,15 +154,15 @@ type CellBuilder() =
| NoneRequired m -> NoneOptional m
| se -> se
with
| err -> NoneOptional [err.Message]
| err -> NoneOptional [message err.Message]

member inline this.Run(children: Expr<RequiredSource<SheetEntity<Value list>>>) =
try
match this.AsCellElement ((eval<RequiredSource<SheetEntity<Value list>>> children).Source) with
| NoneOptional m -> NoneRequired m
| se -> se
with
| err -> NoneOptional [err.Message]
| err -> NoneOptional [message err.Message]

member inline this.Run(children: Expr<SheetEntity<Value list>>) =
this.AsCellElement (eval<SheetEntity<Value list>> children)
Expand Down
6 changes: 3 additions & 3 deletions src/FsSpreadsheet/DSL/ColumnBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ColumnBuilder() =

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (sprintf "In Column: %s")
|> List.map (fun m -> m.MapText (sprintf "In Column: %s"))

member inline _.Yield(c: ColumnElement) =
SheetEntity.ok [c]
Expand Down Expand Up @@ -175,15 +175,15 @@ type ColumnBuilder() =
| NoneRequired m -> NoneOptional m
| se -> se
with
| err -> NoneOptional [err.Message]
| err -> NoneOptional [message err.Message]

member inline this.Run(children: Expr<RequiredSource<SheetEntity<ColumnElement list>>>) =
try
match (eval<RequiredSource<SheetEntity<ColumnElement list>>> children).Source with
| NoneOptional m -> NoneRequired m
| se -> se
with
| err -> NoneOptional [err.Message]
| err -> NoneOptional [message err.Message]

member inline this.Run(children: Expr<SheetEntity<ColumnElement list>>) =
(eval<SheetEntity<ColumnElement list>> children).Value
2 changes: 1 addition & 1 deletion src/FsSpreadsheet/DSL/DSL.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type DSL =
DSL.opt elem
with
| err ->
NoneOptional([err.Message])
NoneOptional([message err.Message])

/// Drops the cell with the given message
static member dropCell message : SheetEntity<Value> = NoneRequired [message]
Expand Down
4 changes: 2 additions & 2 deletions src/FsSpreadsheet/DSL/Operators.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ module Operators =
///
/// If expression does fail, returns a missing required value
let inline (!!) (v : 'T) : SheetEntity<Value> =
let f = fun s -> NoneRequired([s])
let f = fun s -> NoneRequired([message s])
parseAny f v

/// Optional value operator
///
/// If expression does fail, returns a missing optional value
let inline (!?) (v : 'T) : SheetEntity<Value> =
let f = fun s -> NoneOptional([s])
let f = fun s -> NoneOptional([message s])
parseAny f v

/// Optional operators for cell, row, column and sheet expressions
Expand Down
6 changes: 3 additions & 3 deletions src/FsSpreadsheet/DSL/RowBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type RowBuilder() =

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (sprintf "In Row: %s")
|> List.map (fun m -> m.MapText (sprintf "In Row: %s"))

member inline this.Yield(n: RequiredSource<unit>) =
n
Expand Down Expand Up @@ -163,15 +163,15 @@ type RowBuilder() =
| NoneRequired m -> NoneOptional m
| se -> se
with
| err -> NoneOptional [err.Message]
| err -> NoneOptional [message err.Message]

member inline this.Run(children: Expr<RequiredSource<SheetEntity<RowElement list>>>) =
try
match (eval<RequiredSource<SheetEntity<RowElement list>>> children).Source with
| NoneOptional m -> NoneRequired m
| se -> se
with
| err -> NoneOptional [err.Message]
| err -> NoneOptional [message err.Message]

member inline this.Run(children: Expr<SheetEntity<RowElement list>>) =
(eval<SheetEntity<RowElement list>> children).Value
2 changes: 1 addition & 1 deletion src/FsSpreadsheet/DSL/SheetBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SheetBuilder(name : string) =

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (sprintf "In Sheet %s: %s" name)
|> List.map (fun m -> m.MapText (sprintf "In Sheet %s: %s" name))

member inline _.Yield(se: SheetElement) =
SheetEntity.ok [se]
Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet/DSL/TableBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TableBuilder(name : string) =

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (sprintf "In Sheet %s: %s" name)
|> List.map (fun m -> m.MapText (sprintf "In Sheet %s: %s" name))

member inline _.Yield(se: TableElement) =
SheetEntity.ok [se]
Expand Down
58 changes: 56 additions & 2 deletions src/FsSpreadsheet/DSL/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,61 @@

open FsSpreadsheet

type Message = string
[<AutoOpen>]
type Message =
| Text of string
| Exception of exn

static member message (s : string) = Text s

static member message (e : #exn) = Exception e

member this.MapText(m : string -> string) =
match this with
| Text s -> Text (m s)
| Exception e -> this

member this.AsString() =
match this with
| Text s -> s
| Exception e -> e.Message

member this.TryText() =
match this with
| Text s -> Some s
| _ -> None

member this.TryException() =
match this with
| Exception e -> Some e
| _ -> None

member this.IsTxt =
match this with
| Text s -> true
| _ -> false

member this.IsExc =
match this with
| Text s -> true
| _ -> false


module Messages =

let format (ms : Message list) =
ms
|> List.map (fun m -> m.AsString())
|> List.reduce (fun a b -> a + ";" + b)

let fail (ms : Message list) =
let s = format ms
if ms |> List.exists (fun m -> m.IsExc) then
printfn "s"
raise (ms |> List.pick (fun m -> m.TryException()))
else
failwith s


[<AutoOpen>]
type SheetEntity<'T> =
Expand Down Expand Up @@ -30,7 +84,7 @@ module SheetEntityExtensions =
| NoneOptional ms | NoneRequired ms when ms = [] ->
failwith $"SheetEntity of type {typeof<'T>.Name} does not contain Value."
| NoneOptional ms | NoneRequired ms ->
let appendedMessages = ms |> List.reduce (fun a b -> a + "\n\t" + b)
let appendedMessages = Messages.format ms
failwith $"SheetEntity of type {typeof<'T>.Name} does not contain Value: \n\t{appendedMessages}"

type Value = DataType * string
Expand Down
2 changes: 1 addition & 1 deletion src/FsSpreadsheet/DSL/WorkbookBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type WorkbookBuilder() =

member this.SignMessages (messages : Message list) : Message list =
messages
|> List.map (sprintf "In Workbook: %s")
|> List.map (fun m -> m.MapText (sprintf "In Workbook: %s"))

member inline _.Yield(c: WorkbookElement) =
SheetEntity.ok [c]
Expand Down

0 comments on commit fa28800

Please sign in to comment.