From 9f79eb253bb17424393b0396ffb41cb6193fe0ed Mon Sep 17 00:00:00 2001 From: Simone Carlo Surace Date: Thu, 30 Oct 2025 17:30:28 +0100 Subject: [PATCH 1/2] Move JLD2.jl dependency to a package extension --- Project.toml | 14 ++++++++++---- ext/StaticGraphsJLD2Ext.jl | 32 ++++++++++++++++++++++++++++++++ src/StaticGraphs.jl | 1 - src/persistence.jl | 30 +++++++----------------------- test/runtests.jl | 1 + 5 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 ext/StaticGraphsJLD2Ext.jl diff --git a/Project.toml b/Project.toml index a6ca72d..53fd368 100644 --- a/Project.toml +++ b/Project.toml @@ -1,19 +1,25 @@ name = "StaticGraphs" uuid = "4c8beaf5-199b-59a0-a7f2-21d17de635b6" -version = "0.3.1" +version = "0.4.0" [deps] Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" -JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" +[weakdeps] +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" + +[extensions] +StaticGraphsJLD2Ext = "JLD2" + [compat] Graphs = "1.4" JLD2 = "0.1 - 0.6" -julia = "1" +julia = "1.10" [extras] +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test"] +test = ["JLD2", "Test"] diff --git a/ext/StaticGraphsJLD2Ext.jl b/ext/StaticGraphsJLD2Ext.jl new file mode 100644 index 0000000..e011427 --- /dev/null +++ b/ext/StaticGraphsJLD2Ext.jl @@ -0,0 +1,32 @@ +module StaticGraphsJLD2Ext + +using JLD2 +using StaticGraphs + +function StaticGraphs.savesg(fn::AbstractString, g::StaticGraph) + f_ind = g.f_ind + f_vec = g.f_vec + @save fn f_vec f_ind + return 1 +end + +function StaticGraphs.savesg(fn::AbstractString, g::StaticDiGraph) + f_ind = g.f_ind + f_vec = g.f_vec + b_ind = g.b_ind + b_vec = g.b_vec + @save fn f_vec f_ind b_vec b_ind + return 1 +end + +function StaticGraphs.loadsg(fn::AbstractString, ::SGFormat) + @load fn f_vec f_ind + return StaticGraph(f_vec, f_ind) +end + +function StaticGraphs.loadsg(fn::AbstractString, ::SDGFormat) + @load fn f_vec f_ind b_vec b_ind + return StaticDiGraph(f_vec, f_ind, b_vec, b_ind) +end + +end \ No newline at end of file diff --git a/src/StaticGraphs.jl b/src/StaticGraphs.jl index d01d0cf..54a2a2e 100644 --- a/src/StaticGraphs.jl +++ b/src/StaticGraphs.jl @@ -1,7 +1,6 @@ module StaticGraphs using Graphs -using JLD2 using SparseArrays import Base: diff --git a/src/persistence.jl b/src/persistence.jl index 7ca59db..0840c1d 100644 --- a/src/persistence.jl +++ b/src/persistence.jl @@ -7,31 +7,15 @@ abstract type StaticGraphFormat <: AbstractGraphFormat end struct SGFormat <: StaticGraphFormat end struct SDGFormat <: StaticGraphFormat end -function savesg(fn::AbstractString, g::StaticGraph) - f_ind = g.f_ind - f_vec = g.f_vec - @save fn f_vec f_ind - return 1 +function loadsg(args...) + error("In order to load static graphs from binary files, you need to load the JLD2.jl \ + package") end -function savesg(fn::AbstractString, g::StaticDiGraph) - f_ind = g.f_ind - f_vec = g.f_vec - b_ind = g.b_ind - b_vec = g.b_vec - @save fn f_vec f_ind b_vec b_ind - return 1 -end - -function loadsg(fn::AbstractString, ::SGFormat) - @load fn f_vec f_ind - return StaticGraph(f_vec, f_ind) -end - -function loadsg(fn::AbstractString, ::SDGFormat) - @load fn f_vec f_ind b_vec b_ind - return StaticDiGraph(f_vec, f_ind, b_vec, b_ind) +function savesg(args...) + error("In order to save static graphs to binary files, you need to load the JLD2.jl \ + package") end loadgraph(fn::AbstractString, gname::String, s::StaticGraphFormat) = loadsg(fn, s) -savegraph(fn::AbstractString, g::AbstractStaticGraph) = savesg(fn, g) \ No newline at end of file +savegraph(fn::AbstractString, g::AbstractStaticGraph) = savesg(fn, g) \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index d770e89..df69e00 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,7 @@ using StaticGraphs using Graphs using Graphs.SimpleGraphs +using JLD2 using Test const testdir = dirname(@__FILE__) From fa8d0142e454c26ad3dce2f6012174359d2c6287 Mon Sep 17 00:00:00 2001 From: Simone Carlo Surace Date: Sat, 1 Nov 2025 11:29:23 +0100 Subject: [PATCH 2/2] Use error hint instead of explicit error The latter will lead to invalidation when the extension is loaded --- src/StaticGraphs.jl | 15 +++++++++++++++ src/persistence.jl | 11 ++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/StaticGraphs.jl b/src/StaticGraphs.jl index 54a2a2e..9c7261d 100644 --- a/src/StaticGraphs.jl +++ b/src/StaticGraphs.jl @@ -114,4 +114,19 @@ eltype(::Type{StaticEdgeIter{StaticDiGraph{T, U}}}) where T where U = StaticDiGr include("overrides.jl") +function __init__() + # Register error hint for the `loadsg` and `savesg` functions + if isdefined(Base.Experimental, :register_error_hint) + Base.Experimental.register_error_hint(MethodError) do io, exc, _, _ + if exc.f === loadsg + print(io, "\n\nIn order to load static graphs from binary files, you need \ + to load the JLD2.jl package.") + elseif exc.f === savesg + print(io,"\n\nIn order to save static graphs to binary files, you need to \ + load the JLD2.jl package.") + end + end + end +end + end # module diff --git a/src/persistence.jl b/src/persistence.jl index 0840c1d..b918796 100644 --- a/src/persistence.jl +++ b/src/persistence.jl @@ -7,15 +7,8 @@ abstract type StaticGraphFormat <: AbstractGraphFormat end struct SGFormat <: StaticGraphFormat end struct SDGFormat <: StaticGraphFormat end -function loadsg(args...) - error("In order to load static graphs from binary files, you need to load the JLD2.jl \ - package") -end - -function savesg(args...) - error("In order to save static graphs to binary files, you need to load the JLD2.jl \ - package") -end +function loadsg end +function savesg end loadgraph(fn::AbstractString, gname::String, s::StaticGraphFormat) = loadsg(fn, s) savegraph(fn::AbstractString, g::AbstractStaticGraph) = savesg(fn, g) \ No newline at end of file