aboutsummaryrefslogtreecommitdiffstats
path: root/src/sexpr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sexpr.rs')
-rw-r--r--src/sexpr.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/sexpr.rs b/src/sexpr.rs
index ea07bf9..d761192 100644
--- a/src/sexpr.rs
+++ b/src/sexpr.rs
@@ -10,13 +10,14 @@
*/
use std::str;
+use std::fmt;
use std::io::Read;
use std::fs::File;
use std::path::Path;
//////////// Types and Constants
-#[derive(Clone, PartialEq)]
+#[derive(Clone, PartialEq, Debug)]
pub enum SExpr {
SNull,
SBoolean(bool),
@@ -238,3 +239,21 @@ pub fn sexpr_parse_file(fpath: &Path) -> Result<(), String> {
let (_ast_list, _) = sexpr_parse(&tokens, 0)?;
Ok(())
}
+
+impl SExpr {
+ pub fn from_str(raw: &str) -> Result<SExpr, String> {
+ let tokens = sexpr_tokenize(&raw)?;
+ let (mut ast, _) = sexpr_parse(&tokens, 0)?;
+ Ok(ast.remove(0))
+ }
+}
+
+impl fmt::Display for SExpr {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match sexpr_repr(self) {
+ Ok(repr) => write!(f, "{}", repr),
+ Err(_) => Err(std::fmt::Error),
+ }
+ }
+}
+