annotate rust/hg-core/src/utils/hg_path.rs @ 45907:06b64fabf91c

copies: cache the ancestor checking call when tracing copy A good share of the time spent in this function is spent doing ancestors checking. To avoid spending time in duplicated call, we cache the result of calls. In the slower case, this provide a quite significant performance boost. Below are the result for a set of selected pairs (many of them pathological): (And further down is another table that summarize the current state of filelog based vs changeset base copy tracing) The benchmark have been configured to be killed after 6 minutes of runtime, which mean that any detect slower than 2 minutes will be marked as "killed". This drop some useful information about how much slower these case are? but also prevent 99% of the benchmark time to be spent on case that can be labelled "very slow" anyway. Repo Case Source-Rev Dest-Rev Old-Time New-Time Difference Factor ------------------------------------------------------------------------------------------------------------------------------------ mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000044 s, 0.000044 s, +0.000000 s, ? 1.0000 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.000138 s, 0.000138 s, +0.000000 s, ? 1.0000 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.005067 s, 0.005052 s, -0.000015 s, ? 0.9970 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.000218 s, 0.000219 s, +0.000001 s, ? 1.0046 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.000053 s, 0.000055 s, +0.000002 s, ? 1.0377 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.000125 s, 0.000128 s, +0.000003 s, ? 1.0240 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.001098 s, 0.001089 s, -0.000009 s, ? 0.9918 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.017546 s, 0.017407 s, -0.000139 s, ? 0.9921 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 0.096723 s, 0.094175 s, -0.002548 s, ? 0.9737 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 0.271796 s, 0.238009 s, -0.033787 s, ? 0.8757 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 0.128602 s, 0.125876 s, -0.002726 s, ? 0.9788 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 7.086742 s, 3.581556 s, -3.505186 s, ? 0.5054 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 0.016634 s, 0.016721 s, +0.000087 s, ? 1.0052 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 0.254225 s, 0.242367 s, -0.011858 s, ? 0.9534 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.000166 s, 0.000165 s, -0.000001 s, ? 0.9940 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.000118 s, 0.000114 s, -0.000004 s, ? 0.9661 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.000296 s, 0.000296 s, +0.000000 s, ? 1.0000 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.001137 s, 0.001124 s, -0.000013 s, ? 0.9886 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.014133 s, 0.013060 s, -0.001073 s, ? 0.9241 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.016988 s, 0.017112 s, +0.000124 s, ? 1.0073 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.676361 s, 0.660350 s, -0.016011 s, ? 0.9763 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 12.515149 s, 10.032499 s, -2.482650 s, ? 0.8016 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.000186 s, 0.000189 s, +0.000003 s, ? 1.0161 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.000459 s, 0.000462 s, +0.000003 s, ? 1.0065 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.000273 s, 0.000270 s, -0.000003 s, ? 0.9890 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.001503 s, 0.001474 s, -0.000029 s, ? 0.9807 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.004862 s, 0.004806 s, -0.000056 s, ? 0.9885 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.088291 s, 0.085150 s, -0.003141 s, ? 0.9644 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007113 s, 0.007064 s, -0.000049 s, ? 0.9931 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004687 s, 0.004741 s, +0.000054 s, ? 1.0115 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 0.198710 s, 0.190133 s, -0.008577 s, ? 0.9568 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.036068 s, 0.035651 s, -0.000417 s, ? 0.9884 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 0.465362 s, 0.440694 s, -0.024668 s, ? 0.9470 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 24.519684 s, 18.454163 s, -6.065521 s, ? 0.7526 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 42.711897 s, 31.562719 s, -11.149178 s, ? 0.7390 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.001201 s, 0.001189 s, -0.000012 s, ? 0.9900 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.001216 s, 0.001204 s, -0.000012 s, ? 0.9901 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.000595 s, 0.000586 s, -0.000009 s, ? 0.9849 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.001856 s, 0.001845 s, -0.000011 s, ? 0.9941 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 0.064936 s, 0.063822 s, -0.001114 s, ? 0.9828 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.090601 s, 0.088038 s, -0.002563 s, ? 0.9717 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007510 s, 0.007389 s, -0.000121 s, ? 0.9839 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004911 s, 0.004868 s, -0.000043 s, ? 0.9912 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 0.233231 s, 0.222450 s, -0.010781 s, ? 0.9538 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.419989 s, 0.370675 s, -0.049314 s, ? 0.8826 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.401521 s, 0.358020 s, -0.043501 s, ? 0.8917 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 0.179555 s, 0.145235 s, -0.034320 s, ? 0.8089 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.038004 s, 0.037606 s, -0.000398 s, ? 0.9895 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 52.838482 s, 7.382439 s, -45.456043 s, ? 0.1397 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 8.705874 s, 7.273506 s, -1.432368 s, ? 0.8355 mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 1.126708 s, 1.074593 s, -0.052115 s, ? 0.9537 mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 83.854020 s, 27.746195 s, -56.107825 s, ? 0.3309 Below is a table comparing the runtime of the current "filelog centric" algorithm, with the "changeset centric" one, we just modified. The changeset centric algorithm is a significant win in many scenario, but they are still various cases where it is quite slower. When many revision has to be considered the cost of retrieving the copy information, creating new dictionaries, merging dictionaries and checking if revision are ancestors of each other can slow things down. The rest of this series, will introduce a rust version of the copy tracing code to deal with most of theses issues. Repo Case Source-Rev Dest-Rev filelog sidedata Difference Factor --------------------------------------------------------------------------------------------------------------------------------------- mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000914 s, 0.000044 s, - 0.000870 s, ? 0.048140 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.001812 s, 0.000138 s, - 0.001674 s, ? 0.076159 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.017954 s, 0.005052 s, - 0.012902 s, ? 0.281386 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.001509 s, 0.000219 s, - 0.001290 s, ? 0.145129 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.206881 s, 0.000055 s, - 0.206826 s, ? 0.000266 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.016951 s, 0.000128 s, - 0.016823 s, ? 0.007551 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.019096 s, 0.001089 s, - 0.018007 s, ? 0.057028 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.762506 s, 0.017407 s, - 0.745099 s, ? 0.022829 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 1.179211 s, 0.094175 s, - 1.085036 s, ? 0.079863 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 1.249058 s, 0.238009 s, - 1.011049 s, ? 0.190551 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 1.614107 s, 0.125876 s, - 1.488231 s, ? 0.077985 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 0.001064 s, 3.581556 s, + 3.580492 s, ? 3366.124060 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 1.061275 s, 0.016721 s, - 1.044554 s, ? 0.015756 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 1.341119 s, 0.242367 s, - 1.098752 s, ? 0.180720 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.027803 s, 0.000165 s, - 0.027638 s, ? 0.005935 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.130014 s, 0.000114 s, - 0.129900 s, ? 0.000877 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.024990 s, 0.000296 s, - 0.024694 s, ? 0.011845 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.052201 s, 0.001124 s, - 0.051077 s, ? 0.021532 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.037642 s, 0.013060 s, - 0.024582 s, ? 0.346953 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.197086 s, 0.017112 s, - 0.179974 s, ? 0.086825 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.935148 s, 0.660350 s, - 0.274798 s, ? 0.706145 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 3.920674 s, 10.032499 s, + 6.111825 s, ? 2.558871 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.024232 s, 0.000189 s, - 0.024043 s, ? 0.007800 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.141483 s, 0.000462 s, - 0.141021 s, ? 0.003265 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.025775 s, 0.000270 s, - 0.025505 s, ? 0.010475 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.084922 s, 0.001474 s, - 0.083448 s, ? 0.017357 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.194784 s, 0.004806 s, - 0.189978 s, ? 0.024673 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.161103 s, 0.085150 s, - 2.075953 s, ? 0.039401 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089347 s, 0.007064 s, - 0.082283 s, ? 0.079063 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.732171 s, 0.004741 s, - 0.727430 s, ? 0.006475 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 1.157287 s, 0.190133 s, - 0.967154 s, ? 0.164292 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.726568 s, 0.035651 s, - 6.690917 s, ? 0.005300 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 3.266229 s, 0.440694 s, - 2.825535 s, ? 0.134924 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 15.860534 s, 18.454163 s, + 2.593629 s, ? 1.163527 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 20.450475 s, 31.562719 s, +11.112244 s, ? 1.543373 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.080442 s, 0.001189 s, - 0.079253 s, ? 0.014781 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.497672 s, 0.001204 s, - 0.496468 s, ? 0.002419 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.021183 s, 0.000586 s, - 0.020597 s, ? 0.027664 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.230991 s, 0.001845 s, - 0.229146 s, ? 0.007987 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 1.118461 s, 0.063822 s, - 1.054639 s, ? 0.057062 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.206083 s, 0.088038 s, - 2.118045 s, ? 0.039907 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089404 s, 0.007389 s, - 0.082015 s, ? 0.082647 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.733043 s, 0.004868 s, - 0.728175 s, ? 0.006641 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 1.163367 s, 0.222450 s, - 0.940917 s, ? 0.191212 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.085456 s, 0.370675 s, + 0.285219 s, ? 4.337612 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.083601 s, 0.358020 s, + 0.274419 s, ? 4.282485 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 7.366614 s, 0.145235 s, - 7.221379 s, ? 0.019715 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.664464 s, 0.037606 s, - 6.626858 s, ? 0.005643 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 7.467836 s, 7.382439 s, - 0.085397 s, ? 0.988565 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 9.801294 s, 7.273506 s, - 2.527788 s, ? 0.742097 mozilla-try x00000_revs_x_added_0_copies 6a320851d377 1ebb79acd503 : 0.091886 s, killed mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 26.491140 s, 1.074593 s, -25.416547 s, ? 0.040564 mozilla-try x00000_revs_x_added_x_copies 5173c4b6f97c 95d83ee7242d : 0.092863 s, killed mozilla-try x00000_revs_x000_added_x_copies 9126823d0e9c ca82787bb23c : 0.226823 s, killed mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 18.914630 s, 27.746195 s, + 8.831565 s, ? 1.466917 mozilla-try x00000_revs_x00000_added_x0000_copies 1b661134e2ca 1ae03d022d6d : 21.198903 s, killed mozilla-try x00000_revs_x00000_added_x000_copies 9b2a99adc05e 8e29777b48e6 : 24.952268 s, killed Differential Revision: https://phab.mercurial-scm.org/D9296
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 02 Nov 2020 11:03:56 +0100
parents 2d5dfc8fed55
children 2e2033081274
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
1 // hg_path.rs
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
2 //
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
4 //
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
5 // This software may be used and distributed according to the terms of the
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
6 // GNU General Public License version 2 or any later version.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
7
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
8 use std::borrow::Borrow;
45544
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
9 use std::convert::TryFrom;
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
10 use std::ffi::{OsStr, OsString};
43845
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
11 use std::fmt;
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
12 use std::ops::Deref;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
13 use std::path::{Path, PathBuf};
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
14
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
15 #[derive(Debug, Eq, PartialEq)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
16 pub enum HgPathError {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
17 /// Bytes from the invalid `HgPath`
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
18 LeadingSlash(Vec<u8>),
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
19 ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
20 bytes: Vec<u8>,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
21 second_slash_index: usize,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
22 },
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
23 ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
24 bytes: Vec<u8>,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
25 null_byte_index: usize,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
26 },
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
27 /// Bytes
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
28 DecodeError(Vec<u8>),
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
29 /// The rest come from audit errors
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
30 EndsWithSlash(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
31 ContainsIllegalComponent(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
32 /// Path is inside the `.hg` folder
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
33 InsideDotHg(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
34 IsInsideNestedRepo {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
35 path: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
36 nested_repo: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
37 },
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
38 TraversesSymbolicLink {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
39 path: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
40 symlink: HgPathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
41 },
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
42 NotFsCompliant(HgPathBuf),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
43 /// `path` is the smallest invalid path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
44 NotUnderRoot {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
45 path: PathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
46 root: PathBuf,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
47 },
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
48 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
49
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
50 impl ToString for HgPathError {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
51 fn to_string(&self) -> String {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
52 match self {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
53 HgPathError::LeadingSlash(bytes) => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
54 format!("Invalid HgPath '{:?}': has a leading slash.", bytes)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
55 }
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
56 HgPathError::ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
57 bytes,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
58 second_slash_index: pos,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
59 } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
60 "Invalid HgPath '{:?}': consecutive slashes at pos {}.",
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
61 bytes, pos
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
62 ),
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
63 HgPathError::ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
64 bytes,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
65 null_byte_index: pos,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
66 } => format!(
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
67 "Invalid HgPath '{:?}': contains null byte at pos {}.",
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
68 bytes, pos
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
69 ),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
70 HgPathError::DecodeError(bytes) => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
71 format!("Invalid HgPath '{:?}': could not be decoded.", bytes)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
72 }
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
73 HgPathError::EndsWithSlash(path) => {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
74 format!("Audit failed for '{}': ends with a slash.", path)
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
75 }
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
76 HgPathError::ContainsIllegalComponent(path) => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
77 "Audit failed for '{}': contains an illegal component.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
78 path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
79 ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
80 HgPathError::InsideDotHg(path) => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
81 "Audit failed for '{}': is inside the '.hg' folder.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
82 path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
83 ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
84 HgPathError::IsInsideNestedRepo {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
85 path,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
86 nested_repo: nested,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
87 } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
88 "Audit failed for '{}': is inside a nested repository '{}'.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
89 path, nested
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
90 ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
91 HgPathError::TraversesSymbolicLink { path, symlink } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
92 "Audit failed for '{}': traverses symbolic link '{}'.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
93 path, symlink
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
94 ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
95 HgPathError::NotFsCompliant(path) => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
96 "Audit failed for '{}': cannot be turned into a \
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
97 filesystem path.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
98 path
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
99 ),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
100 HgPathError::NotUnderRoot { path, root } => format!(
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
101 "Audit failed for '{}': not under root {}.",
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
102 path.display(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
103 root.display()
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
104 ),
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
105 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
106 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
107 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
108
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
109 impl From<HgPathError> for std::io::Error {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
110 fn from(e: HgPathError) -> Self {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
111 std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
112 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
113 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
114
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
115 /// This is a repository-relative path (or canonical path):
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
116 /// - no null characters
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
117 /// - `/` separates directories
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
118 /// - no consecutive slashes
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
119 /// - no leading slash,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
120 /// - no `.` nor `..` of special meaning
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
121 /// - stored in repository and shared across platforms
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
122 ///
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
123 /// Note: there is no guarantee of any `HgPath` being well-formed at any point
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
124 /// in its lifetime for performance reasons and to ease ergonomics. It is
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
125 /// however checked using the `check_state` method before any file-system
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
126 /// operation.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
127 ///
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
128 /// This allows us to be encoding-transparent as much as possible, until really
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
129 /// needed; `HgPath` can be transformed into a platform-specific path (`OsStr`
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
130 /// or `Path`) whenever more complex operations are needed:
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
131 /// On Unix, it's just byte-to-byte conversion. On Windows, it has to be
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
132 /// decoded from MBCS to WTF-8. If WindowsUTF8Plan is implemented, the source
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
133 /// character encoding will be determined on a per-repository basis.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
134 //
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
135 // FIXME: (adapted from a comment in the stdlib)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
136 // `HgPath::new()` current implementation relies on `Slice` being
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
137 // layout-compatible with `[u8]`.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
138 // When attribute privacy is implemented, `Slice` should be annotated as
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
139 // `#[repr(transparent)]`.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
140 // Anyway, `Slice` representation and layout are considered implementation
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
141 // detail, are not documented and must not be relied upon.
43914
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
142 #[derive(Eq, Ord, PartialEq, PartialOrd, Hash)]
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
143 pub struct HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
144 inner: [u8],
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
145 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
146
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
147 impl HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
148 pub fn new<S: AsRef<[u8]> + ?Sized>(s: &S) -> &Self {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
149 unsafe { &*(s.as_ref() as *const [u8] as *const Self) }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
150 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
151 pub fn is_empty(&self) -> bool {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
152 self.inner.is_empty()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
153 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
154 pub fn len(&self) -> usize {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
155 self.inner.len()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
156 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
157 fn to_hg_path_buf(&self) -> HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
158 HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
159 inner: self.inner.to_owned(),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
160 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
161 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
162 pub fn bytes(&self) -> std::slice::Iter<u8> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
163 self.inner.iter()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
164 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
165 pub fn to_ascii_uppercase(&self) -> HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
166 HgPathBuf::from(self.inner.to_ascii_uppercase())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
167 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
168 pub fn to_ascii_lowercase(&self) -> HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
169 HgPathBuf::from(self.inner.to_ascii_lowercase())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
170 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
171 pub fn as_bytes(&self) -> &[u8] {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
172 &self.inner
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
173 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
174 pub fn contains(&self, other: u8) -> bool {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
175 self.inner.contains(&other)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
176 }
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
177 pub fn starts_with(&self, needle: impl AsRef<Self>) -> bool {
43851
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
178 self.inner.starts_with(needle.as_ref().as_bytes())
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
179 }
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
180 pub fn trim_trailing_slash(&self) -> &Self {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
181 Self::new(if self.inner.last() == Some(&b'/') {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
182 &self.inner[..self.inner.len() - 1]
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
183 } else {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
184 &self.inner[..]
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
185 })
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
186 }
44314
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
187 /// Returns a tuple of slices `(base, filename)` resulting from the split
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
188 /// at the rightmost `/`, if any.
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
189 ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
190 /// # Examples:
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
191 ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
192 /// ```
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
193 /// use hg::utils::hg_path::HgPath;
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
194 ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
195 /// let path = HgPath::new(b"cool/hg/path").split_filename();
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
196 /// assert_eq!(path, (HgPath::new(b"cool/hg"), HgPath::new(b"path")));
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
197 ///
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
198 /// let path = HgPath::new(b"pathwithoutsep").split_filename();
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
199 /// assert_eq!(path, (HgPath::new(b""), HgPath::new(b"pathwithoutsep")));
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
200 /// ```
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
201 pub fn split_filename(&self) -> (&Self, &Self) {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
202 match &self.inner.iter().rposition(|c| *c == b'/') {
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
203 None => (HgPath::new(""), &self),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
204 Some(size) => (
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
205 HgPath::new(&self.inner[..*size]),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
206 HgPath::new(&self.inner[*size + 1..]),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
207 ),
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
208 }
0e9ac3968b56 rust-dirs-multiset: add `DirsChildrenMultiset`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44313
diff changeset
209 }
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
210 pub fn join<T: ?Sized + AsRef<Self>>(&self, other: &T) -> HgPathBuf {
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
211 let mut inner = self.inner.to_owned();
44998
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44314
diff changeset
212 if !inner.is_empty() && inner.last() != Some(&b'/') {
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
213 inner.push(b'/');
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
214 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
215 inner.extend(other.as_ref().bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
216 HgPathBuf::from_bytes(&inner)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
217 }
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
218 pub fn parent(&self) -> &Self {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
219 let inner = self.as_bytes();
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
220 HgPath::new(match inner.iter().rposition(|b| *b == b'/') {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
221 Some(pos) => &inner[..pos],
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
222 None => &[],
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
223 })
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
224 }
43851
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
225 /// Given a base directory, returns the slice of `self` relative to the
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
226 /// base directory. If `base` is not a directory (does not end with a
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
227 /// `b'/'`), returns `None`.
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
228 pub fn relative_to(&self, base: impl AsRef<Self>) -> Option<&Self> {
43851
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
229 let base = base.as_ref();
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
230 if base.is_empty() {
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
231 return Some(self);
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
232 }
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
233 let is_dir = base.as_bytes().ends_with(b"/");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
234 if is_dir && self.starts_with(base) {
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
235 Some(Self::new(&self.inner[base.len()..]))
43851
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
236 } else {
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
237 None
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
238 }
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
239 }
44221
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
240
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
241 #[cfg(windows)]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
242 /// Copied from the Python stdlib's `os.path.splitdrive` implementation.
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
243 ///
44266
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
244 /// Split a pathname into drive/UNC sharepoint and relative path
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
245 /// specifiers. Returns a 2-tuple (drive_or_unc, path); either part may
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
246 /// be empty.
44221
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
247 ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
248 /// If you assign
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
249 /// result = split_drive(p)
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
250 /// It is always true that:
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
251 /// result[0] + result[1] == p
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
252 ///
44266
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
253 /// If the path contained a drive letter, drive_or_unc will contain
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
254 /// everything up to and including the colon.
44221
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
255 /// e.g. split_drive("c:/dir") returns ("c:", "/dir")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
256 ///
44266
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
257 /// If the path contained a UNC path, the drive_or_unc will contain the
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
258 /// host name and share up to but not including the fourth directory
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
259 /// separator character.
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
260 /// e.g. split_drive("//host/computer/dir") returns ("//host/computer",
732098027b34 rust: re-format with nightly rustfmt
Martin von Zweigbergk <martinvonz@google.com>
parents: 44221
diff changeset
261 /// "/dir")
44221
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
262 ///
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
263 /// Paths cannot contain both a drive letter and a UNC path.
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
264 pub fn split_drive<'a>(&self) -> (&HgPath, &HgPath) {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
265 let bytes = self.as_bytes();
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
266 let is_sep = |b| std::path::is_separator(b as char);
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
267
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
268 if self.len() < 2 {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
269 (HgPath::new(b""), &self)
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
270 } else if is_sep(bytes[0])
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
271 && is_sep(bytes[1])
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
272 && (self.len() == 2 || !is_sep(bytes[2]))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
273 {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
274 // Is a UNC path:
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
275 // vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
276 // \\machine\mountpoint\directory\etc\...
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
277 // directory ^^^^^^^^^^^^^^^
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
278
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
279 let machine_end_index = bytes[2..].iter().position(|b| is_sep(*b));
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
280 let mountpoint_start_index = if let Some(i) = machine_end_index {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
281 i + 2
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
282 } else {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
283 return (HgPath::new(b""), &self);
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
284 };
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
285
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
286 match bytes[mountpoint_start_index + 1..]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
287 .iter()
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
288 .position(|b| is_sep(*b))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
289 {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
290 // A UNC path can't have two slashes in a row
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
291 // (after the initial two)
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
292 Some(0) => (HgPath::new(b""), &self),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
293 Some(i) => {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
294 let (a, b) =
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
295 bytes.split_at(mountpoint_start_index + 1 + i);
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
296 (HgPath::new(a), HgPath::new(b))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
297 }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
298 None => (&self, HgPath::new(b"")),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
299 }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
300 } else if bytes[1] == b':' {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
301 // Drive path c:\directory
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
302 let (a, b) = bytes.split_at(2);
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
303 (HgPath::new(a), HgPath::new(b))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
304 } else {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
305 (HgPath::new(b""), &self)
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
306 }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
307 }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
308
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
309 #[cfg(unix)]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
310 /// Split a pathname into drive and path. On Posix, drive is always empty.
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
311 pub fn split_drive(&self) -> (&HgPath, &HgPath) {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
312 (HgPath::new(b""), &self)
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
313 }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
314
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
315 /// Checks for errors in the path, short-circuiting at the first one.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
316 /// This generates fine-grained errors useful for debugging.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
317 /// To simply check if the path is valid during tests, use `is_valid`.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
318 pub fn check_state(&self) -> Result<(), HgPathError> {
44998
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44314
diff changeset
319 if self.is_empty() {
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
320 return Ok(());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
321 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
322 let bytes = self.as_bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
323 let mut previous_byte = None;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
324
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
325 if bytes[0] == b'/' {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
326 return Err(HgPathError::LeadingSlash(bytes.to_vec()));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
327 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
328 for (index, byte) in bytes.iter().enumerate() {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
329 match byte {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
330 0 => {
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
331 return Err(HgPathError::ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
332 bytes: bytes.to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
333 null_byte_index: index,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
334 })
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
335 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
336 b'/' => {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
337 if previous_byte.is_some() && previous_byte == Some(b'/') {
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
338 return Err(HgPathError::ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
339 bytes: bytes.to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
340 second_slash_index: index,
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
341 });
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
342 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
343 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
344 _ => (),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
345 };
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
346 previous_byte = Some(*byte);
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
347 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
348 Ok(())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
349 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
350
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
351 #[cfg(test)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
352 /// Only usable during tests to force developers to handle invalid states
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
353 fn is_valid(&self) -> bool {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
354 self.check_state().is_ok()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
355 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
356 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
357
43914
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
358 impl fmt::Debug for HgPath {
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
359 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
360 write!(f, "HgPath({:?})", String::from_utf8_lossy(&self.inner))
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
361 }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
362 }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
363
43845
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
364 impl fmt::Display for HgPath {
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
365 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
366 write!(f, "{}", String::from_utf8_lossy(&self.inner))
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
367 }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
368 }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
369
44998
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44314
diff changeset
370 #[derive(Default, Eq, Ord, Clone, PartialEq, PartialOrd, Hash)]
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
371 pub struct HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
372 inner: Vec<u8>,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
373 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
374
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
375 impl HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
376 pub fn new() -> Self {
44998
26114bd6ec60 rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44314
diff changeset
377 Default::default()
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
378 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
379 pub fn push(&mut self, byte: u8) {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
380 self.inner.push(byte);
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
381 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
382 pub fn from_bytes(s: &[u8]) -> HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
383 HgPath::new(s).to_owned()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
384 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
385 pub fn into_vec(self) -> Vec<u8> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
386 self.inner
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
387 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
388 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
389
43914
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
390 impl fmt::Debug for HgPathBuf {
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
391 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
392 write!(f, "HgPathBuf({:?})", String::from_utf8_lossy(&self.inner))
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
393 }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
394 }
4b3c8df189bc rust-hg-path: implement more readable custom Debug for HgPath{,Buf}
Martin von Zweigbergk <martinvonz@google.com>
parents: 43851
diff changeset
395
43845
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
396 impl fmt::Display for HgPathBuf {
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
397 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
398 write!(f, "{}", String::from_utf8_lossy(&self.inner))
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
399 }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
400 }
c27e688fcdc3 rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43250
diff changeset
401
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
402 impl Deref for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
403 type Target = HgPath;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
404
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
405 #[inline]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
406 fn deref(&self) -> &HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
407 &HgPath::new(&self.inner)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
408 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
409 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
410
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
411 impl From<Vec<u8>> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
412 fn from(vec: Vec<u8>) -> Self {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
413 Self { inner: vec }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
414 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
415 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
416
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
417 impl<T: ?Sized + AsRef<HgPath>> From<&T> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
418 fn from(s: &T) -> HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
419 s.as_ref().to_owned()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
420 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
421 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
422
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
423 impl Into<Vec<u8>> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
424 fn into(self) -> Vec<u8> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
425 self.inner
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
426 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
427 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
428
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
429 impl Borrow<HgPath> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
430 fn borrow(&self) -> &HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
431 &HgPath::new(self.as_bytes())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
432 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
433 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
434
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
435 impl ToOwned for HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
436 type Owned = HgPathBuf;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
437
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
438 fn to_owned(&self) -> HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
439 self.to_hg_path_buf()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
440 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
441 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
442
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
443 impl AsRef<HgPath> for HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
444 fn as_ref(&self) -> &HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
445 self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
446 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
447 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
448
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
449 impl AsRef<HgPath> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
450 fn as_ref(&self) -> &HgPath {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
451 self
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
452 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
453 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
454
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
455 impl Extend<u8> for HgPathBuf {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
456 fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
457 self.inner.extend(iter);
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
458 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
459 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
460
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
461 /// TODO: Once https://www.mercurial-scm.org/wiki/WindowsUTF8Plan is
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
462 /// implemented, these conversion utils will have to work differently depending
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
463 /// on the repository encoding: either `UTF-8` or `MBCS`.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
464
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
465 pub fn hg_path_to_os_string<P: AsRef<HgPath>>(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
466 hg_path: P,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
467 ) -> Result<OsString, HgPathError> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
468 hg_path.as_ref().check_state()?;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
469 let os_str;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
470 #[cfg(unix)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
471 {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
472 use std::os::unix::ffi::OsStrExt;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
473 os_str = std::ffi::OsStr::from_bytes(&hg_path.as_ref().as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
474 }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
475 // TODO Handle other platforms
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
476 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
477 Ok(os_str.to_os_string())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
478 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
479
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
480 pub fn hg_path_to_path_buf<P: AsRef<HgPath>>(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
481 hg_path: P,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
482 ) -> Result<PathBuf, HgPathError> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
483 Ok(Path::new(&hg_path_to_os_string(hg_path)?).to_path_buf())
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
484 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
485
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
486 pub fn os_string_to_hg_path_buf<S: AsRef<OsStr>>(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
487 os_string: S,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
488 ) -> Result<HgPathBuf, HgPathError> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
489 let buf;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
490 #[cfg(unix)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
491 {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
492 use std::os::unix::ffi::OsStrExt;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
493 buf = HgPathBuf::from_bytes(&os_string.as_ref().as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
494 }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
495 // TODO Handle other platforms
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
496 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
497
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
498 buf.check_state()?;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
499 Ok(buf)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
500 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
501
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
502 pub fn path_to_hg_path_buf<P: AsRef<Path>>(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
503 path: P,
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
504 ) -> Result<HgPathBuf, HgPathError> {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
505 let buf;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
506 let os_str = path.as_ref().as_os_str();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
507 #[cfg(unix)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
508 {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
509 use std::os::unix::ffi::OsStrExt;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
510 buf = HgPathBuf::from_bytes(&os_str.as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
511 }
43250
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
512 // TODO Handle other platforms
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
513 // TODO: convert from WTF8 to Windows MBCS (ANSI encoding).
98d996a138de rust-cross-platform: remove `unimplemented!` to get compile-time errors
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42959
diff changeset
514
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
515 buf.check_state()?;
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
516 Ok(buf)
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
517 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
518
45544
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
519 impl TryFrom<PathBuf> for HgPathBuf {
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
520 type Error = HgPathError;
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
521 fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
522 path_to_hg_path_buf(path)
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
523 }
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
524 }
2d5dfc8fed55 hg-core: impl TryFrom<PathBuff> for HgPathBuf
Antoine Cezar <antoine.cezar@octobus.net>
parents: 44998
diff changeset
525
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
526 #[cfg(test)]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
527 mod tests {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
528 use super::*;
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
529 use pretty_assertions::assert_eq;
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
530
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
531 #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
532 fn test_path_states() {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
533 assert_eq!(
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
534 Err(HgPathError::LeadingSlash(b"/".to_vec())),
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
535 HgPath::new(b"/").check_state()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
536 );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
537 assert_eq!(
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
538 Err(HgPathError::ConsecutiveSlashes {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
539 bytes: b"a/b//c".to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
540 second_slash_index: 4
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
541 }),
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
542 HgPath::new(b"a/b//c").check_state()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
543 );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
544 assert_eq!(
44312
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
545 Err(HgPathError::ContainsNullByte {
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
546 bytes: b"a/b/\0c".to_vec(),
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
547 null_byte_index: 4
c18dd48cea4a rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44266
diff changeset
548 }),
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
549 HgPath::new(b"a/b/\0c").check_state()
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
550 );
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
551 // TODO test HgPathError::DecodeError for the Windows implementation.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
552 assert_eq!(true, HgPath::new(b"").is_valid());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
553 assert_eq!(true, HgPath::new(b"a/b/c").is_valid());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
554 // Backslashes in paths are not significant, but allowed
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
555 assert_eq!(true, HgPath::new(br"a\b/c").is_valid());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
556 // Dots in paths are not significant, but allowed
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
557 assert_eq!(true, HgPath::new(b"a/b/../c/").is_valid());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
558 assert_eq!(true, HgPath::new(b"./a/b/../c/").is_valid());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
559 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
560
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
561 #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
562 fn test_iter() {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
563 let path = HgPath::new(b"a");
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
564 let mut iter = path.bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
565 assert_eq!(Some(&b'a'), iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
566 assert_eq!(None, iter.next_back());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
567 assert_eq!(None, iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
568
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
569 let path = HgPath::new(b"a");
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
570 let mut iter = path.bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
571 assert_eq!(Some(&b'a'), iter.next_back());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
572 assert_eq!(None, iter.next_back());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
573 assert_eq!(None, iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
574
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
575 let path = HgPath::new(b"abc");
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
576 let mut iter = path.bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
577 assert_eq!(Some(&b'a'), iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
578 assert_eq!(Some(&b'c'), iter.next_back());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
579 assert_eq!(Some(&b'b'), iter.next_back());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
580 assert_eq!(None, iter.next_back());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
581 assert_eq!(None, iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
582
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
583 let path = HgPath::new(b"abc");
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
584 let mut iter = path.bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
585 assert_eq!(Some(&b'a'), iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
586 assert_eq!(Some(&b'b'), iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
587 assert_eq!(Some(&b'c'), iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
588 assert_eq!(None, iter.next_back());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
589 assert_eq!(None, iter.next());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
590
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
591 let path = HgPath::new(b"abc");
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
592 let iter = path.bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
593 let mut vec = Vec::new();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
594 vec.extend(iter);
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
595 assert_eq!(vec![b'a', b'b', b'c'], vec);
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
596
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
597 let path = HgPath::new(b"abc");
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
598 let mut iter = path.bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
599 assert_eq!(Some(2), iter.rposition(|c| *c == b'c'));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
600
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
601 let path = HgPath::new(b"abc");
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
602 let mut iter = path.bytes();
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
603 assert_eq!(None, iter.rposition(|c| *c == b'd'));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
604 }
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
605
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
606 #[test]
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
607 fn test_join() {
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
608 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"b"));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
609 assert_eq!(b"a/b", path.as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
610
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
611 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"b/c"));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
612 assert_eq!(b"a/b/c", path.as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
613
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
614 // No leading slash if empty before join
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
615 let path = HgPathBuf::new().join(HgPath::new(b"b/c"));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
616 assert_eq!(b"b/c", path.as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
617
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
618 // The leading slash is an invalid representation of an `HgPath`, but
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
619 // it can happen. This creates another invalid representation of
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
620 // consecutive bytes.
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
621 // TODO What should be done in this case? Should we silently remove
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
622 // the extra slash? Should we change the signature to a problematic
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
623 // `Result<HgPathBuf, HgPathError>`, or should we just keep it so and
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
624 // let the error happen upon filesystem interaction?
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
625 let path = HgPathBuf::from_bytes(b"a/").join(HgPath::new(b"/b"));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
626 assert_eq!(b"a//b", path.as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
627 let path = HgPathBuf::from_bytes(b"a").join(HgPath::new(b"/b"));
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
628 assert_eq!(b"a//b", path.as_bytes());
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
629 }
43851
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
630
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
631 #[test]
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
632 fn test_relative_to() {
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
633 let path = HgPath::new(b"");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
634 let base = HgPath::new(b"");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
635 assert_eq!(Some(path), path.relative_to(base));
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
636
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
637 let path = HgPath::new(b"path");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
638 let base = HgPath::new(b"");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
639 assert_eq!(Some(path), path.relative_to(base));
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
640
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
641 let path = HgPath::new(b"a");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
642 let base = HgPath::new(b"b");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
643 assert_eq!(None, path.relative_to(base));
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
644
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
645 let path = HgPath::new(b"a/b");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
646 let base = HgPath::new(b"a");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
647 assert_eq!(None, path.relative_to(base));
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
648
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
649 let path = HgPath::new(b"a/b");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
650 let base = HgPath::new(b"a/");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
651 assert_eq!(Some(HgPath::new(b"b")), path.relative_to(base));
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
652
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
653 let path = HgPath::new(b"nested/path/to/b");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
654 let base = HgPath::new(b"nested/path/");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
655 assert_eq!(Some(HgPath::new(b"to/b")), path.relative_to(base));
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
656
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
657 let path = HgPath::new(b"ends/with/dir/");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
658 let base = HgPath::new(b"ends/");
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
659 assert_eq!(Some(HgPath::new(b"with/dir/")), path.relative_to(base));
4f1543a2f5c3 rust-hg-path: add method to get part of a path relative to a prefix
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43845
diff changeset
660 }
44221
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
661
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
662 #[test]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
663 #[cfg(unix)]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
664 fn test_split_drive() {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
665 // Taken from the Python stdlib's tests
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
666 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
667 HgPath::new(br"/foo/bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
668 (HgPath::new(b""), HgPath::new(br"/foo/bar"))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
669 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
670 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
671 HgPath::new(br"foo:bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
672 (HgPath::new(b""), HgPath::new(br"foo:bar"))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
673 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
674 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
675 HgPath::new(br":foo:bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
676 (HgPath::new(b""), HgPath::new(br":foo:bar"))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
677 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
678 // Also try NT paths; should not split them
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
679 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
680 HgPath::new(br"c:\foo\bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
681 (HgPath::new(b""), HgPath::new(br"c:\foo\bar"))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
682 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
683 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
684 HgPath::new(b"c:/foo/bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
685 (HgPath::new(b""), HgPath::new(br"c:/foo/bar"))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
686 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
687 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
688 HgPath::new(br"\\conky\mountpoint\foo\bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
689 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
690 HgPath::new(b""),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
691 HgPath::new(br"\\conky\mountpoint\foo\bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
692 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
693 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
694 }
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
695
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
696 #[test]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
697 #[cfg(windows)]
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
698 fn test_split_drive() {
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
699 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
700 HgPath::new(br"c:\foo\bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
701 (HgPath::new(br"c:"), HgPath::new(br"\foo\bar"))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
702 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
703 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
704 HgPath::new(b"c:/foo/bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
705 (HgPath::new(br"c:"), HgPath::new(br"/foo/bar"))
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
706 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
707 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
708 HgPath::new(br"\\conky\mountpoint\foo\bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
709 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
710 HgPath::new(br"\\conky\mountpoint"),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
711 HgPath::new(br"\foo\bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
712 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
713 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
714 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
715 HgPath::new(br"//conky/mountpoint/foo/bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
716 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
717 HgPath::new(br"//conky/mountpoint"),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
718 HgPath::new(br"/foo/bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
719 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
720 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
721 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
722 HgPath::new(br"\\\conky\mountpoint\foo\bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
723 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
724 HgPath::new(br""),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
725 HgPath::new(br"\\\conky\mountpoint\foo\bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
726 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
727 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
728 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
729 HgPath::new(br"///conky/mountpoint/foo/bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
730 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
731 HgPath::new(br""),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
732 HgPath::new(br"///conky/mountpoint/foo/bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
733 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
734 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
735 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
736 HgPath::new(br"\\conky\\mountpoint\foo\bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
737 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
738 HgPath::new(br""),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
739 HgPath::new(br"\\conky\\mountpoint\foo\bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
740 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
741 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
742 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
743 HgPath::new(br"//conky//mountpoint/foo/bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
744 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
745 HgPath::new(br""),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
746 HgPath::new(br"//conky//mountpoint/foo/bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
747 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
748 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
749 // UNC part containing U+0130
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
750 assert_eq!(
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
751 HgPath::new(b"//conky/MOUNTPO\xc4\xb0NT/foo/bar").split_drive(),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
752 (
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
753 HgPath::new(b"//conky/MOUNTPO\xc4\xb0NT"),
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
754 HgPath::new(br"/foo/bar")
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
755 )
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
756 );
baa4e7fdfd47 rust-utils: add Rust implementation of Python's "os.path.splitdrive"
Rapha?l Gom?s <rgomes@octobus.net>
parents: 43914
diff changeset
757 }
44313
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
758
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
759 #[test]
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
760 fn test_parent() {
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
761 let path = HgPath::new(b"");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
762 assert_eq!(path.parent(), path);
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
763
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
764 let path = HgPath::new(b"a");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
765 assert_eq!(path.parent(), HgPath::new(b""));
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
766
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
767 let path = HgPath::new(b"a/b");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
768 assert_eq!(path.parent(), HgPath::new(b"a"));
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
769
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
770 let path = HgPath::new(b"a/other/b");
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
771 assert_eq!(path.parent(), HgPath::new(b"a/other"));
9ab4830e9e3d rust-hg-path: add useful methods to `HgPath`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 44312
diff changeset
772 }
42959
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
773 }