Skip to main content

VaultManager

Struct VaultManager 

Source
pub struct VaultManager {
    pub registry: VaultRegistry,
    pub open_vaults: HashMap<String, Vault>,
}

Fields§

§registry: VaultRegistry§open_vaults: HashMap<String, Vault>

Implementations§

Source§

impl VaultManager

Source

pub fn new() -> Result<Self>

Creates a new instance of the struct.

§Returns

Returns a Result containing a newly initialized instance of Self if the operation is successful. Otherwise, it returns an error.

§Procedure
  • Loads the vault registry using the VaultRegistry::load() method.
  • Initializes the open_vaults field as an empty HashMap.
§Errors

This function will return an error if VaultRegistry::load() fails.

Source

pub fn list_vaults(&self) -> Vec<&VaultInfo>

List all available vaults

Source

pub fn create_vault( &mut self, name: String, path: Option<PathBuf>, category: VaultCategory, description: Option<String>, master_password: &str, ) -> Result<String>

Creates a new vault with the specified parameters and initializes it with the given master password.

§Arguments
  • name - A String representing the name of the vault. This will serve as the identifying label for the vault.
  • path - An optional PathBuf specifying the file system location where the vault will be stored. If None is provided, a default location will be determined by the system.
  • category - A VaultCategory enum indicating the category or use case associated with this vault (e.g., personal, business).
  • description - An optional String for providing additional details or metadata about the vault.
  • master_password - A reference to a str that will be used to secure the freshly created vault. This master password is required for future vault access.
§Returns

Returns a Result<String>:

  • Ok(String) - The unique identifier (vault ID) of the newly created vault on successful creation and initialization.
  • Err - An error is returned if vault creation, retrieval, or initialization fails.
§Errors

This function may return an error in the following scenarios:

  • If the vault could not be successfully created in the registry.
  • If the vault information retrieval process encounters an issue.
  • If the vault initialization process fails (e.g., due to encryption setup or invalid master password).
§Panics
§Notes
  • Ensure that master_password is secure and not easily guessable, as it safeguards the integrity of the vault’s contents.
  • It is the caller’s responsibility to manage and securely store the returned vault ID for future reference.
§See Also
  • self.registry.create_vault - Handles the creation of the vault in the internal registry.
  • Vault::open_or_create - Opens an existing vault or creates a new one if it does not exist.
  • Vault::initialize - Prepares the vault for use by setting up encryption and other necessary configurations.
Source

pub fn open_vault( &mut self, vault_id: &str, master_password: &str, ) -> Result<()>

Opens a vault by its identifier and unlocks it using the provided master password.

§Parameters
  • vault_id: A string slice representing the unique identifier of the vault to be opened.
  • master_password: A string slice representing the master password used to unlock the vault.
§Returns
  • Ok(()): If the vault is successfully opened and unlocked.
  • Err: If the vault does not exist or an error occurs during any operation (e.g., unlocking or reading the vault).
§Errors
  • Returns an error if the vault with the given vault_id is not found in the registry.
  • Returns an error if unlocking the vault with the master_password fails.
  • Returns an error if there is an issue opening or creating the vault.
§Side Effects
  • Adds the opened vault to the open_vaults collection, which keeps track of currently open vaults.
Source

pub fn switch_active_vault(&mut self, vault_id: &str) -> Result<()>

Switches the active vault to the specified vault ID.

This function checks if the given vault_id exists in the registry’s list of vaults. If the vault exists, it sets the specified vault as the active vault. If the vault does not exist, an error is returned.

§Arguments
  • vault_id - A string slice that holds the ID of the vault to be set as active.
§Returns
  • Ok(()) if the active vault is successfully switched.
  • Err if the specified vault_id is not found in the registry’s list of vaults or if there is an error setting the active vault.
§Errors

This function returns an error under the following conditions:

  • The specified vault_id is not found in the registry.
  • There is an error while calling set_active_vault.
Source

pub fn get_active_vault(&mut self) -> Result<&mut Vault>

Retrieves a mutable reference to the currently active Vault.

This function checks if there is an active vault ID registered in self.registry. If no active vault ID is set, it returns an error indicating that there is no active vault. If an active vault ID is set but the corresponding vault is not unlocked (i.e., not present in self.open_vaults), it returns an error indicating that the vault is not unlocked. On success, it returns a mutable reference to the active Vault.

§Returns
  • Ok(&mut Vault) - A mutable reference to the active Vault if one exists and is unlocked.
  • Err(anyhow::Error) - An error if there is no active vault ID set or the vault is not unlocked.
§Errors
  • Returns an error with the message "No active vault" if self.registry.active_vault_id is None.
  • Returns an error with the message "Active vault '<ID>' is not unlocked" if the active vault is not found in self.open_vaults.
§Panics
  • This function will panic if unwrap() is called and the vault corresponding to the active ID is unexpectedly missing from self.open_vaults. However, this condition should not occur due to the prior contains_key check.
Source

pub fn close_vault(&mut self, vault_id: &str) -> Result<()>

Closes the vault associated with the given vault ID.

§Parameters
  • vault_id: A string slice that represents the unique identifier of the vault to be closed.
§Returns
  • Ok(()) if the operation was successful.
  • Err(e) if an error occurs (specific error type depends on the implementation of Result).
§Errors
§Behavior
  • This function removes the specified vault_id from the open_vaults collection.
  • After the operation, the vault will no longer be considered open.
Source

pub fn close_all_vaults(&mut self)

Close all vaults

Source

pub fn delete_vault(&mut self, vault_id: &str, delete_file: bool) -> Result<()>

Deletes a specified vault from the system.

This method performs the following operations:

  1. Closes the vault if it is currently open.
  2. Removes the vault from the registry.
  3. Optionally deletes the vault’s associated files from the storage if delete_file is set to true.
§Arguments
  • vault_id - A string slice that represents the unique identifier of the vault to be deleted.
  • delete_file - A boolean that, when true, ensures the associated vault files are also deleted from the system.
§Returns
  • Ok(()) on successful deletion of the vault.
  • Err if there is an issue with deleting the vault from the registry or associated files.
§Errors

Returns an error in the following cases:

  • If there is a failure while removing the vault from the registry.
  • If deleting the associated files fails when delete_file is true.
Source

pub fn import_vault( &mut self, vault_file: &Path, name: String, category: VaultCategory, copy_file: bool, ) -> Result<String>

Imports a vault into the registry.

This function allows you to import a vault by specifying the path to the vault file, a name for the vault, a category, and whether you want to copy the file during the import process.

§Arguments
  • vault_file - A reference to the file path of the vault to be imported.
  • name - A String representing the name to be assigned to the imported vault.
  • category - A VaultCategory specifying the category of the vault.
  • copy_file - A bool indicating whether the vault file should be copied during the import.
    • true: The file will be copied to the registry.
    • false: The original file path will be used without copying.
§Returns

Returns a Result containing:

  • Ok(String): A success message or identifier for the imported vault.
  • Err(_): An error if the import fails.
§Errors

This function will return an error if:

  • The file at the given path cannot be accessed.
  • The file cannot be imported due to issues such as invalid format or permissions.
  • The registry encounters an internal error during the import process.
Source

pub fn update_vault_info( &mut self, vault_id: &str, name: Option<String>, description: Option<String>, category: Option<VaultCategory>, is_favorite: Option<bool>, ) -> Result<()>

Updates the information of a vault identified by its vault_id.

This method allows updating various properties of a vault stored in the registry, such as its name, description, category, and favorite status. Each property can be updated optionally by passing a Some value, or left unchanged by passing None.

§Parameters
  • vault_id: A string slice that uniquely identifies the vault to be updated.
  • name: An Option<String> representing the new name of the vault. Pass Some(new_name) to update the name or None to leave it unchanged.
  • description: An Option<String> representing the new description for the vault. Pass Some(new_description) to update the description or None to leave it unchanged.
  • category: An Option<VaultCategory> specifying a new category for the vault. Pass Some(new_category) to update the category or None to leave it unchanged.
  • is_favorite: An Option<bool> indicating whether the vault is a favorite. Pass Some(true) to mark it as a favorite, Some(false) to remove it as a favorite, or None to leave this setting unchanged.
§Returns
  • Result<()>: Returns an Ok(()) on success, indicating that the vault’s information was successfully updated. Returns an error if vault_id does not exist or if there is an issue with updating the registry.
§Errors

This method will return an error in the following cases:

  • The registry fails to find the vault with the given vault_id.
  • An unexpected error occurs while trying to update the vault’s information.
Source

pub fn is_vault_open(&self, vault_id: &str) -> bool

Check if a vault is currently open/unlocked

Source

pub fn get_vault(&mut self, vault_id: &str) -> Option<&mut Vault>

Get vault by ID (must be open)

Trait Implementations§

Source§

impl Debug for VaultManager

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for VaultManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more