1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 //! Rust utils for cxx
17 #[cxx::bridge]
18 mod ffi{
19 #![allow(dead_code)]
20 extern "Rust"{
canonicalize(path: String) -> Result<String>21 fn canonicalize(path: String) -> Result<String>;
22 }
23 }
24
25 use std::fs;
26 use std::io::{Error, ErrorKind};
27
canonicalize(path: String) -> Result<String, Error>28 fn canonicalize(path: String) -> Result<String, Error> {
29 match fs::canonicalize(path) {
30 Ok(abs_path) => {
31 match abs_path.to_str() {
32 Some(path) => Ok(path.to_string()),
33 None => Err(Error::new(ErrorKind::Other, "canonicalize failed")),
34 }
35 },
36 Err(_) => Err(Error::new(ErrorKind::Other, "canonicalize failed")),
37 }
38 }