1pub const VERSION: &str = env!("CARGO_PKG_VERSION");
4
5#[macro_export]
6macro_rules! bail { ($msg:expr) => { panic!("{}", $msg) }; }
7#[macro_export]
8macro_rules! ensure { ($cond:expr, $msg:expr) => { if !$cond { panic!("{}", $msg) } }; }
9
10pub mod primitive {
11 pub type Entero64 = i64;
12 pub type Flotante64 = f64;
13 pub type Booleano = bool;
14 pub type Texto = String;
15 pub type Byte = u8;
16 pub type Void = ();
17
18 pub trait SilPrimitive: Sized + Clone {
19 fn to_sil_string(&self) -> String;
20 fn from_sil_string(s: &str) -> Result<Self, &'static str>;
21 }
22
23 impl SilPrimitive for i64 {
24 fn to_sil_string(&self) -> String { self.to_string() }
25 fn from_sil_string(s: &str) -> Result<Self, &'static str> {
26 s.parse().map_err(|_| "Invalid Entero64")
27 }
28 }
29
30 impl SilPrimitive for f64 {
31 fn to_sil_string(&self) -> String { self.to_string() }
32 fn from_sil_string(s: &str) -> Result<Self, &'static str> {
33 s.parse().map_err(|_| "Invalid Flotante64")
34 }
35 }
36
37 impl SilPrimitive for bool {
38 fn to_sil_string(&self) -> String { self.to_string() }
39 fn from_sil_string(s: &str) -> Result<Self, &'static str> {
40 match s {
41 "verdadero" | "true" | "1" => Ok(true),
42 "falso" | "false" | "0" => Ok(false),
43 _ => Err("Invalid Booleano"),
44 }
45 }
46 }
47
48 impl SilPrimitive for String {
49 fn to_sil_string(&self) -> String { self.clone() }
50 fn from_sil_string(s: &str) -> Result<Self, &'static str> { Ok(s.to_string()) }
51 }
52}
53
54pub mod error {
55 #[derive(Debug, Clone)]
56 pub struct SilError {
57 msg: String,
58 }
59
60 impl SilError {
61 pub fn new(msg: &str) -> Self {
62 Self { msg: msg.to_string() }
63 }
64 }
65
66 impl core::fmt::Display for SilError {
67 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
68 write!(f, "{}", self.msg)
69 }
70 }
71
72 impl std::error::Error for SilError {}
73
74 pub type SilResult<T> = Result<T, SilError>;
75}
76
77pub mod option {
78 pub use core::option::Option::{self, Some, None};
79
80 pub trait OptionExt<T> {
81 fn unwrap_or(self, default: T) -> T;
82 fn expect(self, msg: &str) -> T;
83 }
84
85 impl<T> OptionExt<T> for Option<T> {
86 fn unwrap_or(self, default: T) -> T { self.unwrap_or(default) }
87 fn expect(self, msg: &str) -> T { self.expect(msg) }
88 }
89}
90
91pub mod result {
92 pub use core::result::Result::{self, Ok, Err};
93
94 pub trait ResultExt<T, E> {
95 fn unwrap_or(self, default: T) -> T;
96 fn map<U>(self, f: impl FnOnce(T) -> U) -> Result<U, E>;
97 }
98
99 impl<T, E> ResultExt<T, E> for Result<T, E> {
100 fn unwrap_or(self, default: T) -> T { self.unwrap_or(default) }
101 fn map<U>(self, f: impl FnOnce(T) -> U) -> Result<U, E> { self.map(f) }
102 }
103}
104
105pub mod prelude {
106 pub use crate::primitive::{Entero64, Flotante64, Booleano, Texto, Byte, Void, SilPrimitive};
107 pub use crate::error::{SilError, SilResult};
108 pub use crate::option::{Option, Some, None, OptionExt};
109 pub use crate::result::{Result, Ok, Err, ResultExt};
110 pub use crate::{bail, ensure};
111}
112
113pub use prelude::*;
114pub use primitive::{Entero64, Flotante64, Booleano, Texto, Byte, Void, SilPrimitive};
115pub use error::{SilError, SilResult};
116pub use option::{Option, Some, None, OptionExt};
117pub use result::{Result, Ok, Err, ResultExt};