Debian Codegraph @

third_party/rust/rkv/src/helpers.rs

raw
1 // Copyright 2018-2019 Mozilla
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 // this file except in compliance with the License. You may obtain a copy of the
5 // License at http://www.apache.org/licenses/LICENSE-2.0
6 // Unless required by applicable law or agreed to in writing, software distributed
7 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 // specific language governing permissions and limitations under the License.
10
11 use std::{
12 io,
13 path::{Path, PathBuf},
14 };
15
16 use url::Url;
17
18 use crate::{error::StoreError, value::Value};
19
20 pub(crate) fn read_transform(value: Result<&[u8], StoreError>) -> Result<Value, StoreError> {
21 match value {
22 Ok(bytes) => Value::from_tagged_slice(bytes).map_err(StoreError::DataError),
23 Err(e) => Err(e),
24 }
25 }
26
27 // Workaround the UNC path on Windows, see https://github.com/rust-lang/rust/issues/42869.
28 // Otherwise, `Env::from_builder()` will panic with error_no(123).
29 pub(crate) fn canonicalize_path<'p, P>(path: P) -> io::Result<PathBuf>
30 where
31 P: Into<&'p Path>,
32 {
33 let canonical = path.into().canonicalize()?;
34
35 Ok(if cfg!(target_os = "windows") {
36 let map_err = |_| io::Error::new(io::ErrorKind::Other, "path canonicalization error");
37 Url::from_file_path(&canonical)
38 .and_then(|url| url.to_file_path())
39 .map_err(map_err)?
40 } else {
41 canonical
42 })
43 }