Skip to content

Commit 6c2382b

Browse files
committed
Reorganise and improve consistency for module imports and exports.
1 parent e89eadb commit 6c2382b

4 files changed

Lines changed: 45 additions & 41 deletions

File tree

src/be/mock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
55
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
66

7-
use chrono::Utc;
87
use std::collections::hash_map::DefaultHasher;
98
use std::hash::{Hash, Hasher};
109
use std::path::{Path, PathBuf};
1110
use std::str::FromStr;
1211
use std::sync::RwLock;
1312

13+
use chrono::Utc;
14+
1415
use super::validation::{validate_be_name, validate_component};
1516
use super::{
1617
BootEnvironment, Client, Error, Label, MountMode, Root, Snapshot, generate_snapshot_name,

src/be/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
55
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
66

7+
use std::path::{Path, PathBuf};
8+
use std::str::FromStr;
9+
710
use clap::ValueEnum;
8-
use std::{
9-
path::{Path, PathBuf},
10-
str::FromStr,
11-
};
1211
use thiserror::Error as ThisError;
1312
#[cfg(feature = "dbus")]
1413
use zvariant::{DeserializeDict, SerializeDict, Type};
1514

16-
pub mod mock;
17-
pub mod validation;
18-
pub mod zfs;
15+
mod mock;
16+
mod validation;
17+
mod zfs;
18+
19+
pub use mock::EmulatorClient;
20+
pub use zfs::{LibZfsClient, format_zfs_bytes};
1921

2022
#[derive(ThisError, Debug)]
2123
pub enum Error {

src/dbus.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
55
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
66

7-
use crate::be::Error as BeError;
8-
use crate::be::{BootEnvironment, Client, Label, MountMode, Root, Snapshot};
9-
use event_listener::Listener;
107
use std::collections::{BTreeMap, HashMap, HashSet};
118
use std::path::{Path, PathBuf};
129
use std::str::FromStr;
1310
use std::sync::{Arc, Mutex, RwLock};
11+
12+
use event_listener::Listener;
1413
use tracing;
1514
use tracing_subscriber;
1615
use zbus::object_server::SignalEmitter;
1716
use zbus::{blocking, interface};
1817
use zvariant::ObjectPath;
1918

19+
use crate::be::{BootEnvironment, Client, Error, Label, MountMode, Root, Snapshot};
20+
2021
// D-Bus service constants
2122
const SERVICE_NAME: &str = "ca.kamacite.BootEnvironments1";
2223
const MANAGER_INTERFACE: &str = "ca.kamacite.BootEnvironmentManager";
@@ -42,7 +43,7 @@ impl ClientProxy {
4243
/// service or D-Bus itself is unavailable.
4344
///
4445
/// This will also ping the D-Bus service to check if it's available.
45-
pub fn new() -> Result<Self, BeError> {
46+
pub fn new() -> Result<Self, Error> {
4647
// This is equivalent to async_io::block_on(zbus::Connection::system())?.
4748
let connection = zbus::blocking::Connection::system()?;
4849
// Look up the ActiveRoot property, which is an indirect way to check if
@@ -75,7 +76,7 @@ impl Client for ClientProxy {
7576
source: Option<&Label>,
7677
properties: &[String],
7778
root: Option<&Root>,
78-
) -> Result<(), BeError> {
79+
) -> Result<(), Error> {
7980
let desc = description.unwrap_or("");
8081
let src = source.map(|label| label.to_string()).unwrap_or_default();
8182
let props: Vec<String> = properties.to_vec();
@@ -103,7 +104,7 @@ impl Client for ClientProxy {
103104
host_id: Option<&str>,
104105
properties: &[String],
105106
root: Option<&Root>,
106-
) -> Result<(), BeError> {
107+
) -> Result<(), Error> {
107108
let desc = description.unwrap_or("");
108109
let hid = host_id.unwrap_or("");
109110
let props: Vec<String> = properties.to_vec();
@@ -130,7 +131,7 @@ impl Client for ClientProxy {
130131
force_unmount: bool,
131132
snapshots: bool,
132133
root: Option<&Root>,
133-
) -> Result<(), BeError> {
134+
) -> Result<(), Error> {
134135
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
135136
match target {
136137
Label::Name(name) => self.connection.call_method(
@@ -157,7 +158,7 @@ impl Client for ClientProxy {
157158
mountpoint: Option<&Path>,
158159
mode: MountMode,
159160
root: Option<&Root>,
160-
) -> Result<PathBuf, BeError> {
161+
) -> Result<PathBuf, Error> {
161162
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
162163
let read_only = match mode {
163164
MountMode::ReadOnly => true,
@@ -183,7 +184,7 @@ impl Client for ClientProxy {
183184
be_name: &str,
184185
force: bool,
185186
root: Option<&Root>,
186-
) -> Result<Option<PathBuf>, BeError> {
187+
) -> Result<Option<PathBuf>, Error> {
187188
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
188189
let result: String = self
189190
.connection
@@ -204,12 +205,12 @@ impl Client for ClientProxy {
204205
}
205206
}
206207

207-
fn hostid(&self, _be_name: &str, _root: Option<&Root>) -> Result<Option<u32>, BeError> {
208+
fn hostid(&self, _be_name: &str, _root: Option<&Root>) -> Result<Option<u32>, Error> {
208209
// TODO: Decide whether to implement this.
209210
Ok(None)
210211
}
211212

212-
fn rename(&self, be_name: &str, new_name: &str, root: Option<&Root>) -> Result<(), BeError> {
213+
fn rename(&self, be_name: &str, new_name: &str, root: Option<&Root>) -> Result<(), Error> {
213214
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
214215
self.connection.call_method(
215216
Some(SERVICE_NAME),
@@ -221,7 +222,7 @@ impl Client for ClientProxy {
221222
Ok(())
222223
}
223224

224-
fn activate(&self, be_name: &str, temporary: bool, root: Option<&Root>) -> Result<(), BeError> {
225+
fn activate(&self, be_name: &str, temporary: bool, root: Option<&Root>) -> Result<(), Error> {
225226
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
226227
self.connection.call_method(
227228
Some(SERVICE_NAME),
@@ -233,7 +234,7 @@ impl Client for ClientProxy {
233234
Ok(())
234235
}
235236

236-
fn clear_boot_once(&self, root: Option<&Root>) -> Result<(), BeError> {
237+
fn clear_boot_once(&self, root: Option<&Root>) -> Result<(), Error> {
237238
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
238239
self.connection.call_method(
239240
Some(SERVICE_NAME),
@@ -245,7 +246,7 @@ impl Client for ClientProxy {
245246
Ok(())
246247
}
247248

248-
fn rollback(&self, be_name: &str, snapshot: &str, root: Option<&Root>) -> Result<(), BeError> {
249+
fn rollback(&self, be_name: &str, snapshot: &str, root: Option<&Root>) -> Result<(), Error> {
249250
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
250251
self.connection.call_method(
251252
Some(SERVICE_NAME),
@@ -257,10 +258,10 @@ impl Client for ClientProxy {
257258
Ok(())
258259
}
259260

260-
fn get_boot_environments(&self, root: Option<&Root>) -> Result<Vec<BootEnvironment>, BeError> {
261+
fn get_boot_environments(&self, root: Option<&Root>) -> Result<Vec<BootEnvironment>, Error> {
261262
let root = match root.or(self.active_root.as_ref()) {
262263
Some(root) => root,
263-
None => return Err(BeError::NoActiveBootEnvironment),
264+
None => return Err(Error::NoActiveBootEnvironment),
264265
};
265266
let body = self
266267
.connection
@@ -287,7 +288,7 @@ impl Client for ClientProxy {
287288
Ok(boot_environments)
288289
}
289290

290-
fn get_snapshots(&self, be_name: &str, root: Option<&Root>) -> Result<Vec<Snapshot>, BeError> {
291+
fn get_snapshots(&self, be_name: &str, root: Option<&Root>) -> Result<Vec<Snapshot>, Error> {
291292
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
292293
let snapshots_data: Vec<(String, Root, String, u64, i64)> = self
293294
.connection
@@ -324,7 +325,7 @@ impl Client for ClientProxy {
324325
source: Option<&Label>,
325326
description: Option<&str>,
326327
root: Option<&Root>,
327-
) -> Result<String, BeError> {
328+
) -> Result<String, Error> {
328329
let src = source.map(|label| label.to_string()).unwrap_or_default();
329330
let desc = description.unwrap_or("");
330331
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
@@ -342,7 +343,7 @@ impl Client for ClientProxy {
342343
Ok(result)
343344
}
344345

345-
fn init(&self, pool: &str) -> Result<(), BeError> {
346+
fn init(&self, pool: &str) -> Result<(), Error> {
346347
let _result: () = self
347348
.connection
348349
.call_method(
@@ -362,7 +363,7 @@ impl Client for ClientProxy {
362363
target: &Label,
363364
description: &str,
364365
root: Option<&Root>,
365-
) -> Result<(), BeError> {
366+
) -> Result<(), Error> {
366367
let target_str = target.to_string();
367368
let beroot = root.map(|r| r.as_str()).unwrap_or_default();
368369
self.connection.call_method(
@@ -894,7 +895,7 @@ impl<T: Client + 'static> BootEnvironmentManager<T> {
894895
.into_iter()
895896
.find(|be| be.name == name)
896897
.map(|be| be.guid)
897-
.ok_or_else(|| BeError::not_found(name))?;
898+
.ok_or_else(|| Error::not_found(name))?;
898899

899900
tracing::info!(
900901
name,
@@ -934,7 +935,7 @@ impl<T: Client + 'static> BootEnvironmentManager<T> {
934935
.into_iter()
935936
.find(|be| be.name == name)
936937
.map(|be| be.guid)
937-
.ok_or_else(|| BeError::not_found(name))?;
938+
.ok_or_else(|| Error::not_found(name))?;
938939

939940
tracing::info!(name, description = desc, "Created empty boot environment");
940941
self.refresh(conn.object_server()).await?;

src/main.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
55
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
66

7+
use std::fs;
8+
use std::path::PathBuf;
9+
710
use anyhow::{Context, Result};
811
use chrono::TimeZone;
912
use clap::{Parser, Subcommand, ValueEnum};
10-
use std::fs;
11-
use std::path::PathBuf;
13+
14+
use be::{
15+
BootEnvironment, Client, EmulatorClient, Error, Label, LibZfsClient, MountMode, Root, Snapshot,
16+
format_zfs_bytes, is_temp_mountpoint,
17+
};
1218

1319
mod be;
1420
#[cfg(feature = "dbus")]
1521
mod dbus;
1622
#[cfg(feature = "hooks")]
1723
mod hooks;
1824

19-
use be::mock::EmulatorClient;
20-
use be::zfs::{LibZfsClient, format_zfs_bytes};
21-
use be::{BootEnvironment, Client, Error, Label, MountMode, Root, Snapshot, is_temp_mountpoint};
22-
#[cfg(feature = "dbus")]
23-
use dbus::{ClientProxy, serve};
24-
2525
#[derive(Parser)]
2626
#[command(version, about = "Boot Environment Administration")]
2727
struct Cli {
@@ -745,7 +745,7 @@ fn execute_command<T: Client + 'static>(
745745
.enable_all()
746746
.build()
747747
.expect("launch of a multi-threaded tokio runtime")
748-
.block_on(serve(client, *user))
748+
.block_on(dbus::serve(client, *user))
749749
.context("Failed to start D-Bus service")?;
750750
Ok(())
751751
}
@@ -776,7 +776,7 @@ fn main() -> Result<()> {
776776
// When the client type is "default", we check if the D-Bus service
777777
// is available but don't emit errors if it's not.
778778
#[cfg(feature = "dbus")]
779-
if let Ok(client) = ClientProxy::new() {
779+
if let Ok(client) = dbus::ClientProxy::new() {
780780
return execute_command(&cli.command, cli.root.as_ref(), client);
781781
} else if cli.verbose {
782782
println!("D-Bus service not available, falling back to libzfs.");
@@ -788,7 +788,7 @@ fn main() -> Result<()> {
788788
#[cfg(feature = "dbus")]
789789
ClientType::DBus => {
790790
// When the client type is explicitly "dbus", errors are fatal.
791-
let client = ClientProxy::new()?;
791+
let client = dbus::ClientProxy::new()?;
792792
execute_command(&cli.command, cli.root.as_ref(), client)
793793
}
794794
ClientType::LibZfs => execute_command(&cli.command, cli.root.as_ref(), LibZfsClient::new()),

0 commit comments

Comments
 (0)