From 3d1ff942e53e05813fc18356aba5423e1722857e Mon Sep 17 00:00:00 2001 From: veradec Date: Sat, 12 Sep 2026 12:23:33 +0530 Subject: feat: unwrap function --- src/lib.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 5 ++++- 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 { + 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 { 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, 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::read_from_file(file_path); - println!("{:?}", parsed_ast); + // println!("{:?}", parsed_ast); + for item in parsed_ast { + println!("{} {:?}", item.key(), item.value()); + } } -- cgit v1.2.3