Skip to content

Commit

Permalink
chore: fix more lint by clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Jul 10, 2024
1 parent 1ab631e commit b84f08e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
49 changes: 35 additions & 14 deletions y-octo/src/doc/codec/any.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fmt, ops::RangeInclusive};
use std::{
fmt::{self, Display},
ops::RangeInclusive,
};

use ordered_float::OrderedFloat;

Expand Down Expand Up @@ -515,21 +518,39 @@ impl serde::Serialize for Any {
}
}

impl ToString for Any {
fn to_string(&self) -> String {
impl Display for Any {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::True => "true".to_string(),
Self::False => "false".to_string(),
Self::String(s) => s.clone(),
Self::Integer(i) => i.to_string(),
Self::Float32(f) => f.to_string(),
Self::Float64(f) => f.to_string(),
Self::BigInt64(i) => i.to_string(),
// TODO: stringify other types
_ => {
debug!("any to string {:?}", self);
String::default()
Self::True => write!(f, "true"),
Self::False => write!(f, "false"),
Self::String(s) => write!(f, "\"{}\"", s),
Self::Integer(i) => write!(f, "{}", i),
Self::Float32(v) => write!(f, "{}", v),
Self::Float64(v) => write!(f, "{}", v),
Self::BigInt64(v) => write!(f, "{}", v),
Self::Object(map) => {
write!(f, "{{")?;
for (i, (key, value)) in map.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}: {}", key, value)?;
}
write!(f, "}}")
}
Self::Array(vec) => {
write!(f, "[")?;
for (i, value) in vec.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", value)?;
}
write!(f, "]")
}
Self::Binary(buf) => write!(f, "{:?}", buf),
Self::Undefined => write!(f, "undefined"),
Self::Null => write!(f, "null"),
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions y-octo/src/doc/types/text.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use super::list::ListType;
use crate::{impl_type, Content, JwstCodecResult};

Expand Down Expand Up @@ -27,19 +29,15 @@ impl Text {
}
}

impl ToString for Text {
fn to_string(&self) -> String {
let mut ret = String::with_capacity(self.len() as usize);

self.iter_item().fold(&mut ret, |ret, item| {
impl Display for Text {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.iter_item().try_for_each(|item| {
if let Content::String(str) = &item.get().unwrap().content {
ret.push_str(str);
write!(f, "{}", str)
} else {
Ok(())
}

ret
});

ret
})
}
}

Expand Down
12 changes: 7 additions & 5 deletions y-octo/src/doc/types/value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use super::*;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -123,12 +125,12 @@ impl From<Doc> for Value {
}
}

impl ToString for Value {
fn to_string(&self) -> String {
impl Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Value::Any(any) => any.to_string(),
Value::Text(text) => text.to_string(),
_ => String::default(),
Value::Any(any) => write!(f, "{}", any),
Value::Text(text) => write!(f, "{}", text),
_ => write!(f, ""),
}
}
}
Expand Down

0 comments on commit b84f08e

Please sign in to comment.