gnu-units 0.2.0

Safe Rust bindings for the GNU units conversion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Parses the embedded `definitions.units` file and registers entries into
//! either the vendored C-library database (via FFI) or the pure-Rust native
//! database, depending on the active feature.
//!
//! Produces a sorted [`Vec<Definition>`] exposed through the public API.

use std::cmp::Ordering;
use std::collections::HashMap;
use std::sync::{LazyLock, RwLock};

#[cfg(feature = "vendored")]
use std::ffi::{CStr, CString};
#[cfg(feature = "vendored")]
use std::os::raw::c_int;
#[cfg(feature = "vendored")]
use std::sync::Mutex;

#[cfg(feature = "vendored")]
use crate::engine::ffi;

#[cfg(feature = "native")]
use crate::engine::native::database::{Database, init as db_init};

use crate::units;

/// The kind of a definition entry in the GNU units database.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DefinitionKind {
    /// A plain unit (e.g. `meter`, `second`).
    Unit,
    /// A unit prefix (e.g. `kilo-`, `mega-`).
    Prefix,
    /// A conversion function (e.g. `tempC(x)`).
    Function,
    /// A piecewise unit table (e.g. `dBV[...]`).
    Table,
    /// A unit list alias (defined via `!unitlist`).
    Alias,
}

impl DefinitionKind {
    fn from_name(name: &str) -> DefinitionKind {
        match name {
            _ if name.ends_with('-') => DefinitionKind::Prefix,
            _ if name.contains('[') => DefinitionKind::Table,
            _ if name.contains('(') => DefinitionKind::Function,
            _ => DefinitionKind::Unit,
        }
    }
}

/// A single entry from the GNU units definitions database.
#[derive(Debug, Clone, Eq)]
pub struct Definition {
    /// The name of the unit, prefix, function, table, or alias.
    pub name: String,
    /// The definition string for this entry.
    pub definition: String,
    /// What kind of definition this is.
    pub kind: DefinitionKind,
}

impl PartialEq for Definition {
    fn eq(&self, other: &Self) -> bool {
        self.canonical_name() == other.canonical_name() && self.kind == other.kind
    }
}

impl Ord for Definition {
    fn cmp(&self, other: &Self) -> Ordering {
        self.canonical_name()
            .cmp(other.canonical_name())
            .then_with(|| self.kind.cmp(&other.kind))
    }
}

impl PartialOrd for Definition {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Definition {
    /// Returns the canonical lookup name used for deduplication:
    /// - Prefixes: trailing `-` stripped (`kilo-` → `kilo`)
    /// - Functions: stripped from `(` onward (`tempC(x)` → `tempC`)
    /// - Tables: stripped from `[` onward (`gasmark[degR]` → `gasmark`)
    /// - Units and aliases: full name as-is
    pub fn canonical_name(&self) -> &str {
        if let Some(idx) = self.name.find('(') {
            &self.name[..idx]
        } else if let Some(idx) = self.name.find('[') {
            &self.name[..idx]
        } else {
            self.name.trim_end_matches('-')
        }
    }
}

/// Sorted list of all known definitions, populated once at start-up.
pub(crate) static DEFINITIONS: LazyLock<RwLock<Vec<Definition>>> = LazyLock::new(|| {
    #[cfg(feature = "vendored")]
    {
        let _guard = ffi::lock();
        // SAFETY: static C strings live for the process lifetime.
        // LazyLock guarantees single-threaded execution here.
        unsafe {
            gnu_units_sys::mylocale = c"en_US".as_ptr() as *mut std::os::raw::c_char;
            gnu_units_sys::progname = c"gnu-units".as_ptr() as *mut std::os::raw::c_char;
            gnu_units_sys::utf8mode = 1;
        }
    }

    #[cfg(feature = "native")]
    let mut native_db = Database::default();

    let content = include_str!("../data/definitions.units");
    let mut env: HashMap<String, String> = HashMap::new();

    #[cfg(feature = "vendored")]
    let mut defs = load_core(content, c"definitions.units", &mut env);
    #[cfg(feature = "native")]
    let mut defs = load_core(content, c"definitions.units", &mut env, &mut native_db);

    #[cfg(feature = "native")]
    db_init(native_db);

    // Emulate C last-write-wins: reverse so last-in-file entries come first.
    defs.reverse();
    defs.sort();
    defs.dedup();
    defs.sort_by(|a, b| a.name.cmp(&b.name));
    RwLock::new(defs)
});

/// Forces the definitions to be loaded exactly once.
pub(crate) fn ensure_definitions() {
    LazyLock::force(&DEFINITIONS);
}

#[cfg(feature = "vendored")]
static FILE_PTRS: LazyLock<Mutex<HashMap<Vec<u8>, usize>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

#[cfg(feature = "vendored")]
fn intern_filename(filename: &CStr) -> *mut std::os::raw::c_char {
    let key = filename.to_bytes().to_vec();
    let mut map = FILE_PTRS.lock().unwrap_or_else(|e| e.into_inner());
    let addr = *map
        .entry(key)
        .or_insert_with(|| CString::new(filename.to_bytes()).unwrap().into_raw() as usize);
    addr as *mut std::os::raw::c_char
}

pub(crate) fn replace_operators(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        let replacement = match ch {
            '\u{2012}' | '\u{2013}' | '\u{2212}' => "-",
            '\u{00D7}' | '\u{2A09}' | '\u{00B7}' | '\u{22C5}' => "*",
            '\u{00F7}' | '\u{2215}' => "/",
            '\u{2044}' => "|",
            '\u{00A0}'
            | '\u{1680}'
            | '\u{2000}'..='\u{200A}'
            | '\u{202F}'
            | '\u{205F}'
            | '\u{3000}' => " ",
            '\u{200B}' | '\u{200C}' => "",
            _ => {
                out.push(ch);
                continue;
            }
        };
        out.push_str(replacement);
    }
    out
}

#[cfg_attr(not(feature = "currency-update"), allow(dead_code))]
pub(crate) fn load_definitions(content: &str, filename: &std::ffi::CStr) -> Vec<Definition> {
    let mut env: HashMap<String, String> = HashMap::new();
    #[cfg(feature = "vendored")]
    {
        load_core(content, filename, &mut env)
    }
    #[cfg(feature = "native")]
    {
        let _ = filename;
        let db_rw = crate::engine::native::database::get();
        let mut db = db_rw.write().unwrap_or_else(|e| e.into_inner());
        load_core(content, c"", &mut env, &mut db)
    }
}

#[cfg(feature = "vendored")]
fn load_core(
    content: &str,
    filename: &std::ffi::CStr,
    env: &mut HashMap<String, String>,
) -> Vec<Definition> {
    let file_ptr = intern_filename(filename);
    // SAFETY: utf8mode is a simple i32 global set during initialization.
    // It is only read here after initialization is complete.
    let utf8mode = unsafe { gnu_units_sys::utf8mode };
    load_lines_vendored(content, env, file_ptr, utf8mode)
}

#[cfg(feature = "vendored")]
fn load_lines_vendored(
    content: &str,
    env: &mut HashMap<String, String>,
    file_ptr: *mut std::os::raw::c_char,
    utf8mode: i32,
) -> Vec<Definition> {
    let mut results = Vec::new();
    let mut wrong_locale = false;
    let mut in_utf8 = false;
    let mut wrong_var = false;

    for (linenum, raw_line) in join_continuations(content) {
        let line = strip_comment(&raw_line);
        let line = line.trim();

        if let Some(rest) = line.strip_prefix('!') {
            let (directive, arg) = split2(rest.trim());
            match directive {
                "locale" => {
                    wrong_locale = arg != "en_US";
                }
                "endlocale" => {
                    wrong_locale = false;
                }
                "utf8" => {
                    in_utf8 = true;
                }
                "endutf8" => {
                    in_utf8 = false;
                }
                "var" => {
                    let (vname, vals) = split2(arg);
                    wrong_var = match lookup_var(vname, env) {
                        Some(v) => !vals.split_whitespace().any(|w| w == v),
                        None => true,
                    };
                }
                "varnot" => {
                    let (vname, vals) = split2(arg);
                    wrong_var = match lookup_var(vname, env) {
                        Some(v) => vals.split_whitespace().any(|w| w == v),
                        None => true,
                    };
                }
                "endvar" => {
                    wrong_var = false;
                }
                "set" if !wrong_locale && !wrong_var => {
                    let (vname, val) = split2(arg);
                    if lookup_var(vname, env).is_none() {
                        env.insert(vname.to_owned(), val.to_owned());
                    }
                }
                "set" | "message" | "prompt" => {}
                "unitlist" if !(wrong_locale || wrong_var || in_utf8 && utf8mode == 0) => {
                    let (name, def) = split2(arg);
                    if !name.is_empty() {
                        ffi::newalias(name, def, linenum as c_int, file_ptr);
                        results.push(Definition {
                            name: name.to_owned(),
                            definition: def.to_owned(),
                            kind: DefinitionKind::Alias,
                        });
                    }
                }
                "unitlist" => {}
                "include" if !(wrong_locale || wrong_var || in_utf8 && utf8mode == 0) => {
                    results.extend(include_vendored(arg, env, utf8mode));
                }
                "include" => {}
                _ => {}
            }
            continue;
        }

        if wrong_locale || wrong_var || (in_utf8 && utf8mode == 0) {
            continue;
        }

        let norm = replace_operators(line);
        let line = norm.trim();
        if line.is_empty() {
            continue;
        }

        let (raw_name, def) = split2(line);
        if raw_name == "-" || def.is_empty() {
            continue;
        }
        let name = raw_name.strip_prefix('+').unwrap_or(raw_name);
        if name.is_empty() {
            continue;
        }

        let lnum = linenum as c_int;
        if name.ends_with('-') {
            ffi::newprefix(name, def, lnum, file_ptr);
        } else if name.contains('[') {
            ffi::newtable(name, def, lnum, file_ptr);
        } else if name.contains('(') {
            ffi::newfunction(name, def, lnum, file_ptr);
        } else {
            ffi::newunit(name, def, lnum, file_ptr);
        }

        results.push(Definition {
            name: name.to_owned(),
            definition: def.to_owned(),
            kind: DefinitionKind::from_name(name),
        });
    }
    results
}

#[cfg(feature = "vendored")]
fn include_vendored(
    arg: &str,
    env: &mut HashMap<String, String>,
    utf8mode: i32,
) -> Vec<Definition> {
    let (content, filename): (&str, &std::ffi::CStr) = match arg {
        "elements.units" => (units::ELEMENTS, c"elements.units"),
        #[cfg(feature = "currency-update")]
        "currency.units" => (units::CURRENCY, c"currency.units"),
        #[cfg(feature = "currency-update")]
        "crypto.units" => (units::CRYPTO, c"crypto.units"),
        #[cfg(feature = "currency-update")]
        "metal_prices.units" => (units::METAL_PRICES, c"metal_prices.units"),
        #[cfg(feature = "currency-update")]
        "cpi.units" => (units::CPI, c"cpi.units"),
        _ => return vec![],
    };
    let file_ptr = intern_filename(filename);
    load_lines_vendored(content, env, file_ptr, utf8mode)
}

#[cfg(feature = "native")]
fn load_core(
    content: &str,
    _filename: &std::ffi::CStr,
    env: &mut HashMap<String, String>,
    db: &mut Database,
) -> Vec<Definition> {
    let mut results = Vec::new();
    let mut wrong_locale = false;
    let mut in_utf8 = false;
    let mut wrong_var = false;
    const UTF8MODE: i32 = 1;

    for (_linenum, raw_line) in join_continuations(content) {
        let line = strip_comment(&raw_line);
        let line = line.trim();

        if let Some(rest) = line.strip_prefix('!') {
            let (directive, arg) = split2(rest.trim());
            match directive {
                "locale" => {
                    wrong_locale = arg != "en_US";
                }
                "endlocale" => {
                    wrong_locale = false;
                }
                "utf8" => {
                    in_utf8 = true;
                }
                "endutf8" => {
                    in_utf8 = false;
                }
                "var" => {
                    let (vname, vals) = split2(arg);
                    wrong_var = match lookup_var(vname, env) {
                        Some(v) => !vals.split_whitespace().any(|w| w == v),
                        None => true,
                    };
                }
                "varnot" => {
                    let (vname, vals) = split2(arg);
                    wrong_var = match lookup_var(vname, env) {
                        Some(v) => vals.split_whitespace().any(|w| w == v),
                        None => true,
                    };
                }
                "endvar" => {
                    wrong_var = false;
                }
                "set" if !wrong_locale && !wrong_var => {
                    let (vname, val) = split2(arg);
                    if lookup_var(vname, env).is_none() {
                        env.insert(vname.to_owned(), val.to_owned());
                    }
                }
                "set" | "message" | "prompt" => {}
                "unitlist" if !(wrong_locale || wrong_var || in_utf8 && UTF8MODE == 0) => {
                    let (name, def) = split2(arg);
                    if !name.is_empty() {
                        results.push(Definition {
                            name: name.to_owned(),
                            definition: def.to_owned(),
                            kind: DefinitionKind::Alias,
                        });
                    }
                }
                "unitlist" => {}
                "include" if !(wrong_locale || wrong_var || in_utf8 && UTF8MODE == 0) => {
                    results.extend(include_native(arg, env, db));
                }
                "include" => {}
                _ => {}
            }
            continue;
        }

        if wrong_locale || wrong_var || (in_utf8 && UTF8MODE == 0) {
            continue;
        }

        let norm = replace_operators(line);
        let line = norm.trim();
        if line.is_empty() {
            continue;
        }

        let (raw_name, def) = split2(line);
        if raw_name == "-" || def.is_empty() {
            continue;
        }
        let name = raw_name.strip_prefix('+').unwrap_or(raw_name);
        if name.is_empty() {
            continue;
        }

        match name {
            _ if name.ends_with('-') => db.insert_prefix(name, def),
            _ if name.contains('[') => db.insert_table(name, def),
            _ if name.contains('(') => db.insert_function(name, def),
            _ => db.insert_unit(name, def),
        }

        results.push(Definition {
            name: name.to_owned(),
            definition: def.to_owned(),
            kind: DefinitionKind::from_name(name),
        });
    }
    results
}

#[cfg(feature = "native")]
fn include_native(
    arg: &str,
    env: &mut HashMap<String, String>,
    db: &mut Database,
) -> Vec<Definition> {
    let (content, _filename): (&str, &std::ffi::CStr) = match arg {
        "elements.units" => (units::ELEMENTS, c"elements.units"),
        #[cfg(feature = "currency-update")]
        "currency.units" => (units::CURRENCY, c"currency.units"),
        #[cfg(feature = "currency-update")]
        "crypto.units" => (units::CRYPTO, c"crypto.units"),
        #[cfg(feature = "currency-update")]
        "metal_prices.units" => (units::METAL_PRICES, c"metal_prices.units"),
        #[cfg(feature = "currency-update")]
        "cpi.units" => (units::CPI, c"cpi.units"),
        _ => return vec![],
    };
    load_core(content, c"", env, db)
}

fn join_continuations(content: &str) -> Vec<(usize, String)> {
    let mut lines = Vec::new();
    let mut iter = content.lines().enumerate();
    while let Some((idx, line)) = iter.next() {
        let line = if idx == 0 {
            line.trim_start_matches('\u{FEFF}')
        } else {
            line
        };
        let mut buf = line.to_string();
        while buf.ends_with('\\')
            && let Some((_, next)) = iter.next()
        {
            buf.pop();
            buf.push_str(next.trim_start());
        }
        lines.push((idx + 1, buf));
    }
    lines
}

fn strip_comment(line: &str) -> &str {
    if let Some(pos) = line.find('#') {
        return &line[..pos];
    }
    line
}

fn split2(s: &str) -> (&str, &str) {
    let mut parts = s.splitn(2, char::is_whitespace);
    let k = parts.next().unwrap_or("");
    let v = parts.next().unwrap_or("").trim();
    (k, v)
}

fn lookup_var(name: &str, env: &HashMap<String, String>) -> Option<String> {
    env.get(name).cloned().or_else(|| std::env::var(name).ok())
}