-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add shared ScheduleAt type * Lint --------- Co-authored-by: Tyler Cloutier <[email protected]> Co-authored-by: Tyler Cloutier <[email protected]>
- Loading branch information
1 parent
9884068
commit 2e253a1
Showing
4 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { AlgebraicType, SumTypeVariant } from './algebraic_type'; | ||
import type { AlgebraicValue } from './algebraic_value'; | ||
|
||
export namespace ScheduleAt { | ||
export function getAlgebraicType(): AlgebraicType { | ||
return AlgebraicType.createSumType([ | ||
new SumTypeVariant('Interval', AlgebraicType.createU64Type()), | ||
new SumTypeVariant('Time', AlgebraicType.createU64Type()), | ||
]); | ||
} | ||
|
||
export function serialize(value: ScheduleAt): object { | ||
switch (value.tag) { | ||
case 'Interval': | ||
return { Interval: value.value }; | ||
case 'Time': | ||
return { Time: value.value }; | ||
default: | ||
throw 'unreachable'; | ||
} | ||
} | ||
|
||
export type Interval = { tag: 'Interval'; value: BigInt }; | ||
export const Interval = (value: BigInt): Interval => ({ | ||
tag: 'Interval', | ||
value, | ||
}); | ||
export type Time = { tag: 'Time'; value: BigInt }; | ||
export const Time = (value: BigInt): Time => ({ tag: 'Time', value }); | ||
|
||
export function fromValue(value: AlgebraicValue): ScheduleAt { | ||
let sumValue = value.asSumValue(); | ||
switch (sumValue.tag) { | ||
case 0: | ||
return { tag: 'Interval', value: sumValue.value.asBigInt() }; | ||
case 1: | ||
return { tag: 'Time', value: sumValue.value.asBigInt() }; | ||
default: | ||
throw 'unreachable'; | ||
} | ||
} | ||
} | ||
|
||
export type ScheduleAt = ScheduleAt.Interval | ScheduleAt.Time; | ||
export default ScheduleAt; |