Skip to content

Commit

Permalink
Merge branch 'kbarros-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
singularitti committed Jan 24, 2024
2 parents b5d4e3f + b912ffb commit 357e58f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ version = "0.9.3"
CrystallographyCore = "80545937-1184-4bc9-b283-396e91386b5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructEquality = "6ec83bb0-ed9f-11e9-3b4c-2b04cb4e219c"
SumTypes = "8e1ec7a9-0e02-4297-b0fe-6433085c89f2"
spglib_jll = "ac4a9f1e-bdb2-5204-990c-47c8b2f70d4e"

[compat]
CrystallographyCore = "0.1, 0.2, 0.3"
StaticArrays = "0.8.3, 0.9, 0.10, 0.11, 0.12, 1.0"
StructEquality = "1, 2"
SumTypes = "0.4, 0.5"
julia = "1.3"
spglib_jll = "2"

Expand Down
35 changes: 20 additions & 15 deletions src/error.jl
Original file line number Diff line number Diff line change
@@ -1,41 +1,46 @@
using SumTypes: @sum_type

export get_error_code, get_error_message

# See https://www.mcobject.com/docs/Content/Programming/C/Return_Codes.htm or
# https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
"Represent various return codes from the Spglib library."
@sum_type SpglibReturnCode begin
SUCCESS
SPACEGROUP_SEARCH_FAILED
CELL_STANDARDIZATION_FAILED
SYMMETRY_OPERATION_SEARCH_FAILED
ATOMS_TOO_CLOSE
POINTGROUP_NOT_FOUND
NIGGLI_FAILED
DELAUNAY_FAILED
ARRAY_SIZE_SHORTAGE
NONE
@enum SpglibReturnCode::Int8 begin
SUCCESS = 0
SPACEGROUP_SEARCH_FAILED = 1
CELL_STANDARDIZATION_FAILED = 2
SYMMETRY_OPERATION_SEARCH_FAILED = 3
ATOMS_TOO_CLOSE = 4
POINTGROUP_NOT_FOUND = 5
NIGGLI_FAILED = 6
DELAUNAY_FAILED = 7
ARRAY_SIZE_SHORTAGE = 8
NONE = 9
end

struct SpglibError <: Exception
msg::String
end

# The return value for the C function `spg_get_error_code` is a C enum called
# `SpglibError`. The C compiler guarantees that enums are internally stored as
# type `int`, which Julia represents as `Cint`. The size of `int` is
# "implementation dependent". Frequently 4 or 8 bytes, but this depends on
# architecture and compiler details: https://stackoverflow.com/a/366033/500314.
# Directly casting this Cint into an @enum or SumTypes.jl value would be memory
# unsafe (see https://github.com/singularitti/Spglib.jl/issues/183).
"""
get_error_code()
Return an instance of the enumerated type `SpglibReturnCode`.
"""
get_error_code() = @ccall libsymspg.spg_get_error_code()::SpglibReturnCode
get_error_code() = SpglibReturnCode(@ccall libsymspg.spg_get_error_code()::Cint)

"""
get_error_message(code::SpglibReturnCode)
Get the corresponding error message for a given error code.
"""
get_error_message(code::SpglibReturnCode) =
unsafe_string(@ccall libsymspg.spg_get_error_message(code::SpglibReturnCode)::Cstring)
unsafe_string(@ccall libsymspg.spg_get_error_message(Cint(code)::Cint)::Cstring)

"""
check_error()
Expand Down

0 comments on commit 357e58f

Please sign in to comment.