1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use crate::preferences::{Pref, Preferences};
use crate::prefreader::{parse, serialize, PrefReaderError};
use std::collections::btree_map::Iter;
use std::fs::File;
use std::io::prelude::*;
use std::io::Result as IoResult;
use std::path::{Path, PathBuf};
use tempfile::{Builder, TempDir};
#[derive(Debug)]
pub struct Profile {
pub path: PathBuf,
pub temp_dir: Option<TempDir>,
prefs: Option<PrefFile>,
user_prefs: Option<PrefFile>,
}
impl Profile {
pub fn new() -> IoResult<Profile> {
let dir = Builder::new().prefix("rust_mozprofile").tempdir()?;
let path = dir.path().to_path_buf();
let temp_dir = Some(dir);
Ok(Profile {
path,
temp_dir,
prefs: None,
user_prefs: None,
})
}
pub fn new_from_path(p: &Path) -> IoResult<Profile> {
let path = p.to_path_buf();
let temp_dir = None;
Ok(Profile {
path,
temp_dir,
prefs: None,
user_prefs: None,
})
}
pub fn prefs(&mut self) -> Result<&mut PrefFile, PrefReaderError> {
if self.prefs.is_none() {
let mut pref_path = PathBuf::from(&self.path);
pref_path.push("prefs.js");
self.prefs = Some(PrefFile::new(pref_path)?)
};
// This error handling doesn't make much sense
Ok(self.prefs.as_mut().unwrap())
}
pub fn user_prefs(&mut self) -> Result<&mut PrefFile, PrefReaderError> {
if self.user_prefs.is_none() {
let mut pref_path = PathBuf::from(&self.path);
pref_path.push("user.js");
self.user_prefs = Some(PrefFile::new(pref_path)?)
};
// This error handling doesn't make much sense
Ok(self.user_prefs.as_mut().unwrap())
}
}
#[derive(Debug)]
pub struct PrefFile {
path: PathBuf,
pub prefs: Preferences,
}
impl PrefFile {
pub fn new(path: PathBuf) -> Result<PrefFile, PrefReaderError> {
let prefs = if !path.exists() {
Preferences::new()
} else {
let mut f = File::open(&path)?;
let mut buf = String::with_capacity(4096);
f.read_to_string(&mut buf)?;
parse(buf.as_bytes())?
};
Ok(PrefFile { path, prefs })
}
pub fn write(&self) -> IoResult<()> {
let mut f = File::create(&self.path)?;
serialize(&self.prefs, &mut f)
}
pub fn insert_slice<K>(&mut self, preferences: &[(K, Pref)])
where
K: Into<String> + Clone,
{
for &(ref name, ref value) in preferences.iter() {
self.insert((*name).clone(), (*value).clone());
}
}
pub fn insert<K>(&mut self, key: K, value: Pref)
where
K: Into<String>,
{
self.prefs.insert(key.into(), value);
}
pub fn remove(&mut self, key: &str) -> Option<Pref> {
self.prefs.remove(key)
}
pub fn get(&mut self, key: &str) -> Option<&Pref> {
self.prefs.get(key)
}
pub fn contains_key(&self, key: &str) -> bool {
self.prefs.contains_key(key)
}
pub fn iter(&self) -> Iter<String, Pref> {
self.prefs.iter()
}
}
|