diff options
author | Alexander Batischev <eual.jp@gmail.com> | 2023-07-29 14:00:46 +0300 |
---|---|---|
committer | Alexander Batischev <eual.jp@gmail.com> | 2024-10-01 19:09:30 +0300 |
commit | ddd6158ed93a7f5f8520de161e9d5b4de59f3791 (patch) | |
tree | 4bdee6bf86d8890aad2f89027208e27ea9135b8a | |
parent | 281e2c9bb12e458eaa8ff90b857b63b87e835e43 (diff) |
Migrate utils::read_text_file to Filepath
-rw-r--r-- | include/utils.h | 3 | ||||
-rw-r--r-- | rust/libnewsboat-ffi/src/utils.rs | 23 | ||||
-rw-r--r-- | src/utils.cpp | 7 |
3 files changed, 22 insertions, 11 deletions
diff --git a/include/utils.h b/include/utils.h index d62c5e64..7bb8d4c9 100644 --- a/include/utils.h +++ b/include/utils.h @@ -12,6 +12,7 @@ #include "3rd-party/optional.hpp" #include "configcontainer.h" +#include "filepath.h" #include "libnewsboat-ffi/src/utils.rs.h" @@ -156,7 +157,7 @@ struct ReadTextFileError { std::string message; }; nonstd::expected<std::vector<std::string>, ReadTextFileError> read_text_file( - const std::string& filename); + const Filepath& filename); void remove_soft_hyphens(std::string& text); diff --git a/rust/libnewsboat-ffi/src/utils.rs b/rust/libnewsboat-ffi/src/utils.rs index 3959a625..2196b9c3 100644 --- a/rust/libnewsboat-ffi/src/utils.rs +++ b/rust/libnewsboat-ffi/src/utils.rs @@ -1,7 +1,7 @@ +use crate::filepath::PathBuf; use libc::{c_char, c_ulong}; use libnewsboat::utils::{self, *}; use std::ffi::{CStr, CString}; -use std::path::{Path, PathBuf}; #[cxx::bridge(namespace = "newsboat::utils")] mod ffi { @@ -37,6 +37,13 @@ mod ffi { // Functions that should be wrapped on the C++ side for ease of use. #[cxx::bridge(namespace = "newsboat::utils::bridged")] mod bridged { + #[namespace = "newsboat::filepath::bridged"] + extern "C++" { + include!("libnewsboat-ffi/src/filepath.rs.h"); + + type PathBuf = crate::filepath::PathBuf; + } + extern "Rust" { fn to_u(input: String, default_value: u32) -> u32; @@ -44,7 +51,7 @@ mod bridged { fn run_non_interactively(command: &str, caller: &str, exit_code: &mut u8) -> bool; fn read_text_file( - filename: String, + filename: &PathBuf, contents: &mut Vec<String>, error_line_number: &mut u64, error_reason: &mut String, @@ -127,12 +134,12 @@ fn run_non_interactively(command: &str, caller: &str, exit_code: &mut u8) -> boo } fn read_text_file( - filename: String, + filename: &PathBuf, contents: &mut Vec<String>, error_line_number: &mut u64, error_reason: &mut String, ) -> bool { - match utils::read_text_file(Path::new(&filename)) { + match utils::read_text_file(&filename.0) { Ok(c) => { *contents = c; true @@ -191,14 +198,14 @@ fn extract_filter(line: &str) -> ffi::FilterUrlParts { } fn resolve_tilde(path: &str) -> String { - let path = PathBuf::from(path); + let path = std::path::PathBuf::from(path); let result = utils::resolve_tilde(path); result.to_string_lossy().to_string() } fn resolve_relative(reference: &str, path: &str) -> String { - let reference = Path::new(reference); - let path = Path::new(path); + let reference = std::path::Path::new(reference); + let path = std::path::Path::new(path); let result = utils::resolve_relative(reference, path); result.to_string_lossy().to_string() } @@ -210,7 +217,7 @@ fn getcwd() -> String { } fn mkdir_parents(path: &str, mode: u32) -> isize { - let path = Path::new(path); + let path = std::path::Path::new(path); match utils::mkdir_parents(&path, mode) { Ok(_) => 0, Err(_) => -1, diff --git a/src/utils.cpp b/src/utils.cpp index 0f9ca082..6497c805 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -736,12 +736,15 @@ std::string utils::getcwd() } nonstd::expected<std::vector<std::string>, utils::ReadTextFileError> utils::read_text_file( - const std::string& filename) + const Filepath& filename) { rust::Vec<rust::String> c; std::uint64_t error_line_number{}; rust::String error_reason; - const bool result = bridged::read_text_file(filename, c, error_line_number, + const bool result = bridged::read_text_file( + filename, + c, + error_line_number, error_reason); if (result) { |