comparison rust/hg-core/src/utils/hg_path.rs @ 46444:6c778d20c8c2

rust: replace ToString impls with Display ToString is automatically implementing for everything that implements Display, and Display can avoid allocating intermediate strings. Differential Revision: https://phab.mercurial-scm.org/D9904
author Simon Sapin <simon.sapin@octobus.net>
date Thu, 28 Jan 2021 19:21:57 +0100
parents 2e2033081274
children 3da19db33cbc
comparison
equal deleted inserted replaced
46443:43d63979a75e 46444:6c778d20c8c2
45 path: PathBuf, 45 path: PathBuf,
46 root: PathBuf, 46 root: PathBuf,
47 }, 47 },
48 } 48 }
49 49
50 impl ToString for HgPathError { 50 impl fmt::Display for HgPathError {
51 fn to_string(&self) -> String { 51 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52 match self { 52 match self {
53 HgPathError::LeadingSlash(bytes) => { 53 HgPathError::LeadingSlash(bytes) => {
54 format!("Invalid HgPath '{:?}': has a leading slash.", bytes) 54 write!(f, "Invalid HgPath '{:?}': has a leading slash.", bytes)
55 } 55 }
56 HgPathError::ConsecutiveSlashes { 56 HgPathError::ConsecutiveSlashes {
57 bytes, 57 bytes,
58 second_slash_index: pos, 58 second_slash_index: pos,
59 } => format!( 59 } => write!(
60 f,
60 "Invalid HgPath '{:?}': consecutive slashes at pos {}.", 61 "Invalid HgPath '{:?}': consecutive slashes at pos {}.",
61 bytes, pos 62 bytes, pos
62 ), 63 ),
63 HgPathError::ContainsNullByte { 64 HgPathError::ContainsNullByte {
64 bytes, 65 bytes,
65 null_byte_index: pos, 66 null_byte_index: pos,
66 } => format!( 67 } => write!(
68 f,
67 "Invalid HgPath '{:?}': contains null byte at pos {}.", 69 "Invalid HgPath '{:?}': contains null byte at pos {}.",
68 bytes, pos 70 bytes, pos
69 ), 71 ),
70 HgPathError::DecodeError(bytes) => { 72 HgPathError::DecodeError(bytes) => write!(
71 format!("Invalid HgPath '{:?}': could not be decoded.", bytes) 73 f,
74 "Invalid HgPath '{:?}': could not be decoded.",
75 bytes
76 ),
77 HgPathError::EndsWithSlash(path) => {
78 write!(f, "Audit failed for '{}': ends with a slash.", path)
72 } 79 }
73 HgPathError::EndsWithSlash(path) => { 80 HgPathError::ContainsIllegalComponent(path) => write!(
74 format!("Audit failed for '{}': ends with a slash.", path) 81 f,
75 }
76 HgPathError::ContainsIllegalComponent(path) => format!(
77 "Audit failed for '{}': contains an illegal component.", 82 "Audit failed for '{}': contains an illegal component.",
78 path 83 path
79 ), 84 ),
80 HgPathError::InsideDotHg(path) => format!( 85 HgPathError::InsideDotHg(path) => write!(
86 f,
81 "Audit failed for '{}': is inside the '.hg' folder.", 87 "Audit failed for '{}': is inside the '.hg' folder.",
82 path 88 path
83 ), 89 ),
84 HgPathError::IsInsideNestedRepo { 90 HgPathError::IsInsideNestedRepo {
85 path, 91 path,
86 nested_repo: nested, 92 nested_repo: nested,
87 } => format!( 93 } => {
94 write!(f,
88 "Audit failed for '{}': is inside a nested repository '{}'.", 95 "Audit failed for '{}': is inside a nested repository '{}'.",
89 path, nested 96 path, nested
90 ), 97 )
91 HgPathError::TraversesSymbolicLink { path, symlink } => format!( 98 }
99 HgPathError::TraversesSymbolicLink { path, symlink } => write!(
100 f,
92 "Audit failed for '{}': traverses symbolic link '{}'.", 101 "Audit failed for '{}': traverses symbolic link '{}'.",
93 path, symlink 102 path, symlink
94 ), 103 ),
95 HgPathError::NotFsCompliant(path) => format!( 104 HgPathError::NotFsCompliant(path) => write!(
105 f,
96 "Audit failed for '{}': cannot be turned into a \ 106 "Audit failed for '{}': cannot be turned into a \
97 filesystem path.", 107 filesystem path.",
98 path 108 path
99 ), 109 ),
100 HgPathError::NotUnderRoot { path, root } => format!( 110 HgPathError::NotUnderRoot { path, root } => write!(
111 f,
101 "Audit failed for '{}': not under root {}.", 112 "Audit failed for '{}': not under root {}.",
102 path.display(), 113 path.display(),
103 root.display() 114 root.display()
104 ), 115 ),
105 } 116 }