diff mbox series

fetch2/crate: replace instance method monkey-patching with URL dispatch

Message ID 20260507142924.916471-1-thomas.perrot@bootlin.com
State Rejected
Headers show
Series fetch2/crate: replace instance method monkey-patching with URL dispatch | expand

Commit Message

Thomas Perrot May 7, 2026, 2:29 p.m. UTC
From: Thomas Perrot <thomas.perrot@bootlin.com>

Instead of overwriting self.latest_versionstring at init time to swap
in a different implementation, have latest_versionstring() inspect
ud.versionsurl and dispatch to the appropriate private helper.

This avoids surprising instance-level mutation and makes the branching
logic explicit and readable.

Signed-off-by: Thomas Perrot <thomas.perrot@bootlin.com>
---
 lib/bb/fetch2/crate.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/lib/bb/fetch2/crate.py b/lib/bb/fetch2/crate.py
index b46d4f1a9801..bb12f4e9b7c2 100644
--- a/lib/bb/fetch2/crate.py
+++ b/lib/bb/fetch2/crate.py
@@ -81,7 +81,6 @@  class Crate(Wget):
         if host == 'crates.io':
             ud.url = "https://static.crates.io/crates/%s/%s/download" % (name, version)
             ud.versionsurl = 'https://index.crates.io/' + self._generate_index_path(name)
-            self.latest_versionstring = self.latest_versionstring_from_index
         else:
             ud.url = "https://%s/%s/%s/download" % (host, name, version)
             ud.versionsurl = "https://%s/%s/versions" % (host, name)
@@ -158,7 +157,16 @@  class Crate(Wget):
 
     def latest_versionstring(self, ud, d):
         """
-        Return the latest version available when versionsurl is the [name]/versions URL.
+        Return the latest upstream version, dispatching to the appropriate
+        parser based on the versionsurl format.
+        """
+        if ud.versionsurl.startswith('https://index.crates.io/'):
+            return self._latest_versionstring_from_index(ud, d)
+        return self._latest_versionstring_from_api(ud, d)
+
+    def _latest_versionstring_from_api(self, ud, d):
+        """
+        Parse the latest version from a [name]/versions JSON API response.
         """
         json_data = json.loads(self._fetch_index(ud.versionsurl, ud, d))
         versions = [(0, i["num"], "") for i in json_data["versions"]]
@@ -166,10 +174,9 @@  class Crate(Wget):
 
         return (versions[-1][1], "") if versions else ("", "")
 
-    def latest_versionstring_from_index(self, ud, d):
+    def _latest_versionstring_from_index(self, ud, d):
         """
-        Return the latest version available when versionsurl is a Cargo index
-        file.
+        Parse the latest version from a Cargo sparse index file (NDJSON).
         https://doc.rust-lang.org/cargo/reference/registry-index.html#index-files
         """
         versions = []