mirror of
https://github.com/nushell/nushell.git
synced 2025-04-11 14:58:21 +02:00
This commit is more substantial than it looks: there was basically no real support for decimals before, and that impacted values all the way through. I also made Size contain a decimal instead of an integer (`1.6kb` is a reasonable thing to type), which impacted a bunch of code. The biggest impact of this commit is that it creates many more possible ways for valid nu types to fail to serialize as toml, json, etc. which typically can't support the full range of Decimal (or Bigint, which I also think we should support). This commit makes to-toml fallible, and a similar effort is necessary for the rest of the serializations. We also need to figure out how to clearly communicate to users what has happened, but failing to serialize to toml seems clearly superior to me than weird errors in basic math operations.
50 lines
1.1 KiB
Rust
50 lines
1.1 KiB
Rust
use crate::parser::parse::unit::*;
|
|
use crate::prelude::*;
|
|
use crate::{Span, Tagged, Text};
|
|
use std::fmt;
|
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
pub enum RawToken {
|
|
Number(Number),
|
|
Size(Number, Unit),
|
|
String(Span),
|
|
Variable(Span),
|
|
External(Span),
|
|
Bare,
|
|
}
|
|
|
|
impl RawToken {
|
|
pub fn type_name(&self) -> &'static str {
|
|
match self {
|
|
RawToken::Number(_) => "Number",
|
|
RawToken::Size(..) => "Size",
|
|
RawToken::String(_) => "String",
|
|
RawToken::Variable(_) => "Variable",
|
|
RawToken::External(_) => "External",
|
|
RawToken::Bare => "String",
|
|
}
|
|
}
|
|
}
|
|
|
|
pub type Token = Tagged<RawToken>;
|
|
|
|
impl Token {
|
|
pub fn debug<'a>(&self, source: &'a Text) -> DebugToken<'a> {
|
|
DebugToken {
|
|
node: *self,
|
|
source,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct DebugToken<'a> {
|
|
node: Token,
|
|
source: &'a Text,
|
|
}
|
|
|
|
impl fmt::Debug for DebugToken<'_> {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{}", self.node.span().slice(self.source))
|
|
}
|
|
}
|