code wiki / _hdl_build / nx_produce.nx

nx_produce.nx source

↩ module page · 129 lines · 8806 B

1// nx_produce.nx -- LIB: the CLEAN-PRODUCE catalog (R-IMPACT-2). Extends the impact model across the "Dirty 2// Dozen" (the most pesticide-heavy produce conventionally), proving the triple win at PRODUCT-LINE scale: a 3// whole basket of Nishi produce is cleaner + greener + cheaper for the customer, AND higher-margin for us. 4// Aggregates basket savings + our margin; renders a customer-facing storefront. Reuses nx_impact accessors. 5// license_tier: ORIGINAL 6import "nx_impact.nx" 7import "nx_food_science.nx" 8import "nx_seg_store.nx" 9import "nx_syscalls.nx" 10 11func pd_money(dst: *u8, off: i64, cents: i64) -> i64 { return im_money(dst, off, cents) } 12 13func pd_seed(prefix: *u8) -> i64 { 14 let keys: *i64 = sys_mmap(8 * 32) as *i64 15 let vals: *i64 = sys_mmap(8 * 32) as *i64 16 var n: i64 = 0 17 keys[n] = "food:impactids" as *u8 as i64 18 vals[n] = "strawberry\tspinach\tkale\tgrapes\tpeaches\tapples\tbellpepper\tblueberries" as *u8 as i64; n = n + 1 19 keys[n] = "food:impact:strawberry:conv" as *u8 as i64; vals[n] = "Conventional strawberries\t90\t1500\t300\t3\t600\t450\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 20 keys[n] = "food:impact:strawberry:nishi" as *u8 as i64; vals[n] = "Nishi strawberries\t5\t300\t180\t9\t450\t150\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 21 keys[n] = "food:impact:spinach:conv" as *u8 as i64; vals[n] = "Conventional spinach\t85\t800\t200\t3\t400\t300\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 22 keys[n] = "food:impact:spinach:nishi" as *u8 as i64; vals[n] = "Nishi spinach\t6\t200\t120\t9\t300\t100\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 23 keys[n] = "food:impact:kale:conv" as *u8 as i64; vals[n] = "Conventional kale\t80\t700\t180\t3\t350\t260\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 24 keys[n] = "food:impact:kale:nishi" as *u8 as i64; vals[n] = "Nishi kale\t5\t180\t110\t9\t280\t90\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 25 keys[n] = "food:impact:grapes:conv" as *u8 as i64; vals[n] = "Conventional grapes\t75\t1200\t250\t3\t500\t380\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 26 keys[n] = "food:impact:grapes:nishi" as *u8 as i64; vals[n] = "Nishi grapes\t7\t280\t150\t9\t400\t140\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 27 keys[n] = "food:impact:peaches:conv" as *u8 as i64; vals[n] = "Conventional peaches\t78\t1100\t280\t3\t450\t340\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 28 keys[n] = "food:impact:peaches:nishi" as *u8 as i64; vals[n] = "Nishi peaches\t6\t300\t160\t9\t350\t120\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 29 keys[n] = "food:impact:apples:conv" as *u8 as i64; vals[n] = "Conventional apples\t70\t600\t180\t3\t300\t230\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 30 keys[n] = "food:impact:apples:nishi" as *u8 as i64; vals[n] = "Nishi apples\t5\t200\t110\t9\t250\t90\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 31 keys[n] = "food:impact:bellpepper:conv" as *u8 as i64; vals[n] = "Conventional bell peppers\t72\t900\t220\t3\t350\t270\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 32 keys[n] = "food:impact:bellpepper:nishi" as *u8 as i64; vals[n] = "Nishi bell peppers\t4\t250\t130\t9\t270\t95\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 33 keys[n] = "food:impact:blueberries:conv" as *u8 as i64; vals[n] = "Conventional blueberries\t68\t1300\t320\t3\t700\t540\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 34 keys[n] = "food:impact:blueberries:nishi" as *u8 as i64; vals[n] = "Nishi blueberries\t6\t320\t170\t9\t550\t200\tperma_a8_regenerative_ag" as *u8 as i64; n = n + 1 35 36 let w: *i64 = ss_begin() 37 var tw: i64 = 0 38 var i: i64 = 0 39 while i < n { 40 let k: *u8 = keys[i] as *u8 41 let v: *u8 = vals[i] as *u8 42 let vl: i64 = as_len(v) 43 if fd_streq_store(prefix, k, v, vl) == 0 { ss_add(w, 1, k, v, vl); tw = tw + 1 } 44 i = i + 1 45 } 46 if tw > 0 { let seg: i64 = fd_seg_next(prefix); ss_commit(prefix, w, seg) } 47 return tw 48} 49 50func pd_list(prefix: *u8, outids: *i64) -> i64 { 51 let pq: *i64 = sys_mmap(16) as *i64 52 let lq: *i64 = sys_mmap(16) as *i64 53 if ss_get(prefix, "food:impactids" as *u8, pq, lq) != 1 { return 0 } 54 let buf: *u8 = pq[0] as *u8 55 let len: i64 = lq[0] 56 var c: i64 = 0 57 var i: i64 = 0 58 var ls: i64 = 0 59 while i <= len { 60 var sep: i64 = 0 61 if i == len { sep = 1 } else { if buf[i] == (9 as u8) { sep = 1 } } 62 if sep == 1 { 63 let tl: i64 = i - ls 64 if tl > 0 { 65 let tok: *u8 = sys_mmap(48) 66 var t: i64 = 0 67 while t < tl { tok[t] = buf[ls + t]; t = t + 1 } tok[tl] = 0 as u8 68 outids[c] = tok as i64; c = c + 1 69 } 70 ls = i + 1 71 } 72 i = i + 1 73 } 74 return c 75} 76 77// count how many catalog items satisfy the full triple win; total via out 78func pd_all_triple_win(prefix: *u8, total_out: *i64) -> i64 { 79 let ids: *i64 = sys_mmap(8 * 64) as *i64 80 let n: i64 = pd_list(prefix, ids) 81 total_out[0] = n 82 var wins: i64 = 0 83 var i: i64 = 0 84 while i < n { if im_triple_win(prefix, ids[i] as *u8) == 1 { wins = wins + 1 } i = i + 1 } 85 return wins 86} 87// total customer savings (cents) buying the whole Nishi basket instead of conventional 88func pd_basket_savings(prefix: *u8) -> i64 { 89 let ids: *i64 = sys_mmap(8 * 64) as *i64 90 let n: i64 = pd_list(prefix, ids) 91 var s: i64 = 0 92 var i: i64 = 0 93 while i < n { s = s + im_savings(prefix, ids[i] as *u8, "nishi" as *u8, "conv" as *u8); i = i + 1 } 94 return s 95} 96// our total margin (cents) on the basket for `src` 97func pd_basket_margin(prefix: *u8, src: *u8) -> i64 { 98 let ids: *i64 = sys_mmap(8 * 64) as *i64 99 let n: i64 = pd_list(prefix, ids) 100 var s: i64 = 0 101 var i: i64 = 0 102 while i < n { s = s + im_margin(prefix, ids[i] as *u8, src); i = i + 1 } 103 return s 104} 105 106// render the customer-facing CLEAN PRODUCE storefront (NUL-terminated); returns length. 107func pd_render_storefront(prefix: *u8, out: *u8) -> i64 { 108 var o: i64 = 0 109 o = as_append(out, o, "<!doctype html><html lang='en'><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'><title>Nishi clean produce</title><style>body{margin:0;font-family:system-ui,sans-serif;color:#1c2a1d;background:#f1f6ee;line-height:1.5}header{background:#2e6b34;color:#fff;padding:24px 22px}header h1{margin:0;font-size:1.5rem}header p{margin:4px 0 0;color:#d6ebcf}main{max-width:680px;margin:0 auto;padding:16px}table{border-collapse:collapse;width:100%;background:#fff;border-radius:12px;overflow:hidden}th,td{padding:8px 12px;text-align:left;border-top:1px solid #eef3ea}th{background:#e6efe1;color:#33503f}td.n{text-align:right;font-variant-numeric:tabular-nums}.was{color:#a23a2a;text-decoration:line-through}.now{color:#1f7a44;font-weight:700}.big{background:#fff;border-left:5px solid #2e6b34;border-radius:8px;padding:16px;margin-top:16px;font-size:1.05rem}.muted{color:#5b6a52}</style></head><body><header><h1>Nishi clean produce</h1><p>Cleaner, soil-building, and cheaper than the supermarket &mdash; the Dirty Dozen, done right.</p></header><main><table><tr><th>Item</th><th>Clean</th><th>Was</th><th>Now</th><th>Save</th></tr>" as *u8) 110 let ids: *i64 = sys_mmap(8 * 64) as *i64 111 let n: i64 = pd_list(prefix, ids) 112 var i: i64 = 0 113 while i < n { 114 let id: *u8 = ids[i] as *u8 115 o = as_append(out, o, "<tr><td>" as *u8) 116 let nm: *u8 = sys_mmap(48); im_field_str(prefix, id, "nishi" as *u8, 0, nm); o = as_append_escaped(out, o, nm, as_len(nm)) 117 o = as_append(out, o, "</td><td class='n now'>" as *u8); o = fd_apnum(out, o, im_clean(prefix, id, "nishi" as *u8)) 118 o = as_append(out, o, "</td><td class='n was'>" as *u8); o = pd_money(out, o, im_price(prefix, id, "conv" as *u8)) 119 o = as_append(out, o, "</td><td class='n now'>" as *u8); o = pd_money(out, o, im_price(prefix, id, "nishi" as *u8)) 120 o = as_append(out, o, "</td><td class='n now'>" as *u8); o = pd_money(out, o, im_savings(prefix, id, "nishi" as *u8, "conv" as *u8)) 121 o = as_append(out, o, "</td></tr>" as *u8) 122 i = i + 1 123 } 124 o = as_append(out, o, "</table><div class='big'>Buy the whole basket and save <b>" as *u8); o = pd_money(out, o, pd_basket_savings(prefix)) 125 o = as_append(out, o, "</b> over the supermarket &mdash; every item cleaner, every item building soil instead of poisoning it. You spend less and invest in your health and the planet.</div>" as *u8) 126 o = as_append(out, o, "<p class='muted'>Impact + clean figures are Nishi planning estimates grounded in the regenerative-agriculture corpus.</p></main></body></html>" as *u8) 127 out[o] = 0 as u8 128 return o 129}