Commit 9693d992 authored by Andy Barron's avatar Andy Barron
Browse files

initial implementations of unix and os x

parent 8177249f
[package]
name = "app_dirs"
version = "0.0.0"
version = "0.0.1"
authors = ["Andy Barron <AndrewLBarron@gmail.com>"]
description = "Access platform-dependent canonical locations for app-specific data"
......@@ -11,3 +11,5 @@ keywords = ["application", "data", "storage", "location", "directory"]
license = "MIT"
[dependencies]
# TODO conditional dependencies when Rust 1.8.0 is released
xdg = "^2.0.0"
# app-dirs-rs
*Access platform-dependent canonical locations for app-specific data*
TBD :)
\ No newline at end of file
use std;
#[derive(Clone, Debug)]
pub struct AppInfo {
name: String,
author: String,
safe_name: String,
safe_author: String,
}
impl AppInfo {
pub fn new(name: &str, author: &str) -> Self {
AppInfo {
name: name.into(),
author: author.into(),
safe_name: name.into(), // TODO
safe_author: author.into(), // TODO
}
}
pub fn safe_name(&self) -> &str {
&self.safe_name
}
pub fn safe_author(&self) -> &str {
&self.safe_author
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn get_author(&self) -> &str {
&self.author
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum AppDirType {
UserConfig,
UserData,
UserCache,
SharedData,
SharedConfig,
}
impl AppDirType {
pub fn is_shared(&self) -> bool {
use AppDirType::*;
match *self {
SharedData | SharedConfig => true,
_ => false,
}
}
}
pub enum AppDirError {
Io(std::io::Error),
}
impl From<std::io::Error> for AppDirError {
fn from(e: std::io::Error) -> Self {
AppDirError::Io(e)
}
}
pub type AppDirResult<T> = Result<T, AppDirError>;
\ No newline at end of file
#[cfg(target_os="macos")]
mod platform {
mod macos;
pub use self::macos::*;
}
#[cfg(all(unix, not(target_os="macos")))]
mod platform {
mod unix;
pub use self::unix::*;
}
#[cfg(windows)]
mod platform {
mod windows;
pub use self::windows::*;
}
pub use self::platform::*;
// TODO function to create dir (by calling create_dir_all)
// TODO helper functions (optionally using AppInfo) to read/write from actual files
\ No newline at end of file
use common::*;
use AppDirType::*;
use std::env::home_dir;
use std::path::{Component, PathBuf};
pub fn get_app_dir(t: AppDirType) -> Option<PathBuf> {
let dir_base: Option<PathBuf> = if t.is_shared() {
Some(Component::RootDir.as_ref().into())
} else {
home_dir()
};
dir_base.map(|mut path| {
match t {
UserConfig | UserData | SharedConfig | SharedData => {
path.push("Library");
path.push("Application Support");
}
UserCache => {
path.push("Library");
path.push("Caches");
}
};
path
})
}
extern crate xdg;
use self::xdg::BaseDirectories as Xdg;
use common::*;
use std::path::PathBuf;
use AppDirType::*;
pub fn get_app_dir(t: AppDirType) -> Option<PathBuf> {
Xdg::new().ok().as_ref().and_then(|x| match t {
UserConfig => Some(x.get_config_home()),
UserData => Some(x.get_data_home()),
UserCache => Some(x.get_cache_home()),
SharedData => x.get_data_dirs().into_iter().next(),
SharedConfig => x.get_config_dirs().into_iter().next(),
})
}
use common::*;
use std::path::PathBuf;
pub fn get_app_dir(t: AppDirType) -> Option<PathBuf> {
unimplemented!()
}
mod common;
pub use common::*;
mod imp;
pub use imp::*;
#[cfg(test)]
mod tests {
use super::*;
use AppDirType::*;
#[test]
fn it_works() {
// let info = AppInfo::new("Fancy Dev", "Cool App");
let types = [
UserConfig,
UserData,
UserCache,
SharedData,
SharedConfig,
];
for &t in types.iter() {
println!("{:?} = {:?}", t, get_app_dir(t))
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment