Skip to content

Commit 9086040

Browse files
committed
Remove duplicate Display and add matching FromStr for Vec3
1 parent a5c203f commit 9086040

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

azalea-core/src/position.rs

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use crate::resource_location::ResourceLocation;
77
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
88
#[cfg(feature = "serde")]
99
use serde::{Deserialize, Serialize};
10-
use std::fmt::Display;
1110
use std::str::FromStr;
1211
use std::{
1312
fmt,
1413
hash::Hash,
1514
io::{Cursor, Write},
1615
ops::{Add, AddAssign, Mul, Rem, Sub},
1716
};
17+
use std::num::{ParseFloatError, ParseIntError};
1818

1919
macro_rules! vec3_impl {
2020
($name:ident, $type:ty) => {
@@ -272,33 +272,6 @@ impl BlockPos {
272272
}
273273
}
274274

275-
impl Display for BlockPos {
276-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277-
write!(f, "({}, {}, {})", self.x, self.y, self.z)
278-
}
279-
}
280-
281-
impl FromStr for BlockPos {
282-
type Err = &'static str;
283-
284-
fn from_str(s: &str) -> Result<Self, Self::Err> {
285-
let pos = s
286-
.trim_matches(|c| c == '(' || c == ')')
287-
.split(',')
288-
.collect::<Vec<_>>();
289-
290-
if pos.len() != 3 {
291-
return Err("Must be a length of 3");
292-
}
293-
294-
Ok(BlockPos {
295-
x: pos[0].parse()?,
296-
y: pos[1].parse()?,
297-
z: pos[2].parse()?,
298-
})
299-
}
300-
}
301-
302275
/// Chunk coordinates are used to represent where a chunk is in the world. You
303276
/// can convert the x and z to block coordinates by multiplying them by 16.
304277
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -602,6 +575,46 @@ impl fmt::Display for Vec3 {
602575
}
603576
}
604577

578+
impl FromStr for BlockPos {
579+
type Err = String;
580+
581+
fn from_str(s: &str) -> Result<Self, Self::Err> {
582+
let pos = s
583+
.split(' ')
584+
.collect::<Vec<_>>();
585+
586+
if pos.len() != 3 {
587+
return Err("Must be a length of 3".to_string());
588+
}
589+
590+
Ok(BlockPos {
591+
x: pos[0].parse().map_err(|e: ParseIntError| e.to_string())?,
592+
y: pos[1].parse().map_err(|e: ParseIntError| e.to_string())?,
593+
z: pos[2].parse().map_err(|e: ParseIntError| e.to_string())?,
594+
})
595+
}
596+
}
597+
598+
impl FromStr for Vec3 {
599+
type Err = String;
600+
601+
fn from_str(s: &str) -> Result<Self, Self::Err> {
602+
let pos = s
603+
.split(' ')
604+
.collect::<Vec<_>>();
605+
606+
if pos.len() != 3 {
607+
return Err("Must be a length of 3".to_string());
608+
}
609+
610+
Ok(Vec3 {
611+
x: pos[0].parse().map_err(|e: ParseFloatError| e.to_string())?,
612+
y: pos[1].parse().map_err(|e: ParseFloatError| e.to_string())?,
613+
z: pos[2].parse().map_err(|e: ParseFloatError| e.to_string())?,
614+
})
615+
}
616+
}
617+
605618
const PACKED_X_LENGTH: u64 = 1 + 25; // minecraft does something a bit more complicated to get this 25
606619
const PACKED_Z_LENGTH: u64 = PACKED_X_LENGTH;
607620
const PACKED_Y_LENGTH: u64 = 64 - PACKED_X_LENGTH - PACKED_Z_LENGTH;

0 commit comments

Comments
 (0)