Skip to content

Commit

Permalink
Rescript core (#22)
Browse files Browse the repository at this point in the history
* import `rescript/core` and refactor relevant code

* update changelog

* Delete CHANGELOG.MD

* attempt to use `.res.js` suffix
  • Loading branch information
cbowling authored Oct 17, 2024
1 parent ff26940 commit fc08a63
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 210 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ lib/bs
.merlin
.bsb.lock
*.bs.js
*.res.js
node_modules
.idea
8 changes: 7 additions & 1 deletion CHANGELOG.MD → CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.0

### Changed

* Upgraded to ReScript v11.1 and using `@rescript/core`

## 0.3.0

### Breaking changes
Expand All @@ -19,4 +25,4 @@
* `Decode.childElements` decoder
* `Decode.requireSome` helper function
* `Decode.ok` decoder
* `Decode.error` decoder
* `Decode.error` decoder
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"rescript": "^11.1.2",
"rescript-webapi": "^0.9.1"
"rescript-webapi": "^0.9.1",
"@rescript/core": "1.5.2"
}
}
8 changes: 6 additions & 2 deletions bsconfig.json → rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
"package-specs": {
"module": "commonjs"
},
"suffix": ".bs.js",
"suffix": ".res.js",
"bs-dependencies": [
"rescript-webapi"
"rescript-webapi",
"@rescript/core"
],
"bsc-flags": [
"-open RescriptCore"
],
"warnings": {
"error": "+101"
Expand Down
23 changes: 11 additions & 12 deletions src/Xml_Decode.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open Belt
open Webapi

type decoder<'a> = Xml_Element.t => 'a
Expand Down Expand Up @@ -65,8 +64,8 @@ let requireNamespace = (element: Xml_Element.t, namespace: option<string>) => {
raise(
DecodeError(
"namespace '" ++
(namespace->Belt.Option.getWithDefault("") ++
("' expected, got '" ++ (element->namespaceURI->Belt.Option.getWithDefault("") ++ "'"))),
(namespace->Option.getOr("") ++
("' expected, got '" ++ (element->namespaceURI->Option.getOr("") ++ "'"))),
),
)
}
Expand Down Expand Up @@ -126,9 +125,9 @@ let select = (~namespace as targetNamespace=?, targetName, element) =>
let children = (selector: Xml_Element.t => bool, decoder: decoder<'a>, element: Xml_Element.t) => {
Xml_Element.childNodes(element)
->Xml_NodeList.asArrayLike
->Belt.Array.keepMap(Xml_Node.asElement)
->Belt.Array.keep(selector)
->Belt.Array.map(decoder(_))
->Array.filterMap(Xml_Node.asElement)
->Array.filter(selector)
->Array.map(decoder(_))
}

let map = (decoder: decoder<'a>, f: 'a => 'b, elem) => decoder(elem)->f
Expand Down Expand Up @@ -156,7 +155,7 @@ let oneOf = (decoders: list<decoder<'a>>, elem: Xml_Element.t) => {

let i = ref(0)
while result.contents->Option.isNone && i.contents < arr->Array.length {
let d = arr->Js.Array.unsafe_get(i.contents)
let d = arr->Array.getUnsafe(i.contents)
let res = try Some(d(elem)) catch {
| DecodeError(_) => None
}
Expand All @@ -170,8 +169,8 @@ let oneOf = (decoders: list<decoder<'a>>, elem: Xml_Element.t) => {
}

let float = str => {
let f = str->Js.Float.fromString
if f->Js.Float.isFinite {
let f = str->Float.fromString->Option.getUnsafe
if f->Float.isFinite {
f
} else {
raise(DecodeError("float expected"))
Expand All @@ -184,8 +183,8 @@ let int = str =>
}

let date = str => {
let d = str->Js.Date.fromString
if d->Js.Date.getTime->Js.Float.isNaN {
let d = str->Date.fromString
if d->Date.getTime->Float.isNaN {
raise(DecodeError("date expected"))
} else {
d
Expand All @@ -198,4 +197,4 @@ let bool = str =>
}

let childElements = elem =>
elem->Xml_Element.childNodes->Xml_NodeList.asArrayLike->Belt.Array.keepMap(Xml_Node.asElement)
elem->Xml_Element.childNodes->Xml_NodeList.asArrayLike->Array.filterMap(Xml_Node.asElement)
2 changes: 1 addition & 1 deletion src/Xml_Decode.resi
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let oneOf: (list<decoder<'a>>, Xml_Element.t) => 'a

let float: string => float
let int: string => int
let date: string => Js.Date.t
let date: string => Date.t
let bool: string => bool

let childElements: decoder<array<Xml_Element.t>>
6 changes: 2 additions & 4 deletions src/Xml_DomParser.res
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
open Belt

type t

@send
external parseFromString: (t, string, string) => Xml_Document.t = "parseFromString"

let parse = (self, text, type_): Result.t<Xml_Element.t, string> => {
let parse = (self, text, type_): result<Xml_Element.t, string> => {
let doc = self->parseFromString(text, type_)

switch doc->Xml_Document.querySelector("parsererror") {
| Some(errorElement) => Error(errorElement->Xml_Element.textContent)
| None =>
let nodes = doc->Xml_Document.childNodes->Xml_NodeList.asArrayLike
switch nodes->Array.keepMap(Xml_Node.asElement)->Array.get(0) {
switch nodes->Array.filterMap(Xml_Node.asElement)->Array.get(0) {
| Some(root) => Ok(root)
| None => Error("root element missing")
}
Expand Down
6 changes: 3 additions & 3 deletions src/Xml_DomParser.resi
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ type t
@send
external parseFromString: (t, string, string) => Xml_Document.t = "parseFromString"

let parse: (t, string, string) => Belt.Result.t<Xml_Element.t, string>
let parse: (t, string, string) => result<Xml_Element.t, string>

let parseXml: (t, string) => Belt.Result.t<Xml_Element.t, string>
let parseXml: (t, string) => result<Xml_Element.t, string>

let parseHtml: (t, string) => Belt.Result.t<Xml_Element.t, string>
let parseHtml: (t, string) => result<Xml_Element.t, string>

// constructor, avoid external
let make: unit => t
1 change: 0 additions & 1 deletion src/Xml_NodeList.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Webapi
open Belt

type t = Dom.NodeList.t

Expand Down
Loading

0 comments on commit fc08a63

Please sign in to comment.