code wiki / _hdl_build / nx_apistack_version.nx

nx_apistack_version.nx source

↩ module page · 31 lines · 1611 B

1// nx_apistack_version.nx -- CAP-API-VERSION: API version negotiation for /api. Reads the requested version from a 2// media type (Accept: application/vnd.nishi.v2+json) OR a URL path (/api/v3/..) via the "v<digits>" token, then 3// negotiates against the DATA-DRIVEN supported set: highest supported <= requested (latest if unspecified, lowest if 4// below all). Adding a version = a row in the supported set, zero rebuild = the sovereign exceed. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6 7// extract the version integer from a string via the "v<digits>" pattern; 0 if none. 8func ver_scan(s: *u8, n: i64) -> i64 { 9 var i: i64 = 0 10 while i < n - 1 { 11 if s[i] == (118 as u8) { // 'v' 12 let d0: i64 = (s[i + 1] as i64) & 0xff 13 if d0 >= 48 { if d0 <= 57 { 14 var v: i64 = 0; var j: i64 = i + 1; var go: i64 = 1 15 while go == 1 { if j < n { let c: i64 = (s[j] as i64) & 0xff; if c >= 48 { if c <= 57 { v = v * 10 + (c - 48); j = j + 1 } else { go = 0 } } else { go = 0 } } else { go = 0 } } 16 return v 17 } } 18 } 19 i = i + 1 20 } 21 return 0 22} 23 24// negotiate: highest supported <= requested; requested 0 -> latest (supported[n-1]); below all -> lowest (supported[0]). 25func ver_negotiate(requested: i64, supported: *i64, n: i64) -> i64 { 26 if requested == 0 { return supported[n - 1] } 27 var best: i64 = 0; var i: i64 = 0 28 while i < n { if supported[i] <= requested { if supported[i] > best { best = supported[i] } } i = i + 1 } 29 if best == 0 { return supported[0] } 30 return best 31}