Skip to content

Commit 3c71b22

Browse files
committed
feat: added to_value function to error
1 parent ecb40bb commit 3c71b22

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/error/mod.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use std::collections::HashMap;
12
use std::{
23
error::Error,
34
fmt::{Display, Formatter},
45
};
6+
use tucana::shared::value::Kind::{StringValue, StructValue};
7+
use tucana::shared::{Struct, Value};
58

69
#[derive(Debug, Default, Clone)]
710
pub struct RuntimeError {
@@ -38,8 +41,37 @@ impl RuntimeError {
3841
pub fn simple(name: &str, message: String) -> Self {
3942
Self {
4043
name: name.to_string(),
41-
message: message,
44+
message,
4245
suggestion: None,
4346
}
4447
}
48+
49+
pub fn as_value(&self) -> Value {
50+
let suggestion = match self.suggestion {
51+
Some(ref s) => Value {
52+
kind: Some(StringValue(s.clone())),
53+
},
54+
None => Value { kind: None },
55+
};
56+
57+
Value {
58+
kind: Some(StructValue(Struct {
59+
fields: HashMap::from([
60+
(
61+
String::from("name"),
62+
Value {
63+
kind: Some(StringValue(self.name.clone())),
64+
},
65+
),
66+
(
67+
String::from("message"),
68+
Value {
69+
kind: Some(StringValue(self.message.clone())),
70+
},
71+
),
72+
(String::from("suggestion"), suggestion),
73+
]),
74+
})),
75+
}
76+
}
4577
}

0 commit comments

Comments
 (0)