use std::borrow::Cow;
use std::fmt;
use std::num::NonZeroU8;
use crate::{Doc, TextLayout};
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct ByteSize(u64);
const SCALE: u64 = 1024;
const UNITS: [&str; 7] = ["B", "KB", "MB", "GB", "TB", "PB", "EB"];
impl ByteSize {
pub const fn bytes(value: u64) -> Self {
Self(value)
}
pub const fn kilobytes(value: u64) -> Option<Self> {
Self::scaled(value, 1)
}
pub const fn megabytes(value: u64) -> Option<Self> {
Self::scaled(value, 2)
}
pub const fn gigabytes(value: u64) -> Option<Self> {
Self::scaled(value, 3)
}
pub const fn terabytes(value: u64) -> Option<Self> {
Self::scaled(value, 4)
}
pub const fn petabytes(value: u64) -> Option<Self> {
Self::scaled(value, 5)
}
pub const fn as_u64(self) -> u64 {
self.0
}
const fn scaled(value: u64, exponent: u32) -> Option<Self> {
match SCALE.checked_pow(exponent) {
Some(multiplier) => match value.checked_mul(multiplier) {
Some(bytes) => Some(Self(bytes)),
None => None,
},
None => None,
}
}
}
impl From<u64> for ByteSize {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<ByteSize> for u64 {
fn from(value: ByteSize) -> Self {
value.0
}
}
impl fmt::Display for ByteSize {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut exponent = 0u32;
let mut divisor = 1u64;
while exponent + 1 < UNITS.len() as u32 {
match divisor.checked_mul(SCALE) {
Some(next) if self.0 >= next => {
divisor = next;
exponent += 1;
}
_ => break,
}
}
if exponent == 0 {
return write!(formatter, "{} B", self.0);
}
let mut whole = self.0 / divisor;
let remainder = u128::from(self.0 % divisor);
let divisor_wide = u128::from(divisor);
let mut hundredths = (remainder * 100 + divisor_wide / 2) / divisor_wide;
if hundredths == 100 {
hundredths = 0;
whole += 1;
}
if whole == SCALE && (exponent as usize) + 1 < UNITS.len() {
whole = 1;
exponent += 1;
}
write!(
formatter,
"{whole}.{hundredths:02} {}",
UNITS[exponent as usize]
)
}
}
const DEFAULT_TAB_WIDTH: NonZeroU8 = match NonZeroU8::new(4) {
Some(width) => width,
None => panic!("default tab width must be nonzero"),
};
pub fn word_wrapped(content: impl AsRef<str>) -> Doc {
let normalized = normalize_line_endings(content.as_ref());
if normalized.is_empty() {
return Doc::empty();
}
let lines = normalized
.split('\n')
.map(|line| Doc::fill(words(line)))
.collect::<Vec<_>>();
Doc::join(Doc::hard_line(), lines)
}
pub fn fixed_width(content: impl AsRef<str>) -> Doc {
fixed_width_with_tab_width(content, DEFAULT_TAB_WIDTH)
}
pub fn fixed_width_with_tab_width(content: impl AsRef<str>, tab_width: NonZeroU8) -> Doc {
Doc::from_text(content, TextLayout::Preformatted { tab_width })
}
pub fn lines(content: impl AsRef<str>) -> Vec<Doc> {
lines_with_tab_width(content, DEFAULT_TAB_WIDTH)
}
pub fn lines_with_tab_width(content: impl AsRef<str>, tab_width: NonZeroU8) -> Vec<Doc> {
let normalized = normalize_line_endings(content.as_ref());
if normalized.is_empty() {
return Vec::new();
}
normalized
.split('\n')
.map(|line| fixed_width_with_tab_width(line, tab_width))
.collect()
}
pub fn words(content: impl AsRef<str>) -> Vec<Doc> {
content.as_ref().split_whitespace().map(Doc::text).collect()
}
fn normalize_line_endings(value: &str) -> Cow<'_, str> {
if !value.contains('\r') {
return Cow::Borrowed(value);
}
let mut normalized = String::with_capacity(value.len());
let mut characters = value.chars().peekable();
while let Some(character) = characters.next() {
if character == '\r' {
if characters.peek() == Some(&'\n') {
characters.next();
}
normalized.push('\n');
} else {
normalized.push(character);
}
}
Cow::Owned(normalized)
}
#[cfg(feature = "json")]
pub mod json {
use serde::Serialize;
use serde_json::Value;
use crate::{Doc, MachineText, TextSafetyError, tokens};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Pretty {
indent: u16,
}
impl Default for Pretty {
fn default() -> Self {
Self { indent: 2 }
}
}
impl Pretty {
pub const fn new() -> Self {
Self { indent: 2 }
}
pub const fn indent(mut self, indent: u16) -> Self {
self.indent = indent;
self
}
pub const fn configured_indent(self) -> u16 {
self.indent
}
pub fn format<T: Serialize + ?Sized>(self, value: &T) -> Result<Doc, JsonFormatError> {
let value = serde_json::to_value(value).map_err(serialization_error)?;
Ok(Doc::tokens(
[tokens::SYNTAX_CODE, tokens::LANGUAGE_JSON],
value_doc(&value, self.indent),
))
}
}
pub fn pretty<T: Serialize + ?Sized>(value: &T) -> Result<Doc, JsonFormatError> {
Pretty::default().format(value)
}
pub fn ndjson<T: Serialize + ?Sized>(value: &T) -> Result<MachineText, JsonFormatError> {
let mut encoded = serde_json::to_string(value).map_err(serialization_error)?;
encoded.push('\n');
machine_text(encoded)
}
pub fn ndjson_records<I>(records: I) -> Result<MachineText, JsonFormatError>
where
I: IntoIterator,
I::Item: Serialize,
{
let mut encoded = String::new();
for record in records {
let record = serde_json::to_string(&record).map_err(serialization_error)?;
encoded.push_str(&record);
encoded.push('\n');
}
machine_text(encoded)
}
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
pub enum JsonFormatError {
#[error("failed to serialize JSON: {message}")]
Serialize { message: String },
#[error("serialized NDJSON is not valid machine text: {0}")]
UnsafeMachineText(TextSafetyError),
}
fn serialization_error(error: serde_json::Error) -> JsonFormatError {
JsonFormatError::Serialize {
message: error.to_string(),
}
}
fn machine_text(encoded: String) -> Result<MachineText, JsonFormatError> {
MachineText::parse(encoded).map_err(JsonFormatError::UnsafeMachineText)
}
fn value_doc(value: &Value, indent: u16) -> Doc {
match value {
Value::Null => atom(tokens::DATA_NULL, tokens::SYNTAX_CONSTANT, "null"),
Value::Bool(value) => atom(
tokens::DATA_BOOLEAN,
tokens::SYNTAX_BOOLEAN,
if *value { "true" } else { "false" },
),
Value::Number(value) => atom(
tokens::DATA_NUMBER,
tokens::SYNTAX_NUMBER,
value.to_string(),
),
Value::String(value) => atom(
tokens::DATA_STRING,
tokens::SYNTAX_STRING,
serde_json::to_string(value).expect("JSON strings always serialize"),
),
Value::Array(values) => {
let values = values.iter().map(|value| value_doc(value, indent));
delimited("[", "]", values, indent)
}
Value::Object(values) => {
let entries = values.iter().map(|(key, value)| {
let key = atom(
tokens::DATA_STRING,
tokens::SYNTAX_PROPERTY,
serde_json::to_string(key).expect("JSON object keys always serialize"),
);
Doc::concat([
key,
punctuation(tokens::SYNTAX_PUNCTUATION_DELIMITER, ":"),
Doc::nest(indent, Doc::concat([Doc::line(), value_doc(value, indent)])),
])
});
delimited("{", "}", entries, indent)
}
}
}
fn atom(
data_token: crate::TokenId,
syntax_token: crate::TokenId,
text: impl AsRef<str>,
) -> Doc {
Doc::tokens([tokens::DATA, data_token, syntax_token], Doc::text(text))
}
fn delimited(
open: &'static str,
close: &'static str,
values: impl IntoIterator<Item = Doc>,
indent: u16,
) -> Doc {
let values = values.into_iter().collect::<Vec<_>>();
let open = punctuation(tokens::SYNTAX_PUNCTUATION_BRACKET, open);
let close = punctuation(tokens::SYNTAX_PUNCTUATION_BRACKET, close);
if values.is_empty() {
return Doc::concat([open, close]);
}
let separator = Doc::concat([
punctuation(tokens::SYNTAX_PUNCTUATION_DELIMITER, ","),
Doc::line(),
]);
let body = Doc::join(separator, values);
Doc::group(Doc::concat([
open,
Doc::nest(indent, Doc::concat([Doc::soft_line(), body])),
Doc::soft_line(),
close,
]))
}
fn punctuation(token: crate::TokenId, text: &'static str) -> Doc {
Doc::token_text(token, text)
}
}
#[cfg(test)]
mod byte_size_tests {
use super::ByteSize;
#[test]
fn bytes_below_one_kilobyte_are_whole() {
assert_eq!(ByteSize::bytes(0).to_string(), "0 B");
assert_eq!(ByteSize::bytes(1).to_string(), "1 B");
assert_eq!(ByteSize::bytes(1023).to_string(), "1023 B");
}
#[test]
fn unit_boundaries_are_exact() {
assert_eq!(ByteSize::bytes(1024).to_string(), "1.00 KB");
assert_eq!(ByteSize::bytes(1024 * 1024).to_string(), "1.00 MB");
assert_eq!(ByteSize::bytes(1024 * 1024 * 1024).to_string(), "1.00 GB");
}
#[test]
fn rounding_carries_into_the_next_unit() {
let just_under = 1024 * 1024 - 1;
assert_eq!(ByteSize::bytes(just_under).to_string(), "1.00 MB");
assert_eq!(
ByteSize::bytes(1024 * 1024 * 1024 - 1).to_string(),
"1.00 GB"
);
}
#[test]
fn rounding_is_half_away_from_zero() {
assert_eq!(ByteSize::bytes(1029).to_string(), "1.00 KB");
assert_eq!(ByteSize::bytes(1034).to_string(), "1.01 KB");
}
#[test]
fn large_sizes_keep_full_integer_precision() {
let base = 9_007_199_254_740_993u64;
assert_eq!(ByteSize::bytes(base).as_u64(), base);
assert_eq!(ByteSize::bytes(u64::MAX).to_string(), "16.00 EB");
}
#[test]
fn the_reported_scan_size_renders_at_gigabyte_scale() {
assert_eq!(ByteSize::bytes(36_828_222_525).to_string(), "34.30 GB");
assert_eq!(ByteSize::bytes(81_618_377_035).to_string(), "76.01 GB");
assert_eq!(ByteSize::bytes(146_146_432).to_string(), "139.38 MB");
}
#[test]
fn unit_constructors_agree_with_byte_counts() {
assert_eq!(ByteSize::kilobytes(1), Some(ByteSize::bytes(1024)));
assert_eq!(
ByteSize::megabytes(2),
Some(ByteSize::bytes(2 * 1024 * 1024))
);
assert_eq!(
ByteSize::gigabytes(1)
.map(|size| size.to_string())
.as_deref(),
Some("1.00 GB")
);
assert_eq!(
ByteSize::petabytes(u64::MAX),
None,
"overflow is reported, not wrapped"
);
}
}