Packages
caffeine_lang
2.0.3
6.3.1
6.3.0
6.2.2
6.2.1
6.2.0
6.1.2
6.1.1
6.1.0
6.0.0
5.6.0
5.5.0
5.4.4
5.4.3
5.4.2
5.4.1
5.4.0
5.3.0
5.2.0
5.1.1
5.1.0
5.0.12
5.0.11
5.0.10
5.0.8
5.0.7
5.0.6
5.0.5
5.0.4
5.0.1
5.0.0
4.10.0
4.9.0
4.8.3
4.8.2
4.8.1
4.8.0
4.7.9
4.7.8
4.7.7
4.7.6
4.7.5
4.6.7
4.6.6
4.6.5
4.6.4
4.6.3
4.6.2
4.6.0
4.5.1
4.5.0
4.4.4
4.4.3
4.4.1
4.4.0
4.3.7
4.3.6
3.0.6
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
3.0.0
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
1.0.2
1.0.1
0.1.0
0.0.24
0.0.23
0.0.22
0.0.21
0.0.20
0.0.19
0.0.18
0.0.17
0.0.16
0.0.15
0.0.14
0.0.13
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.2
0.0.1
A compiler for generating reliability artifacts from service expectation definitions.
Current section
Files
Jump to
Current section
Files
src/caffeine_lang/parser/artifacts.gleam
import caffeine_lang/common/accepted_types.{type AcceptedTypes}
import caffeine_lang/common/decoders
import caffeine_lang/common/errors.{type CompilationError}
import caffeine_lang/common/helpers
import caffeine_lang/common/validations
import caffeine_lang/standard_library/artifacts
import gleam/dict
import gleam/dynamic/decode
import gleam/json
import gleam/result
/// A reusable artifact template with named parameters.
pub type Artifact {
Artifact(name: String, params: dict.Dict(String, AcceptedTypes))
}
/// Parses the embedded standard library artifacts.
@internal
pub fn parse_standard_library() -> Result(List(Artifact), CompilationError) {
internal_parse_from_json_string(artifacts.standard_library)
}
/// Parses an artifact from an artifacts.json file.
@internal
pub fn parse_from_json_file(
file_path: String,
) -> Result(List(Artifact), CompilationError) {
use json_string <- result.try(helpers.json_from_file(file_path))
internal_parse_from_json_string(json_string)
}
/// The actual, common parsing logic.
fn internal_parse_from_json_string(
content: String,
) -> Result(List(Artifact), CompilationError) {
use artifacts <- result.try(case parse_from_json_string(content) {
Ok(artifacts) -> Ok(artifacts)
Error(err) -> Error(errors.format_json_decode_error(err))
})
use _ <- result.try(validations.validate_relevant_uniqueness(
artifacts,
fn(a) { a.name },
"artifact names",
))
Ok(artifacts)
}
fn parse_from_json_string(
json_string: String,
) -> Result(List(Artifact), json.DecodeError) {
let artifact_decoder = {
use name <- decode.field("name", decoders.non_empty_string_decoder())
use params <- decode.field(
"params",
decode.dict(decode.string, decoders.accepted_types_decoder()),
)
decode.success(Artifact(name:, params:))
}
let artifacts_decoded = {
use artifacts <- decode.field("artifacts", decode.list(artifact_decoder))
decode.success(artifacts)
}
json.parse(from: json_string, using: artifacts_decoded)
}