# hadid-dotenv
Parse `.env` configuration text (`KEY=VALUE`, comments, single/double quotes) cleanly in Hadid.
## Installation
```sh
hadid add hadid-dotenv
```
## Usage
```hadid
import "hadid-dotenv" as dotenv;
let cfg = "# server\nPORT=8080\nAPP_NAME=\"Hadid API\"\n";
match (dotenv.dotenv_get(cfg, "APP_NAME")) {
Some(v) => { print(v); }
None => { print("missing"); }
}
```
Tüm HADID Paketleri
hadid-dotenv
v0.1.0BLAKE2b-256Parse .env configuration strings and files cleanly in Hadid
# hadid-dotenv
Parse `.env` configuration text (`KEY=VALUE`, comments, single/double quotes) cleanly in Hadid.
## Installation
```sh
hadid add hadid-dotenv
```
## Usage
```hadid
import "hadid-dotenv" as dotenv;
let cfg = "# server\nPORT=8080\nAPP_NAME=\"Hadid API\"\n";
match (dotenv.dotenv_get(cfg, "APP_NAME")) {
Some(v) => { print(v); }
None => { print("missing"); }
}
```
[package]
name = "hadid-dotenv"
version = "0.1.0"
entry = "src/lib.had"
description = "Parse .env configuration strings and files cleanly in Hadid"
license = "MIT"
keywords = "dotenv,env,config,settings"
repository = "https://github.com/DGTLHLHAT/hadid"
[dependencies]
// hadid-dotenv: .env configuration parser for Hadid
fn trim_ascii(s: string): string {
let n = length(s);
i32 start = 0;
while (start < n) {
let ch = s[start];
if (ch == 32) { start = start + 1; }
else if (ch == 9) { start = start + 1; }
else if (ch == 13) { start = start + 1; }
else { break; }
}
i32 end = n;
while (end > start) {
let ch = s[end - 1];
if (ch == 32) { end = end - 1; }
else if (ch == 9) { end = end - 1; }
else if (ch == 13) { end = end - 1; }
else { break; }
}
if (end <= start) { return ""; }
match (text_slice(s, start, end - start)) {
Ok(sub) => { return sub; }
Err(e) => { return ""; }
}
}
fn unquote_val(v: string): string {
let n = length(v);
if (n >= 2) {
let first = v[0];
let last = v[n - 1];
if (first == 34) {
if (last == 34) {
match (text_slice(v, 1, n - 2)) {
Ok(inner) => { return inner; }
Err(e) => { return v; }
}
}
}
if (first == 39) {
if (last == 39) {
match (text_slice(v, 1, n - 2)) {
Ok(inner) => { return inner; }
Err(e) => { return v; }
}
}
}
}
return v;
}
fn dotenv_get(content: string, target_key: string): Option<string> {
match (text_split(content, "\n")) {
Err(e) => { return Option<string>.None; }
Ok(lines) => {
let count = length(lines);
Option<string> found = Option<string>.None;
for (let i = 0; i < count; i = i + 1) {
let line = trim_ascii(lines[i]);
let ln = length(line);
if (ln == 0) { continue; }
if (line[0] == 35) { continue; }
match (text_find(line, "=")) {
Err(e) => { continue; }
Ok(opt_eq) => {
match (opt_eq) {
None => { continue; }
Some(eq_pos) => {
if (eq_pos <= 0) { continue; }
let key_part = "";
match (text_slice(line, 0, eq_pos)) {
Ok(kp) => { key_part = trim_ascii(kp); }
Err(e) => { continue; }
}
if (key_part == target_key) {
let val_part = "";
if (eq_pos + 1 < ln) {
match (text_slice(line, eq_pos + 1, ln - eq_pos - 1)) {
Ok(vp) => { val_part = unquote_val(trim_ascii(vp)); }
Err(e) => {}
}
}
found = Option<string>.Some(val_part);
}
}
}
}
}
}
return found;
}
}
}
fn dotenv_get_or(content: string, key: string, fallback: string): string {
match (dotenv_get(content, key)) {
Some(v) => { return v; }
None => { return fallback; }
}
}
fn dotenv_has(content: string, key: string): bool {
match (dotenv_get(content, key)) {
Some(v) => { return true; }
None => { return false; }
}
}
| Sürüm | Boyut | İndirme | BLAKE2b-256 | Durum |
|---|---|---|---|---|
| v0.1.0 | 4745 B | 0 | blake2b-256:65e0203f1d3f… | Aktif |