nuts_tool_api/lib.rs
1// MIT License
2//
3// Copyright (c) 2024 Robin Doer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22
23mod bson;
24mod info;
25mod msg;
26#[cfg(feature = "plugin")]
27pub mod plugin;
28#[cfg(feature = "tool")]
29pub mod tool;
30
31use log::debug;
32use std::fs;
33use std::io::{self, ErrorKind};
34use std::path::PathBuf;
35
36pub use bson::{BsonError, BsonReader, BsonWriter};
37pub use info::{PluginInfo, CURRENT_REVISION};
38pub use msg::{ErrorResponse, OkResponse, Request, Response};
39
40pub fn tool_dir() -> io::Result<PathBuf> {
41 match home::home_dir() {
42 Some(dir) => {
43 let tool_dir = dir.join(".nuts");
44
45 debug!("tool_dir: {}", tool_dir.display());
46
47 if !tool_dir.is_dir() {
48 debug!("creating tool dir {}", tool_dir.display());
49 fs::create_dir(&tool_dir)?;
50 }
51
52 Ok(tool_dir)
53 }
54 None => Err(io::Error::new(
55 ErrorKind::NotFound,
56 "unable to locate home-directory",
57 )),
58 }
59}
60
61pub fn container_dir() -> io::Result<PathBuf> {
62 let parent = tool_dir()?;
63 let dir = parent.join("container.d");
64
65 debug!("container_dir: {}", dir.display());
66
67 if !dir.is_dir() {
68 debug!("creating container dir {}", dir.display());
69 fs::create_dir(&dir)?;
70 }
71
72 Ok(dir)
73}
74
75pub fn container_dir_for<S: AsRef<str>>(name: S) -> io::Result<PathBuf> {
76 let parent = container_dir()?;
77 let dir = parent.join(name.as_ref());
78
79 debug!("container_dir for {}: {}", name.as_ref(), dir.display());
80
81 Ok(dir)
82}