Starting to clean up exporting Problem_Behavior constructor refactor. (#10449)

Use type check everywhere in Standard.Base for Problem_Behavior.
This commit is contained in:
James Dunkerley 2024-07-05 11:05:51 +01:00 committed by GitHub
parent c54c3b7e9d
commit 82678aa7cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 50 additions and 53 deletions

View File

@ -68,7 +68,7 @@ from project.System.File_Format import Auto_Detect, File_Format
@path Text_Input
@format File_Format.default_widget
read : Text | URI | File -> File_Format -> Problem_Behavior -> Any ! File_Error
read path format=Auto_Detect (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) = case path of
read path format=Auto_Detect (on_problems : Problem_Behavior = ..Report_Warning) = case path of
_ : Text -> if Data_Read_Helpers.looks_like_uri path then Data_Read_Helpers.fetch_following_data_links path format=format else
read (File.new path) format on_problems
uri : URI -> Data_Read_Helpers.fetch_following_data_links uri format=format
@ -103,7 +103,7 @@ read path format=Auto_Detect (on_problems : Problem_Behavior = Problem_Behavior.
@path Text_Input
@encoding Encoding.default_widget
read_text : (Text | File) -> Encoding -> Problem_Behavior -> Text
read_text path (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
read_text path (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = ..Report_Warning) =
File.new path . read_text encoding on_problems
## GROUP Input

View File

@ -147,7 +147,7 @@ type Array
[My_Type.Value 'hello', 1].to_array.sort == [1, My_Type.Value 'hello'].to_array
sort : Sort_Direction -> (Any -> Any)|Nothing -> (Any -> Any -> (Ordering|Nothing))|Nothing -> Problem_Behavior -> Vector Any ! Incomparable_Values
sort self (order = Sort_Direction.Ascending) on=Nothing by=Nothing on_incomparable=Problem_Behavior.Ignore =
sort self (order = Sort_Direction.Ascending) on=Nothing by=Nothing on_incomparable:Problem_Behavior=..Ignore =
Array_Like_Helpers.sort self order on by on_incomparable
## ALIAS first, head, last, limit, sample, slice, tail, top, keep
@ -400,7 +400,7 @@ type Array
[1, 2, 3].to_array . map +1
map : (Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
map self function on_problems=Problem_Behavior.Report_Error =
map self function on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
Array_Like_Helpers.map self function on_problems
## ICON union
@ -439,7 +439,7 @@ type Array
[0, 1, 2].to_array . flat_map (n -> Vector.fill n n)
flat_map : (Any -> Vector Any) -> Problem_Behavior | No_Wrap -> Vector Any
flat_map self function on_problems=Problem_Behavior.Report_Error =
flat_map self function on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
Array_Like_Helpers.flat_map self function on_problems
## GROUP Selections
@ -686,7 +686,7 @@ type Array
[1, 2, 3].to_array.map_with_index (+)
map_with_index : (Integer -> Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
map_with_index self function on_problems=Problem_Behavior.Report_Error =
map_with_index self function on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
Array_Like_Helpers.map_with_index self function on_problems
## PRIVATE
@ -915,7 +915,7 @@ type Array
[1, 2, 3].to_array.zip [4, 5, 6].to_array == [[1, 4].to_array, [2, 5].to_array, [3, 6].to_array].to_array
zip : Vector Any -> (Any -> Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
zip self that function=[_,_] on_problems=Problem_Behavior.Report_Error =
zip self that function=[_,_] on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
len = self.length.min that.length
Array_Like_Helpers.vector_from_function len (i-> function (self.at i) (that.at i)) on_problems

View File

@ -743,7 +743,7 @@ Text.is_whitespace self =
"Hello".bytes (Encoding.ascii)
@encoding Encoding.default_widget
Text.bytes : Encoding -> Problem_Behavior -> Vector Integer
Text.bytes self (encoding : Encoding = Encoding.utf_8) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
Text.bytes self (encoding : Encoding = Encoding.utf_8) (on_problems : Problem_Behavior = ..Report_Warning) =
result = Encoding_Utils.get_bytes self encoding.to_java_charset : ResultWithWarnings
vector = Vector.from_polyglot_array result.result
if result.warnings.is_nothing then vector else
@ -765,7 +765,7 @@ Text.bytes self (encoding : Encoding = Encoding.utf_8) (on_problems : Problem_Be
Text.from_bytes [72, 101, 108, 108, 111] (Encoding.ascii)
@encoding Encoding.default_widget
Text.from_bytes : Vector Integer -> Encoding -> Problem_Behavior -> Text
Text.from_bytes bytes (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = Problem_Behavior.Report_Error) =
Text.from_bytes bytes (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = ..Report_Error) =
Reporting_Stream_Decoder_Helper.decode_bytes_to_text bytes encoding on_problems
## ICON convert
@ -787,7 +787,7 @@ Text.from_bytes bytes (encoding : Encoding = Encoding.default) (on_problems : Pr
"Hello".utf_8
Text.utf_8 : Problem_Behavior -> Vector Integer
Text.utf_8 self (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
Text.utf_8 self (on_problems : Problem_Behavior = ..Report_Warning) =
self.bytes Encoding.utf_8 on_problems
## ICON convert
@ -806,7 +806,7 @@ Text.utf_8 self (on_problems : Problem_Behavior = Problem_Behavior.Report_Warnin
Text.from_utf_8 [-32, -92, -107, -32, -91, -115, -32, -92, -73, -32, -92, -65]
Text.from_utf_8 : Vector Integer -> Problem_Behavior -> Text
Text.from_utf_8 bytes (on_problems : Problem_Behavior = Problem_Behavior.Report_Error) =
Text.from_utf_8 bytes (on_problems : Problem_Behavior = ..Report_Error) =
Text.from_bytes bytes Encoding.utf_8 on_problems
## ICON convert

View File

@ -699,7 +699,7 @@ type Vector a
[1, 2, 3] . map +1
map : (Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
map self function on_problems=Problem_Behavior.Report_Error =
map self function on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
Array_Like_Helpers.map self function on_problems
## ICON union
@ -738,7 +738,7 @@ type Vector a
[0, 1, 2] . flat_map (n -> Vector.fill n n)
flat_map : (Any -> Vector Any) -> Problem_Behavior | No_Wrap -> Vector Any
flat_map self function on_problems=Problem_Behavior.Report_Error =
flat_map self function on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
Array_Like_Helpers.flat_map self function on_problems
## GROUP Calculations
@ -792,7 +792,7 @@ type Vector a
[1, 2, 3].map_with_index (+)
map_with_index : (Integer -> Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
map_with_index self function on_problems=Problem_Behavior.Report_Error =
map_with_index self function on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
Array_Like_Helpers.map_with_index self function on_problems
## PRIVATE
@ -1040,7 +1040,7 @@ type Vector a
[1, 2, 3].zip [4, 5, 6] == [[1, 4], [2, 5], [3, 6]]
zip : Vector Any -> (Any -> Any -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
zip self that:Vector function=[_,_] on_problems=Problem_Behavior.Report_Error =
zip self that:Vector function=[_,_] on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
len = self.length.min that.length
Array_Like_Helpers.vector_from_function len (i-> function (self.at i) (that.at i)) on_problems
@ -1183,7 +1183,7 @@ type Vector a
[My_Type.Value 'hello', 1].sort == [1, My_Type.Value 'hello']
sort : Sort_Direction -> (Any -> Any)|Nothing -> (Any -> Any -> (Ordering|Nothing))|Nothing -> Problem_Behavior -> Vector Any ! Incomparable_Values
sort self (order = Sort_Direction.Ascending) on=Nothing by=Nothing on_incomparable=Problem_Behavior.Ignore =
sort self (order = Sort_Direction.Ascending) on=Nothing by=Nothing on_incomparable:Problem_Behavior=..Ignore =
Array_Like_Helpers.sort self order on by on_incomparable
## ALIAS deduplicate, unique

View File

@ -251,7 +251,7 @@ type XML_Document
@path (Widget.Text_Input display=Display.Always)
@encoding Encoding.default_widget
write : Writable_File -> Encoding -> Existing_File_Behavior -> Boolean -> Problem_Behavior -> File
write self path:Writable_File (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (include_xml_declaration : Boolean = Boolean.True) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
write self path:Writable_File (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (include_xml_declaration : Boolean = Boolean.True) (on_problems : Problem_Behavior = ..Report_Warning) =
write_impl self.java_document path encoding on_existing_file include_xml_declaration on_problems
## GROUP Selections
@ -603,7 +603,7 @@ type XML_Element
@path (Widget.Text_Input display=Display.Always)
@encoding Encoding.default_widget
write : Writable_File -> Encoding -> Existing_File_Behavior -> Boolean -> Problem_Behavior -> File
write self path:Writable_File (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (include_xml_declaration : Boolean = Boolean.True) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
write self path:Writable_File (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (include_xml_declaration : Boolean = Boolean.True) (on_problems : Problem_Behavior = ..Report_Warning) =
write_impl self.java_element path encoding on_existing_file include_xml_declaration on_problems
## ICON parse3
@ -753,7 +753,7 @@ child_selector node:(XML_Element | XML_Document) =
## PRIVATE
write an XML document or element to a file.
write_impl node:Node path:Writable_File (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (include_xml_declaration : Boolean = Boolean.True) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
write_impl node:Node path:Writable_File (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (include_xml_declaration : Boolean = Boolean.True) (on_problems : Problem_Behavior = ..Report_Warning) =
declaration = if include_xml_declaration then '<?xml version=\"1.0\" encoding=\"' + encoding.character_set_name + '\"?>\n' else ""
XML_Error.handle_java_exceptions <|
(declaration + XML_Utils.outerXML node True).write path encoding on_existing_file on_problems

View File

@ -7,7 +7,6 @@ import project.Error.Error
import project.Errors.File_Error.File_Error
import project.Errors.Illegal_Argument.Illegal_Argument
import project.Errors.Illegal_State.Illegal_State
import project.Errors.Problem_Behavior.Problem_Behavior
import project.Meta
import project.Nothing.Nothing
import project.Panic.Panic
@ -115,7 +114,7 @@ type Data_Link
options = [File_Access.Read, Data_Link_Access.No_Follow]
bytes = file.underlying.with_input_stream options input_stream->
input_stream.read_all_bytes
Text.from_bytes bytes data_link_encoding on_problems=Problem_Behavior.Report_Error
Text.from_bytes bytes data_link_encoding on_problems=..Report_Error
## PRIVATE
Writes raw data as the data-link configuration.
@ -134,7 +133,7 @@ type Data_Link
exist_options = if replace_existing then [File_Access.Create, File_Access.Truncate_Existing] else [File_Access.Create_New]
options = exist_options + [File_Access.Write, Data_Link_Access.No_Follow]
bytes = raw_content.bytes data_link_encoding on_problems=Problem_Behavior.Report_Error
bytes = raw_content.bytes data_link_encoding on_problems=..Report_Error
r = bytes.if_not_error <| file.underlying.with_output_stream options output_stream->
output_stream.write_bytes bytes
r.if_not_error file.underlying

View File

@ -250,7 +250,7 @@ type Enso_File
If set to `Ignore`, the operation proceeds without errors or warnings.
@format File_Format.default_widget
read : File_Format -> Problem_Behavior -> Any ! Illegal_Argument | File_Error
read self format=Auto_Detect (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
read self format=Auto_Detect (on_problems : Problem_Behavior = ..Report_Warning) =
asset = Existing_Enso_Asset.get_asset_reference_for self
case asset.asset_type of
Enso_Asset_Type.Project -> Error.throw (Illegal_Argument.Error "Projects cannot be read within Enso code. Open using the IDE.")
@ -290,7 +290,7 @@ type Enso_File
If set to `Ignore`, the operation proceeds without errors or warnings.
@encoding Encoding.default_widget
read_text : Encoding -> Problem_Behavior -> Text ! File_Error
read_text self (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
read_text self (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = ..Report_Warning) =
self.read (Plain_Text_Format.Plain_Text encoding) on_problems
## GROUP Metadata

View File

@ -3,8 +3,6 @@ import project.Data.Vector.Vector
import project.Error.Error
import project.Warning.Warning
from project.Errors.Problem_Behavior.Problem_Behavior import all
## Specifies how to handle problems.
type Problem_Behavior
## Ignore the problem and attempt to complete the operation
@ -29,12 +27,12 @@ type Problem_Behavior
error takes precedence.
attach_problem_after : Any -> Any -> Any
attach_problem_after self decorated_value problem = case self of
Ignore ->
Problem_Behavior.Ignore ->
if decorated_value.is_error then decorated_value else
if problem.is_error then problem else decorated_value
Report_Warning ->
Problem_Behavior.Report_Warning ->
Warning.attach problem decorated_value
Report_Error ->
Problem_Behavior.Report_Error ->
if decorated_value.is_error then decorated_value else
Error.throw problem
@ -50,11 +48,11 @@ type Problem_Behavior
a dataflow error.
attach_problem_before : Any -> Any -> Any
attach_problem_before self problem ~decorated_value = case self of
Ignore ->
Problem_Behavior.Ignore ->
if problem.is_error then problem else decorated_value
Report_Warning ->
Problem_Behavior.Report_Warning ->
Warning.attach problem decorated_value
Report_Error ->
Problem_Behavior.Report_Error ->
Error.throw problem
## PRIVATE
@ -79,12 +77,12 @@ type Problem_Behavior
attach_problems_before : Vector -> Any -> Any
attach_problems_before self problems ~decorated_value = case self of
Ignore ->
Problem_Behavior.Ignore ->
if problems.is_error then problems else decorated_value
Report_Warning ->
Problem_Behavior.Report_Warning ->
problems.fold decorated_value acc-> problem->
Warning.attach problem acc
Report_Error ->
Problem_Behavior.Report_Error ->
if problems.is_empty then decorated_value else
Error.throw problems.first
@ -109,13 +107,13 @@ type Problem_Behavior
perform_post_process_checks_and_return_problems
attach_problems_after : Any -> Vector -> Any
attach_problems_after self decorated_value problems = case self of
Ignore ->
Problem_Behavior.Ignore ->
if decorated_value.is_error then decorated_value else
if problems.is_error then problems else decorated_value
Report_Warning ->
Problem_Behavior.Report_Warning ->
problems.fold decorated_value acc-> problem->
Warning.attach problem acc
Report_Error ->
Problem_Behavior.Report_Error ->
if decorated_value.is_error || problems.is_empty then decorated_value else
Error.throw problems.first
@ -133,9 +131,9 @@ type Problem_Behavior
errors. By default `Any` error is caught.
handle_errors : Any -> Any -> Any -> Any
handle_errors self result ~fallback error_type=Any = result.catch error_type error-> case self of
Ignore -> fallback
Report_Warning -> Warning.attach error fallback
Report_Error -> result
Problem_Behavior.Ignore -> fallback
Problem_Behavior.Report_Warning -> Warning.attach error fallback
Problem_Behavior.Report_Error -> result
## PRIVATE
ADVANCED
@ -155,6 +153,6 @@ type Problem_Behavior
Returns a mapping of Problem_Behavior constructors to an integer.
Used for sending the number to Java, rather than sending the atom.
to_number self = case self of
Ignore -> 0
Report_Warning -> 1
Report_Error -> 2
Problem_Behavior.Ignore -> 0
Problem_Behavior.Report_Warning -> 1
Problem_Behavior.Report_Error -> 2

View File

@ -83,7 +83,7 @@ slice vector start end = @Builtin_Method "Array_Like_Helpers.slice"
- Ignore: The result is `Nothing`, and the error is
ignored.
vector_from_function : Integer -> (Integer -> Any) -> Problem_Behavior | No_Wrap -> Vector Any
vector_from_function length function on_problems=Problem_Behavior.Report_Error =
vector_from_function length function on_problems:(Problem_Behavior | No_Wrap)=..Report_Error =
num_errors = Ref.new 0
wrapped_function i =
result = function i

View File

@ -334,7 +334,7 @@ type File
example_read = Examples.csv.read
@encoding Encoding.default_widget
read_text : Encoding -> Problem_Behavior -> Text ! File_Error
read_text self (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
read_text self (encoding : Encoding = Encoding.default) (on_problems : Problem_Behavior = ..Report_Warning) =
bytes = self.read_bytes
Text.from_bytes bytes encoding on_problems

View File

@ -55,7 +55,7 @@ polyglot java import org.enso.base.Array_Utils
@path (Widget.Text_Input display=Display.Always)
@encoding Encoding.default_widget
Text.write : Writable_File -> Encoding -> Existing_File_Behavior -> Problem_Behavior -> File ! Encoding_Error | Illegal_Argument | File_Error
Text.write self (path : Writable_File) (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (on_problems : Problem_Behavior = Problem_Behavior.Report_Warning) =
Text.write self (path : Writable_File) (encoding : Encoding = Encoding.utf_8) (on_existing_file : Existing_File_Behavior = Existing_File_Behavior.Backup) (on_problems : Problem_Behavior = ..Report_Warning) =
bytes = self.bytes encoding on_problems
bytes.if_not_error <|
path.write_handling_dry_run on_existing_file effective_file-> stream->

View File

@ -106,7 +106,7 @@ type File_Format
## PRIVATE
Implements the `File.read` for this `File_Format`
read : File -> Problem_Behavior -> Any
read self file on_problems =
read self file on_problems:Problem_Behavior =
_ = [file, on_problems]
Unimplemented.throw "This is an interface only."
@ -170,7 +170,7 @@ type Plain_Text_Format
## PRIVATE
Implements the `File.read` for this `File_Format`
read : File -> Problem_Behavior -> Any
read self file on_problems =
read self file on_problems:Problem_Behavior =
file.read_text (self.resolve_encoding file) on_problems
## PRIVATE
@ -216,7 +216,7 @@ type Bytes
## PRIVATE
Implements the `File.read` for this `File_Format`
read : File -> Problem_Behavior -> Any
read self file on_problems =
read self file on_problems:Problem_Behavior =
_ = [on_problems]
file.read_bytes
@ -262,7 +262,7 @@ type JSON_Format
## PRIVATE
Implements the `File.read` for this `File_Format`
read : File -> Problem_Behavior -> Any
read self file on_problems =
read self file on_problems:Problem_Behavior =
_ = [on_problems]
text = file.read_text
Json.parse text . catch Invalid_JSON error->

View File

@ -120,14 +120,14 @@ type Output_Stream
Runs an action with a `ReportingStreamEncoder` encoding data to the
output stream with the specified encoding.
with_stream_encoder : Encoding -> Problem_Behavior -> (ReportingStreamEncoder -> Any) -> Any
with_stream_encoder self encoding on_problems action = self.with_java_stream java_stream->
with_stream_encoder self encoding:Encoding on_problems:Problem_Behavior action = self.with_java_stream java_stream->
## We ignore any warnings raised by the `bytes` method, because if the
original Unicode replacement character failed to encode, the `bytes`
method will have replaced it with the simple `?` sign which should be
available in all encodings. And this is exactly the behavior we want:
if available, we use the `<60>` character and otherwise we fallback to
the `?` character.
replacement_sequence = Encoding_Utils.INVALID_CHARACTER.bytes encoding on_problems=Problem_Behavior.Ignore
replacement_sequence = Encoding_Utils.INVALID_CHARACTER.bytes encoding on_problems=..Ignore
java_charset = encoding.to_java_charset
results = Encoding_Utils.with_stream_encoder java_stream java_charset replacement_sequence action
problems = Vector.from_polyglot_array results.problems . map Encoding_Error.Error