Skip to content

Commit

Permalink
Use Option to express no limit on precision or item count
Browse files Browse the repository at this point in the history
  • Loading branch information
vasekp committed Feb 19, 2025
1 parent 426daee commit f6ee7e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 18 additions & 17 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ impl Item {
}
}

pub fn format(&self, max_len: usize) -> (String, Option<StreamError>) {
pub fn format(&self, max_len: Option<usize>) -> (String, Option<StreamError>) {
struct Stateful<'item> {
item: &'item Item,
cell: Cell<Option<StreamError>>
Expand All @@ -635,7 +635,10 @@ impl Item {
}

let s = Stateful{item: self, cell: Default::default()};
let result = format!("{:.*}", max_len, s);
let result = match max_len {
Some(width) => format!("{s:.*}", width),
None => format!("{s}")
};
(result, s.cell.take())
}

Expand Down Expand Up @@ -816,14 +819,14 @@ impl dyn Stream {
{
let mut iter = self.iter();
let (prec, max) = match f.precision() {
Some(prec) => (std::cmp::max(prec, 4), usize::MAX),
None => (usize::MAX, 3)
Some(prec) => (Some(std::cmp::max(prec, 4)), None),
None => (None, Some(3))
};
let mut s = String::new();
let mut i = 0;
s.push('[');
'a: {
while s.len() < prec && i < max {
while prec.is_none_or(|prec| s.len() < prec) && max.is_none_or(|max| i < max) {
match iter.next() {
None => {
s.push(']');
Expand All @@ -834,7 +837,7 @@ impl dyn Stream {
if i > 0 {
s += ", ";
}
let (string, err) = item.format(prec - plen);
let (string, err) = item.format(prec.map(|prec| prec - plen));
s += &string;
if err.is_some() {
error.set(err);
Expand All @@ -857,10 +860,9 @@ impl dyn Stream {
Some(_) => ", ..."
};
}
if s.len() < prec {
write!(f, "{}", s)
} else {
write!(f, "{:.*}...", prec - 3, s)
match prec {
Some(prec) if prec < s.len() => write!(f, "{:.*}...", prec - 3, s),
_ => write!(f, "{}", s)
}
}

Expand All @@ -869,14 +871,14 @@ impl dyn Stream {
{
let mut iter = self.string_iter();
let (prec, max) = match f.precision() {
Some(prec) => (std::cmp::max(prec, 4), usize::MAX),
None => (usize::MAX, 20)
Some(prec) => (Some(std::cmp::max(prec, 4)), None),
None => (None, Some(20))
};
let mut s = String::new();
let mut i = 0;
s.push('"');
'a: {
while s.len() < prec && i < max {
while prec.is_none_or(|prec| s.len() < prec) && max.is_none_or(|max| i < max) {
if let Some(next) = iter.next() {
match next {
Ok(ch) => s += &format!("{ch:#}"),
Expand All @@ -897,10 +899,9 @@ impl dyn Stream {
Some(_) => "..."
};
}
if s.len() < prec {
write!(f, "{}", s)
} else {
write!(f, "{:.*}...", prec - 3, s)
match prec {
Some(prec) if prec < s.len() => write!(f, "{:.*}...", prec - 3, s),
_ => write!(f, "{}", s)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() -> std::io::Result<()> {
match expr.eval() {
Ok(item) => {
println!("Item Describe: {}", item.describe());
let (s, err) = item.format(80);
let (s, err) = item.format(Some(80));
println!("Item Format: {s}");
if let Some(err) = err {
println!("Err: {}", err);
Expand Down

0 comments on commit f6ee7e8

Please sign in to comment.