aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorveradec <glitchmill.dyj3d@simplelogin.co>2026-09-12 12:23:33 +0530
committerveradec <glitchmill.dyj3d@simplelogin.co>2026-09-12 12:23:33 +0530
commit3d1ff942e53e05813fc18356aba5423e1722857e (patch)
tree47d33ddd7da62865ba1d1fdcbd69dd39ee19b758
parent04fc0bdcfd65d9e0c471a08d06b961c214242ad5 (diff)
feat: unwrap function
-rw-r--r--src/lib.rs53
-rw-r--r--src/main.rs5
2 files changed, 56 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 5104167..31f93ee 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,6 +12,47 @@ pub enum OkmlType {
Null,
}
+impl OkmlType {
+ pub fn unwrap_string(&self) -> &String {
+ match self {
+ OkmlType::String(s) => s,
+ _ => panic!("called `unwrap_string` on a non-String value"),
+ }
+ }
+
+ pub fn unwrap_bool(&self) -> bool {
+ match self {
+ OkmlType::Boolean(b) => *b,
+ _ => panic!("called `unwrap_bool` on a non-Boolean value"),
+ }
+ }
+
+ pub fn unwrap_integer(&self) -> i64 {
+ match self {
+ OkmlType::Integer(i) => *i,
+ _ => panic!("called `unwrap_integer` on a non-Integer value"),
+ }
+ }
+
+ pub fn unwrap_float(&self) -> f64 {
+ match self {
+ OkmlType::Float(f) => *f,
+ _ => panic!("called `unwrap_float` on a non-Float value"),
+ }
+ }
+
+ pub fn unwrap_sublist(&self) -> &Vec<Okml> {
+ match self {
+ OkmlType::SubList(v) => v,
+ _ => panic!("called `unwrap_sublist` on a non-SubList value"),
+ }
+ }
+
+ pub fn is_null(&self) -> bool {
+ matches!(self, OkmlType::Null)
+ }
+}
+
#[derive(Debug)]
pub struct Okml {
key: String,
@@ -38,6 +79,7 @@ impl Okml {
let (parsed_ast, _) = parse(tokens, 0);
parsed_ast
}
+
pub fn read_from_string(content: &str) -> Vec<Okml> {
let mut processed_content = pre_process(content.to_string());
let re = Regex::new(r"[ \t\n]+").unwrap();
@@ -45,6 +87,16 @@ impl Okml {
let (parsed_ast, _) = parse(tokens, 0);
parsed_ast
}
+
+ pub fn key(&self) -> &str{
+ let key = &self.key;
+ return &key[..key.len()-1]
+ }
+
+
+ pub fn value(&self) -> &OkmlType {
+ return &self.value
+ }
}
fn pre_process(mut content: String) -> String{
@@ -185,7 +237,6 @@ fn parse_list(tokens: Vec<&str>, mut index: usize) -> (Vec<Okml>, usize) {
});
index = new_index-1;
} else {
- // println!("{} => {} ", current, value);
if value == &"```" {
let mut _current: &str;
let mut result: String = Default::default();
diff --git a/src/main.rs b/src/main.rs
index e11c977..f933d12 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,5 +4,8 @@ use okaml::Okml;
fn main() {
let file_path = "../example/syntax.okml";
let parsed_ast:Vec<Okml> = Okml::read_from_file(file_path);
- println!("{:?}", parsed_ast);
+ // println!("{:?}", parsed_ast);
+ for item in parsed_ast {
+ println!("{} {:?}", item.key(), item.value());
+ }
}