mirror of
https://github.com/nushell/nushell.git
synced 2025-05-05 23:42:56 +00:00
The original purpose of this PR was to modernize the external parser to use the new Shape system. This commit does include some of that change, but a more important aspect of this change is an improvement to the expansion trace. Previous commit 6a7c00ea adding trace infrastructure to the syntax coloring feature. This commit adds tracing to the expander. The bulk of that work, in addition to the tree builder logic, was an overhaul of the formatter traits to make them more general purpose, and more structured. Some highlights: - `ToDebug` was split into two traits (`ToDebug` and `DebugFormat`) because implementations needed to become objects, but a convenience method on `ToDebug` didn't qualify - `DebugFormat`'s `fmt_debug` method now takes a `DebugFormatter` rather than a standard formatter, and `DebugFormatter` has a new (but still limited) facility for structured formatting. - Implementations of `ExpandSyntax` need to produce output that implements `DebugFormat`. Unlike the highlighter changes, these changes are fairly focused in the trace output, so these changes aren't behind a flag.
63 lines
1.6 KiB
Rust
63 lines
1.6 KiB
Rust
use crate::data::base::Value;
|
|
use crate::prelude::*;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::fmt;
|
|
use std::str::FromStr;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
|
|
pub enum Unit {
|
|
B,
|
|
KB,
|
|
MB,
|
|
GB,
|
|
TB,
|
|
PB,
|
|
}
|
|
|
|
impl FormatDebug for Spanned<Unit> {
|
|
fn fmt_debug(&self, f: &mut DebugFormatter, source: &str) -> fmt::Result {
|
|
write!(f, "{}", self.span.slice(source))
|
|
}
|
|
}
|
|
|
|
impl Unit {
|
|
pub fn as_str(&self) -> &str {
|
|
match *self {
|
|
Unit::B => "B",
|
|
Unit::KB => "KB",
|
|
Unit::MB => "MB",
|
|
Unit::GB => "GB",
|
|
Unit::TB => "TB",
|
|
Unit::PB => "PB",
|
|
}
|
|
}
|
|
|
|
pub(crate) fn compute(&self, size: &Number) -> Value {
|
|
let size = size.clone();
|
|
|
|
Value::number(match self {
|
|
Unit::B => size,
|
|
Unit::KB => size * 1024,
|
|
Unit::MB => size * 1024 * 1024,
|
|
Unit::GB => size * 1024 * 1024 * 1024,
|
|
Unit::TB => size * 1024 * 1024 * 1024 * 1024,
|
|
Unit::PB => size * 1024 * 1024 * 1024 * 1024 * 1024,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl FromStr for Unit {
|
|
type Err = ();
|
|
fn from_str(input: &str) -> Result<Self, <Self as std::str::FromStr>::Err> {
|
|
match input {
|
|
"B" | "b" => Ok(Unit::B),
|
|
"KB" | "kb" | "Kb" | "K" | "k" => Ok(Unit::KB),
|
|
"MB" | "mb" | "Mb" => Ok(Unit::MB),
|
|
"GB" | "gb" | "Gb" => Ok(Unit::GB),
|
|
"TB" | "tb" | "Tb" => Ok(Unit::TB),
|
|
"PB" | "pb" | "Pb" => Ok(Unit::PB),
|
|
_ => Err(()),
|
|
}
|
|
}
|
|
}
|