Skip to content

Commit 1cf7c03

Browse files
Merge pull request #50 from cGuille/fix-result-then-deprecations
Replace calls to result.then with result.try (fix deprecation warnings)
2 parents 6bbd95b + 7272659 commit 1cf7c03

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

src/birl.gleam

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn utc_now() -> Time {
9191
///
9292
/// `"+330", "03:30", "-8:00","-7", "-0400", "03"`
9393
pub fn now_with_offset(offset: String) -> Result(Time, Nil) {
94-
use offset <- result.then(parse_offset(offset))
94+
use offset <- result.try(parse_offset(offset))
9595
let now = ffi_now()
9696
let monotonic_now = ffi_monotonic_now()
9797
Time(now, offset, option.None, option.Some(monotonic_now))
@@ -298,7 +298,7 @@ pub fn parse(value: String) -> Result(Time, Nil) {
298298
let assert Ok(offset_pattern) = regexp.from_string("(.*)([+|\\-].*)")
299299
let value = string.trim(value)
300300

301-
use #(day_string, offsetted_time_string) <- result.then(case
301+
use #(day_string, offsetted_time_string) <- result.try(case
302302
string.split(value, "T"),
303303
string.split(value, "t"),
304304
string.split(value, " ")
@@ -314,7 +314,7 @@ pub fn parse(value: String) -> Result(Time, Nil) {
314314
let day_string = string.trim(day_string)
315315
let offsetted_time_string = string.trim(offsetted_time_string)
316316

317-
use #(day_string, time_string, offset_string) <- result.then(case
317+
use #(day_string, time_string, offset_string) <- result.try(case
318318
string.ends_with(offsetted_time_string, "Z")
319319
|| string.ends_with(offsetted_time_string, "z")
320320
{
@@ -342,7 +342,7 @@ pub fn parse(value: String) -> Result(Time, Nil) {
342342
})
343343

344344
let time_string = string.replace(time_string, ":", "")
345-
use #(time_string, milli_seconds_result) <- result.then(case
345+
use #(time_string, milli_seconds_result) <- result.try(case
346346
string.split(time_string, "."),
347347
string.split(time_string, ",")
348348
{
@@ -366,10 +366,10 @@ pub fn parse(value: String) -> Result(Time, Nil) {
366366

367367
case milli_seconds_result {
368368
Ok(milli_seconds) -> {
369-
use day <- result.then(parse_date_section(day_string))
369+
use day <- result.try(parse_date_section(day_string))
370370
let assert [year, month, date] = day
371371

372-
use time_of_day <- result.then(parse_time_section(time_string))
372+
use time_of_day <- result.try(parse_time_section(time_string))
373373
let assert [hour, minute, second] = time_of_day
374374

375375
from_parts(
@@ -412,7 +412,7 @@ pub fn parse_time_of_day(value: String) -> Result(#(TimeOfDay, String), Nil) {
412412
_, _ -> value
413413
}
414414

415-
use #(time_string, offset_string) <- result.then(case
415+
use #(time_string, offset_string) <- result.try(case
416416
string.ends_with(time_string, "Z") || string.ends_with(time_string, "z")
417417
{
418418
True -> Ok(#(string.drop_end(value, 1), "+00:00"))
@@ -430,7 +430,7 @@ pub fn parse_time_of_day(value: String) -> Result(#(TimeOfDay, String), Nil) {
430430

431431
let time_string = string.replace(time_string, ":", "")
432432

433-
use #(time_string, milli_seconds_result) <- result.then(case
433+
use #(time_string, milli_seconds_result) <- result.try(case
434434
string.split(time_string, "."),
435435
string.split(time_string, ",")
436436
{
@@ -453,11 +453,11 @@ pub fn parse_time_of_day(value: String) -> Result(#(TimeOfDay, String), Nil) {
453453

454454
case milli_seconds_result {
455455
Ok(milli_seconds) -> {
456-
use time_of_day <- result.then(parse_time_section(time_string))
456+
use time_of_day <- result.try(parse_time_section(time_string))
457457
let assert [hour, minute, second] = time_of_day
458458

459-
use offset <- result.then(parse_offset(offset_string))
460-
use offset_string <- result.then(generate_offset(offset))
459+
use offset <- result.try(parse_offset(offset_string))
460+
use offset_string <- result.try(generate_offset(offset))
461461

462462
Ok(#(TimeOfDay(hour, minute, second, milli_seconds), offset_string))
463463
}
@@ -479,7 +479,7 @@ pub fn parse_naive_time_of_day(
479479

480480
let time_string = string.replace(time_string, ":", "")
481481

482-
use #(time_string, milli_seconds_result) <- result.then(case
482+
use #(time_string, milli_seconds_result) <- result.try(case
483483
string.split(time_string, "."),
484484
string.split(time_string, ",")
485485
{
@@ -502,7 +502,7 @@ pub fn parse_naive_time_of_day(
502502

503503
case milli_seconds_result {
504504
Ok(milli_seconds) -> {
505-
use time_of_day <- result.then(parse_time_section(time_string))
505+
use time_of_day <- result.try(parse_time_section(time_string))
506506
let assert [hour, minute, second] = time_of_day
507507

508508
Ok(#(TimeOfDay(hour, minute, second, milli_seconds), "Z"))
@@ -579,7 +579,7 @@ pub fn to_naive(value: Time) -> String {
579579
pub fn from_naive(value: String) -> Result(Time, Nil) {
580580
let value = string.trim(value)
581581

582-
use #(day_string, time_string) <- result.then(case
582+
use #(day_string, time_string) <- result.try(case
583583
string.split(value, "T"),
584584
string.split(value, "t"),
585585
string.split(value, " ")
@@ -596,7 +596,7 @@ pub fn from_naive(value: String) -> Result(Time, Nil) {
596596
let time_string = string.trim(time_string)
597597

598598
let time_string = string.replace(time_string, ":", "")
599-
use #(time_string, milli_seconds_result) <- result.then(case
599+
use #(time_string, milli_seconds_result) <- result.try(case
600600
string.split(time_string, "."),
601601
string.split(time_string, ",")
602602
{
@@ -618,10 +618,10 @@ pub fn from_naive(value: String) -> Result(Time, Nil) {
618618

619619
case milli_seconds_result {
620620
Ok(milli_seconds) -> {
621-
use day <- result.then(parse_date_section(day_string))
621+
use day <- result.try(parse_date_section(day_string))
622622
let assert [year, month, date] = day
623623

624-
use time_of_day <- result.then(parse_time_section(time_string))
624+
use time_of_day <- result.try(parse_time_section(time_string))
625625
let assert [hour, minute, second] = time_of_day
626626

627627
from_parts(
@@ -733,7 +733,7 @@ pub fn to_http_with_offset(value: Time) -> String {
733733
/// - `Tuesday, 01 November 2016 08:49:37 +06:30`
734734
pub fn from_http(value: String) -> Result(Time, Nil) {
735735
let value = string.trim(value)
736-
use #(weekday, rest) <- result.then(string.split_once(value, ","))
736+
use #(weekday, rest) <- result.try(string.split_once(value, ","))
737737

738738
use <- bool.guard(
739739
!list.any(weekday_strings, fn(weekday_item) {
@@ -932,8 +932,8 @@ pub fn parse_relative(origin: Time, legible_difference: String) {
932932
True -> string.drop_end(unit, 1)
933933
}
934934

935-
use amount <- result.then(int.parse(amount_string))
936-
use unit <- result.then(list.key_find(string_to_units, unit))
935+
use amount <- result.try(int.parse(amount_string))
936+
use unit <- result.try(list.key_find(string_to_units, unit))
937937
Ok(add(origin, duration.new([#(amount, unit)])))
938938
}
939939

@@ -947,8 +947,8 @@ pub fn parse_relative(origin: Time, legible_difference: String) {
947947
True -> string.drop_end(unit, 1)
948948
}
949949

950-
use amount <- result.then(int.parse(amount_string))
951-
use unit <- result.then(list.key_find(string_to_units, unit))
950+
use amount <- result.try(int.parse(amount_string))
951+
use unit <- result.try(list.key_find(string_to_units, unit))
952952
Ok(subtract(origin, duration.new([#(amount, unit)])))
953953
}
954954

@@ -1195,7 +1195,7 @@ pub fn get_timezone(value: Time) -> option.Option(String) {
11951195
///
11961196
/// `"+330", "03:30", "-8:00","-7", "-0400", "03", "Z"`
11971197
pub fn set_offset(value: Time, new_offset: String) -> Result(Time, Nil) {
1198-
use new_offset_number <- result.then(parse_offset(new_offset))
1198+
use new_offset_number <- result.try(parse_offset(new_offset))
11991199
case value {
12001200
Time(wall_time: t, offset: _, timezone: timezone, monotonic_time: mt) ->
12011201
Time(t, new_offset_number, timezone, mt)
@@ -1311,7 +1311,7 @@ fn from_parts(
13111311
time: #(Int, Int, Int, Int),
13121312
offset: String,
13131313
) -> Result(Time, Nil) {
1314-
use offset_number <- result.then(parse_offset(offset))
1314+
use offset_number <- result.try(parse_offset(offset))
13151315
ffi_from_parts(#(date, time), offset_number)
13161316
|> Time(offset_number, option.None, option.None)
13171317
|> Ok
@@ -1331,7 +1331,7 @@ fn parse_offset(offset: String) -> Result(Int, Nil) {
13311331
use <- bool.guard(list.contains(["Z", "z"], offset), Ok(0))
13321332
let assert Ok(re) = regexp.from_string("([+-])")
13331333

1334-
use #(sign, offset) <- result.then(case regexp.split(re, offset) {
1334+
use #(sign, offset) <- result.try(case regexp.split(re, offset) {
13351335
["", "+", offset] -> Ok(#(1, offset))
13361336
["", "-", offset] -> Ok(#(-1, offset))
13371337
[_] -> Ok(#(1, offset))
@@ -1340,18 +1340,18 @@ fn parse_offset(offset: String) -> Result(Int, Nil) {
13401340

13411341
case string.split(offset, ":") {
13421342
[hour_str, minute_str] -> {
1343-
use hour <- result.then(int.parse(hour_str))
1344-
use minute <- result.then(int.parse(minute_str))
1343+
use hour <- result.try(int.parse(hour_str))
1344+
use minute <- result.try(int.parse(minute_str))
13451345
Ok(sign * { hour * 60 + minute } * 60 * 1_000_000)
13461346
}
13471347
[offset] ->
13481348
case string.length(offset) {
13491349
1 -> {
1350-
use hour <- result.then(int.parse(offset))
1350+
use hour <- result.try(int.parse(offset))
13511351
Ok(sign * hour * 3600 * 1_000_000)
13521352
}
13531353
2 -> {
1354-
use number <- result.then(int.parse(offset))
1354+
use number <- result.try(int.parse(offset))
13551355
case number < 14 {
13561356
True -> Ok(sign * number * 3600 * 1_000_000)
13571357
False ->
@@ -1361,15 +1361,15 @@ fn parse_offset(offset: String) -> Result(Int, Nil) {
13611361
3 -> {
13621362
let assert Ok(hour_str) = string.first(offset)
13631363
let minute_str = string.slice(offset, 1, 2)
1364-
use hour <- result.then(int.parse(hour_str))
1365-
use minute <- result.then(int.parse(minute_str))
1364+
use hour <- result.try(int.parse(hour_str))
1365+
use minute <- result.try(int.parse(minute_str))
13661366
Ok(sign * { hour * 60 + minute } * 60 * 1_000_000)
13671367
}
13681368
4 -> {
13691369
let hour_str = string.slice(offset, 0, 2)
13701370
let minute_str = string.slice(offset, 2, 2)
1371-
use hour <- result.then(int.parse(hour_str))
1372-
use minute <- result.then(int.parse(minute_str))
1371+
use hour <- result.try(int.parse(hour_str))
1372+
use minute <- result.try(int.parse(minute_str))
13731373
Ok(sign * { hour * 60 + minute } * 60 * 1_000_000)
13741374
}
13751375
_ -> Error(Nil)

src/birl/duration.gleam

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub fn parse(expression: String) -> Result(Duration, Nil) {
344344
|> list.try_map(fn(item) {
345345
case item {
346346
regexp.Match(_, [sign_option, option.Some(amount_string)]) -> {
347-
use amount <- result.then(int.parse(amount_string))
347+
use amount <- result.try(int.parse(amount_string))
348348

349349
case sign_option {
350350
option.Some("-") -> Ok(#(-1 * amount, MicroSecond))
@@ -357,8 +357,8 @@ pub fn parse(expression: String) -> Result(Duration, Nil) {
357357
_,
358358
[sign_option, option.Some(amount_string), option.Some(unit)],
359359
) -> {
360-
use amount <- result.then(int.parse(amount_string))
361-
use #(unit, _) <- result.then(
360+
use amount <- result.try(int.parse(amount_string))
361+
use #(unit, _) <- result.try(
362362
list.find(units, fn(item) { list.contains(item.1, unit) }),
363363
)
364364

0 commit comments

Comments
 (0)