Struct WineConfig

Source
pub struct WineConfig { /* private fields */ }
Expand description

The main conversion struct: create one of these to do conversions.

Tracks the WINEPREFIX and the drive letter mappings so they don’t have to be recomputed every time you convert a path.

Implementations§

Source§

impl WineConfig

Source

pub fn from_env() -> Result<Self, WinePathError>

Determine the wine prefix from the environment.

Examples found in repository?
examples/winepath.rs (line 31)
9fn main() {
10    let mut action = Action::ToUnix;
11    let mut path = None;
12
13    if let Some(arg) = std::env::args().nth(1) {
14        match arg.as_str() {
15            "-u" => action = Action::ToUnix,
16            "-w" => action = Action::ToWindows,
17            _ => path = Some(arg),
18        }
19    } else {
20        panic!("usage: winepath [OPTION] [PATH]")
21    };
22
23    let path = if let Some(path) = path {
24        path
25    } else if let Some(path) = std::env::args().nth(2) {
26        path
27    } else {
28        panic!("usage: winepath [OPTION] [PATH]");
29    };
30
31    let config = WineConfig::from_env().unwrap();
32    println!(
33        "{}",
34        match action {
35            Action::ToUnix => config
36                .to_native_path(path)
37                .unwrap()
38                .to_string_lossy()
39                .to_string(),
40            Action::ToWindows => {
41                let path = std::fs::canonicalize(path).unwrap();
42                config.to_wine_path(path).unwrap().to_string()
43            }
44        }
45    )
46}
Source

pub fn from_prefix(path: impl Into<PathBuf>) -> Self

Create a config assuming that the given path is a valid WINEPREFIX.

Note that this is not validated, and you will end up with empty drive mappings if it is not actually a wine prefix.

You can manually validate if a directory is Wine-y enough by doing:

use std::path::Path;
fn is_wineprefix_like(some_path: &Path) -> bool {
    some_path.join("dosdevices").is_dir()
}
Source

pub fn prefix(&self) -> &Path

Get the current wine prefix.

Source

pub fn to_wine_path( &self, path: impl AsRef<Path>, ) -> Result<WinePath, WinePathError>

Convert a native file path to a Wine path.

use winepath::WineConfig;
let config = WineConfig::from_env().unwrap();
let path = config.to_wine_path("/home/username/.wine/drive_c/Program Files/CoolApp/start.exe").unwrap();
assert_eq!(path.to_string(), r"c:\Program Files\CoolApp\start.exe");
let path = config.to_wine_path("/home/username/some-path/some-file").unwrap();
assert_eq!(path.to_string(), r"z:\home\username\some-path\some-file");
Examples found in repository?
examples/winepath.rs (line 42)
9fn main() {
10    let mut action = Action::ToUnix;
11    let mut path = None;
12
13    if let Some(arg) = std::env::args().nth(1) {
14        match arg.as_str() {
15            "-u" => action = Action::ToUnix,
16            "-w" => action = Action::ToWindows,
17            _ => path = Some(arg),
18        }
19    } else {
20        panic!("usage: winepath [OPTION] [PATH]")
21    };
22
23    let path = if let Some(path) = path {
24        path
25    } else if let Some(path) = std::env::args().nth(2) {
26        path
27    } else {
28        panic!("usage: winepath [OPTION] [PATH]");
29    };
30
31    let config = WineConfig::from_env().unwrap();
32    println!(
33        "{}",
34        match action {
35            Action::ToUnix => config
36                .to_native_path(path)
37                .unwrap()
38                .to_string_lossy()
39                .to_string(),
40            Action::ToWindows => {
41                let path = std::fs::canonicalize(path).unwrap();
42                config.to_wine_path(path).unwrap().to_string()
43            }
44        }
45    )
46}
Source

pub fn to_native_path( &self, path: impl Into<WinePath>, ) -> Result<PathBuf, WinePathError>

Convert a Wine path to a native file path.

use winepath::WineConfig;
use std::path::PathBuf;
let config = WineConfig::from_env().unwrap();
let path = config.to_native_path(r"c:\Program Files\CoolApp\start.exe").unwrap();
assert_eq!(path, PathBuf::from("/home/username/.wine/drive_c/Program Files/CoolApp/start.exe"));
let path = config.to_native_path(r"z:\home\username\some-path\some-file").unwrap();
assert_eq!(path, PathBuf::from("/home/username/some-path/some-file"));
Examples found in repository?
examples/winepath.rs (line 36)
9fn main() {
10    let mut action = Action::ToUnix;
11    let mut path = None;
12
13    if let Some(arg) = std::env::args().nth(1) {
14        match arg.as_str() {
15            "-u" => action = Action::ToUnix,
16            "-w" => action = Action::ToWindows,
17            _ => path = Some(arg),
18        }
19    } else {
20        panic!("usage: winepath [OPTION] [PATH]")
21    };
22
23    let path = if let Some(path) = path {
24        path
25    } else if let Some(path) = std::env::args().nth(2) {
26        path
27    } else {
28        panic!("usage: winepath [OPTION] [PATH]");
29    };
30
31    let config = WineConfig::from_env().unwrap();
32    println!(
33        "{}",
34        match action {
35            Action::ToUnix => config
36                .to_native_path(path)
37                .unwrap()
38                .to_string_lossy()
39                .to_string(),
40            Action::ToWindows => {
41                let path = std::fs::canonicalize(path).unwrap();
42                config.to_wine_path(path).unwrap().to_string()
43            }
44        }
45    )
46}

Trait Implementations§

Source§

impl Debug for WineConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.