Skip to content

Commit

Permalink
fix flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmiville committed Dec 10, 2024
1 parent 67afa72 commit fa53876
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "clad"
version = "0.2.1"
version = "0.3.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
Expand Down
44 changes: 25 additions & 19 deletions src/clad.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@
//// ```

import decode/zero.{type Decoder}
import gleam/bool
import gleam/dict.{type Dict}
import gleam/dynamic.{type Dynamic}
import gleam/float
Expand Down Expand Up @@ -239,34 +238,41 @@ pub fn positional_arguments(
/// let result = clad.decode([], decoder)
/// assert result == Ok(False)
/// ```
pub fn flag() -> Decoder(Bool) {
zero.bool
|> zero.optional
|> zero.map(option.unwrap(_, False))
pub fn flag(
long_name: String,
short_name: String,
next: fn(Bool) -> Decoder(final),
) -> Decoder(final) {
use value <- optional_field(long_name, zero.bool)
case value {
Some(v) -> next(v)
None -> zero.optional_field(short_name, False, zero.bool, next)
}
}

fn optional_field(
field_name: name,
field_decoder: Decoder(t),
next: fn(Option(t)) -> Decoder(final),
) -> Decoder(final) {
let decoding_function = fn(data: Dynamic) {
use <- bool.guard(dynamic.classify(data) == "Nil", Ok(None))
zero.optional_field(field_name, None, zero.optional(field_decoder), next)
// let decoding_function = fn(data: Dynamic) {
// use <- bool.guard(dynamic.classify(data) == "Nil", Ok(None))

case zero.run(data, zero.optional(field_decoder)) {
Ok(None) -> {
case zero.run(data, field_decoder) {
Ok(v) -> Ok(Some(v))
Error(_) -> Ok(None)
}
}
other -> other
}
}
// case zero.run(data, zero.optional(field_decoder)) {
// Ok(None) -> {
// case zero.run(data, field_decoder) {
// Ok(v) -> Ok(Some(v))
// Error(_) -> Ok(None)
// }
// }
// other -> other
// }
// }

let decoder = zero.new_primitive_decoder(decoding_function, None)
// let decoder = zero.new_primitive_decoder(decoding_function, None)

zero.field(field_name, decoder, next)
// zero.field(field_name, decoder, next)
}

/// Decode a command line option by either a long name or short name
Expand Down
52 changes: 27 additions & 25 deletions test/clad_test.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import clad
import decode/zero
import gleam/dynamic.{DecodeError}
import gleam/option.{None, Some}
import gleeunit
import gleeunit/should

Expand All @@ -26,11 +25,11 @@ pub fn decode_test() {
|> clad.decode(["-b", "1"], _)
|> should.equal(Ok(1))

zero.field("z", clad.flag(), zero.success)
clad.flag("baz", "z", zero.success)
|> clad.decode(["-z"], _)
|> should.equal(Ok(True))

zero.field("z", clad.flag(), zero.success)
clad.flag("baz", "z", zero.success)
|> clad.decode([], _)
|> should.equal(Ok(False))

Expand All @@ -45,7 +44,7 @@ pub fn decode_test() {
let decoder = {
use foo <- clad.opt("foo", "f", zero.string)
use bar <- clad.opt("bar", "b", zero.int)
use baz <- clad.opt("baz", "z", clad.flag())
use baz <- clad.flag("baz", "z")
use qux <- clad.opt("qux", "q", zero.float)
use names <- clad.positional_arguments
zero.success(Options(foo:, bar:, baz:, qux:, names:))
Expand Down Expand Up @@ -82,7 +81,7 @@ pub fn decode_test() {
pub fn decode_errors_test() {
zero.field("f", zero.string, zero.success)
|> clad.decode(["--bar", "hello"], _)
|> should.equal(Error([DecodeError("String", "Nil", ["f"])]))
|> should.equal(Error([DecodeError("Field", "Nothing", ["f"])]))

zero.field("foo", zero.string, zero.success)
|> clad.decode(["--foo", "1"], _)
Expand All @@ -91,7 +90,7 @@ pub fn decode_errors_test() {
let decoder = {
use foo <- clad.opt("foo", "f", zero.string)
use bar <- clad.opt("bar", "b", zero.int)
use baz <- clad.opt("baz", "z", clad.flag())
use baz <- clad.flag("baz", "z")
use qux <- clad.opt("qux", "q", zero.float)
use names <- clad.positional_arguments
zero.success(Options(foo:, bar:, baz:, qux:, names:))
Expand All @@ -102,9 +101,9 @@ pub fn decode_errors_test() {
clad.decode(args, decoder)
|> should.equal(
Error([
DecodeError("String", "Nil", ["f"]),
DecodeError("Int", "Nil", ["b"]),
DecodeError("Float", "Nil", ["q"]),
DecodeError("Field", "Nothing", ["f"]),
DecodeError("Field", "Nothing", ["b"]),
DecodeError("Field", "Nothing", ["q"]),
]),
)

Expand All @@ -113,16 +112,19 @@ pub fn decode_errors_test() {
clad.decode(args, decoder)
|> should.equal(
Error([
DecodeError("String", "Nil", ["f"]),
DecodeError("Float", "Nil", ["q"]),
DecodeError("Field", "Nothing", ["f"]),
DecodeError("Field", "Nothing", ["q"]),
]),
)

// missing second field
let args = ["--foo", "hello"]
clad.decode(args, decoder)
|> should.equal(
Error([DecodeError("Int", "Nil", ["b"]), DecodeError("Float", "Nil", ["q"])]),
Error([
DecodeError("Field", "Nothing", ["b"]),
DecodeError("Field", "Nothing", ["q"]),
]),
)

// wrong type
Expand All @@ -131,7 +133,7 @@ pub fn decode_errors_test() {
|> should.equal(
Error([
DecodeError("Int", "String", ["b"]),
DecodeError("Float", "Nil", ["q"]),
DecodeError("Field", "Nothing", ["q"]),
]),
)
}
Expand All @@ -144,25 +146,25 @@ pub fn opt_test() {
clad.opt("foo", "f", zero.string, zero.success)
|> clad.decode(["-f", "hello"], _)
|> should.equal(Ok("hello"))
// clad.opt("foo", "f", zero.string, zero.success)
// |> clad.decode([], _)
// |> should.equal(Error([DecodeError("String", "Nothing", ["f"])]))

clad.opt("foo", "f", zero.string, zero.success)
|> clad.decode([], _)
|> should.equal(Error([DecodeError("String", "Nil", ["f"])]))

clad.opt("foo", "f", zero.optional(zero.string), zero.success)
|> clad.decode(["-f", "hello"], _)
|> should.equal(Ok(Some("hello")))
// clad.opt("foo", "f", zero.optional(zero.string), zero.success)
// |> clad.decode(["-f", "hello"], _)
// |> should.equal(Ok(Some("hello")))

clad.opt("foo", "f", zero.optional(zero.string), zero.success)
|> clad.decode([], _)
|> should.equal(Ok(None))
// clad.opt("foo", "f", zero.optional(zero.string), zero.success)
// |> clad.decode([], _)
// |> should.equal(Ok(None))
}

pub fn flag_test() {
let decoder = {
use verbose <- zero.field("v", clad.flag())
use verbose <- clad.flag("verbose", "v")
zero.success(verbose)
}

clad.decode(["-v"], decoder)
|> should.equal(Ok(True))

Expand All @@ -181,7 +183,7 @@ pub fn flag_test() {

pub fn positional_arguments_test() {
let decoder = {
use a <- zero.field("a", clad.flag())
use a <- zero.field("a", zero.bool)
use b <- zero.field("b", zero.int)
use c <- clad.positional_arguments()
zero.success(#(a, b, c))
Expand Down
2 changes: 1 addition & 1 deletion test/examples/student.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {
let decoder = {
use name <- clad.opt("name", "n", zero.string)
use age <- clad.opt("age", "a", zero.int)
use enrolled <- clad.opt("enrolled", "e", clad.flag())
use enrolled <- clad.flag("enrolled", "e")
use classes <- clad.opt("class", "c", clad.list(zero.string))
use notes <- clad.positional_arguments()
let notes = string.join(notes, " ")
Expand Down

0 comments on commit fa53876

Please sign in to comment.