<commit_before>/* @flow */

import type { Decode, Decoder } from "./Decoder/Decoder"
import type { float } from "./Decoder/Float"
import type { integer } from "./Decoder/Integer"
import type { Dictionary } from "./Decoder/Dictionary"
import type { Record } from "./Decoder/Record"

import StringDecoder from "./Decoder/String"
import BooleanDecoder from "./Decoder/Boolean"
import IntegerDecoder from "./Decoder/Integer"
import FloatDecoder from "./Decoder/Float"
import Maybe from "./Decoder/Maybe"
import RecordDecoder from "./Decoder/Record"
import ArrayDecoder from "./Decoder/Array"
import Accessor from "./Decoder/Accessor"
import DictionaryDecoder from "./Decoder/Dictionary"
import Either from "./Decoder/Either"
import Field from "./Decoder/Field"
import Null from "./Decoder/Null"
import Undefined from "./Decoder/Undefined"
import Ok from "./Decoder/Ok"
import Index from "./Decoder/Index"
import Error from "./Decoder/Error"

import * as Reader from "./Reader"
import * as result from "result.flow"

export type Result<a> = result.Result<Error, a>
export type { Decoder, Decode, float, integer, Record, Dictionary, Error }

export const decode = <a>(decoder: Decoder<a>, input: mixed): Result<a> => {
  const value = Reader.read(decoder, input)
  if (value instanceof Error) {
    return result.error(value)
  } else {
    return result.ok(value)
  }
}

export const String: Decoder<string> = new StringDecoder()
export const Boolean: Decoder<boolean> = new BooleanDecoder()
export const Integer: Decoder<integer> = new IntegerDecoder()
export const Float: Decoder<float> = new FloatDecoder()

export const error = <a>(reason: string): Decoder<a> => new Error(reason)<commit_msg>Provide Decoder.parse for parsing string input.<commit_after>/* @flow */

import type { Decode, Decoder } from "./Decoder/Decoder"
import type { float } from "./Decoder/Float"
import type { integer } from "./Decoder/Integer"
import type { Dictionary } from "./Decoder/Dictionary"
import type { Record } from "./Decoder/Record"

import StringDecoder from "./Decoder/String"
import BooleanDecoder from "./Decoder/Boolean"
import IntegerDecoder from "./Decoder/Integer"
import FloatDecoder from "./Decoder/Float"
import Maybe from "./Decoder/Maybe"
import RecordDecoder from "./Decoder/Record"
import ArrayDecoder from "./Decoder/Array"
import Accessor from "./Decoder/Accessor"
import DictionaryDecoder from "./Decoder/Dictionary"
import Either from "./Decoder/Either"
import Field from "./Decoder/Field"
import Null from "./Decoder/Null"
import Undefined from "./Decoder/Undefined"
import Ok from "./Decoder/Ok"
import Index from "./Decoder/Index"
import Error from "./Decoder/Error"

import * as Reader from "./Reader"
import * as result from "result.flow"

export type Result<a> = result.Result<Error, a>
export type { Decoder, Decode, float, integer, Record, Dictionary, Error }

class ParseError extends Error {
  name = "ParseError"
  error: { message: string }
  constructor(error: { message: string }) {
    super()
    this.error = error
  }
  describe(context: string): string {
    return `Parse error: ${this.error.message}`
  }
}

export const parse = <a>(decoder: Decoder<a>, input: string): Result<a> => {
  try {
    return decode(decoder, JSON.parse(input))
  } catch (error) {
    return result.error(new ParseError(error))
  }
}

export const decode = <a>(decoder: Decoder<a>, input: mixed): Result<a> => {
  const value = Reader.read(decoder, input)
  if (value instanceof Error) {
    return result.error(value)
  } else {
    return result.ok(value)
  }
}

export const String: Decoder<string> = new StringDecoder()
export const Boolean: Decoder<boolean> = new BooleanDecoder()
export const Integer: Decoder<integer> = new IntegerDecoder()
export const Float: Decoder<float> = new FloatDecoder()

export const error = <a>(reason: string): Decoder<a> => new Error(reason)
{'fraction_non_alphanumeric': 0.1311517650090183, 'fraction_numerical': 0.0, 'mean_word_length': 4.545714285714285, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7079733', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1060, 'n_words': 381}