summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@pep-project.org>2017-11-30 18:52:45 +0100
committerJustus Winter <justus@pep-project.org>2017-12-01 13:54:28 +0100
commit8659731e6ecd694e7fa87799497aeb0338b7292c (patch)
treec5a9f0b6a30a6052ae063945506c90d890409bab /src/lib.rs
parente55151e501270875731cb89e83dd4e16402de025 (diff)
Create context objects.
- Context objects can be used to tweak the library configuration. Currently, a home and lib directory can be set. Reasonable defaults are provided. - Add ffi functions.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 079b6713..5530361e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -16,3 +16,68 @@ pub mod key_store;
pub mod net;
pub mod ffi;
pub mod armor;
+
+use std::env;
+use std::fs;
+use std::io;
+use std::path::{Path, PathBuf};
+
+/// A `&Context` is required for many operations.
+pub struct Context {
+ home: PathBuf,
+ lib: PathBuf,
+}
+
+fn prefix() -> PathBuf {
+ /* XXX: Windows support. */
+ PathBuf::from(option_env!("PREFIX").or(Some("/usr/local")).unwrap())
+}
+
+impl Context {
+ /// Create a `Pre(Context)` with reasonable defaults. `Pre(Context)`s
+ /// can be modified, and have to be finalized in order to turn
+ /// them into a Context.
+ pub fn new() -> Pre {
+ Pre(Context {
+ home: env::home_dir().unwrap_or(env::temp_dir())
+ .join(".sequoia"),
+ lib: prefix().join("lib").join("sequoia"),
+ })
+ }
+
+ /// Return the directory containing shared state and rendezvous
+ /// nodes.
+ pub fn home(&self) -> &Path {
+ &self.home
+ }
+
+ /// Return the directory containing backend servers.
+ pub fn lib(&self) -> &Path {
+ &self.lib
+ }
+}
+
+/// A `Pre(Context)` is a context object that can be modified.
+pub struct Pre(Context);
+
+impl Pre {
+ /// Finalize the configuration and return a `Context`.
+ pub fn finalize(self) -> io::Result<Context> {
+ let c = self.0;
+ fs::create_dir_all(c.home())?;
+ Ok(c)
+ }
+
+ /// Set the directory containing shared state and rendezvous
+ /// nodes.
+ pub fn home<P: AsRef<Path>>(mut self, new: P) -> Self {
+ self.0.home = PathBuf::new().join(new);
+ self
+ }
+
+ /// Set the directory containing backend servers.
+ pub fn lib<P: AsRef<Path>>(mut self, new: P) -> Self {
+ self.0.lib = PathBuf::new().join(new);
+ self
+ }
+}