Mercurial > public > mercurial-scm > hg-stable
comparison rust/hg-core/src/utils/hg_path.rs @ 43845:c27e688fcdc3
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
This is useful when debugging, to get a human readable output instead of an
array of `u8`.
Differential Revision: https://phab.mercurial-scm.org/D7523
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Fri, 29 Nov 2019 17:15:24 +0100 |
parents | 98d996a138de |
children | 4f1543a2f5c3 |
comparison
equal
deleted
inserted
replaced
43844:5ac243a92e37 | 43845:c27e688fcdc3 |
---|---|
5 // This software may be used and distributed according to the terms of the | 5 // This software may be used and distributed according to the terms of the |
6 // GNU General Public License version 2 or any later version. | 6 // GNU General Public License version 2 or any later version. |
7 | 7 |
8 use std::borrow::Borrow; | 8 use std::borrow::Borrow; |
9 use std::ffi::{OsStr, OsString}; | 9 use std::ffi::{OsStr, OsString}; |
10 use std::fmt; | |
10 use std::ops::Deref; | 11 use std::ops::Deref; |
11 use std::path::{Path, PathBuf}; | 12 use std::path::{Path, PathBuf}; |
12 | 13 |
13 #[derive(Debug, Eq, PartialEq)] | 14 #[derive(Debug, Eq, PartialEq)] |
14 pub enum HgPathError { | 15 pub enum HgPathError { |
160 fn is_valid(&self) -> bool { | 161 fn is_valid(&self) -> bool { |
161 self.check_state().is_ok() | 162 self.check_state().is_ok() |
162 } | 163 } |
163 } | 164 } |
164 | 165 |
166 impl fmt::Display for HgPath { | |
167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
168 write!(f, "{}", String::from_utf8_lossy(&self.inner)) | |
169 } | |
170 } | |
171 | |
165 #[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)] | 172 #[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)] |
166 pub struct HgPathBuf { | 173 pub struct HgPathBuf { |
167 inner: Vec<u8>, | 174 inner: Vec<u8>, |
168 } | 175 } |
169 | 176 |
180 pub fn into_vec(self) -> Vec<u8> { | 187 pub fn into_vec(self) -> Vec<u8> { |
181 self.inner | 188 self.inner |
182 } | 189 } |
183 pub fn as_ref(&self) -> &[u8] { | 190 pub fn as_ref(&self) -> &[u8] { |
184 self.inner.as_ref() | 191 self.inner.as_ref() |
192 } | |
193 } | |
194 | |
195 impl fmt::Display for HgPathBuf { | |
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
197 write!(f, "{}", String::from_utf8_lossy(&self.inner)) | |
185 } | 198 } |
186 } | 199 } |
187 | 200 |
188 impl Deref for HgPathBuf { | 201 impl Deref for HgPathBuf { |
189 type Target = HgPath; | 202 type Target = HgPath; |