قراءة قيم من وثيقة TOML، ومنها جدول متداخل
// قراءة قيم من وثيقة TOML، ومنها جدول متداخل.
استورد std.io.println;
استورد std.toml.parser;
عرف الرئيسية(): عدد {
دع المصدر: نص = "title = \"السلطان\"\nstable = true\n\n[build]\nstage = 2\n";
طابق حلل_تومل(&المصدر) مع
| نتيجة.خطأ(_) -> { اطبع_سطر("ملف TOML غير صالح"); أرجع 1; }
| نتيجة.نجح(الوثيقة) -> {
دع مفتاح_العنوان: نص = "title";
طابق قيمة_مفتاح_تومل(&الوثيقة, 0, &مفتاح_العنوان) مع
| اختياري.موجود(قيمة_تومل.سلسلة(العنوان_الرئيسي)) -> { اطبع_سطر(العنوان_الرئيسي); }
| _ -> { اطبع_سطر("لا يوجد عنوان"); };
دع مفتاح_البناء: نص = "build";
دع مفتاح_المرحلة: نص = "stage";
طابق جدول_مفتاح_تومل(&الوثيقة, 0, &مفتاح_البناء) مع
| اختياري.موجود(الجدول) -> {
طابق قيمة_مفتاح_تومل(&الوثيقة, الجدول, &مفتاح_المرحلة) مع
| اختياري.موجود(قيمة_تومل.عدد(المرحلة)) -> { اطبع_سطر(المرحلة); }
| _ -> { اطبع_سطر("لا توجد مرحلة"); };
}
| اختياري.لا_شيء -> { اطبع_سطر("لا يوجد جدول build"); };
};
أرجع 0;
}// Reading values from a TOML document, including a nested table.
import std.io.println;
import std.toml.parser;
def main(): int {
let source: string = "title = \"SultanC\"\nstable = true\n\n[build]\nstage = 2\n";
match parse_toml(&source) with
| result.err(_) -> { println("invalid toml"); return 1; }
| result.ok(document) -> {
let title_key: string = "title";
// Patterns use the canonical variant names; English variant aliases are
// accepted in expressions but not yet in patterns.
match toml_value_at(&document, 0, &title_key) with
| option.some(قيمة_تومل.سلسلة(title)) -> { println(title); }
| _ -> { println("no title"); };
let build_key: string = "build";
let stage_key: string = "stage";
match toml_table_at(&document, 0, &build_key) with
| option.some(table) -> {
match toml_value_at(&document, table, &stage_key) with
| option.some(قيمة_تومل.عدد(stage)) -> { println(stage); }
| _ -> { println("no stage"); };
}
| option.none -> { println("no build table"); };
};
return 0;
}
التشغيل
بالمفسر، دون ترجمة:
./build/sultanc run examples/ar/formats/toml.snأو ترجمه إلى ملف تنفيذي:
./build/sultanc examples/ar/formats/toml.sn -o build/example && ./build/example