Skip to content

Commit

Permalink
clean main std
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Dec 19, 2024
1 parent 851d378 commit e3e239a
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 274 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[cfg(feature = "apple-keychain")]
#[path = "apple-keychain/mod.rs"]
pub mod apple_keychain;
#[path = "sans-io.rs"]
#[path = "sans-io/mod.rs"]
pub mod sans_io;
#[cfg(feature = "secret-service")]
#[path = "secret-service/mod.rs"]
Expand Down
189 changes: 0 additions & 189 deletions src/sans-io.rs

This file was deleted.

38 changes: 38 additions & 0 deletions src/sans-io/flow-entry-delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use super::{Flow, GetKey, Io};

/// [`Flow`] for deleting a secret from a keyring entry.
#[derive(Clone, Debug)]
pub struct DeleteEntryFlow {
key: String,
deleted: bool,
}

impl DeleteEntryFlow {
pub fn new(key: impl ToString) -> Self {
Self {
key: key.to_string(),
deleted: false,
}
}
}

impl Iterator for DeleteEntryFlow {
type Item = Io;

fn next(&mut self) -> Option<Self::Item> {
if self.deleted {
None
} else {
self.deleted = true;
Some(Io::Delete)
}
}
}

impl Flow for DeleteEntryFlow {}

impl GetKey for DeleteEntryFlow {
fn get_key(&self) -> &str {
self.key.as_str()
}
}
53 changes: 53 additions & 0 deletions src/sans-io/flow-entry-read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use secrecy::SecretSlice;

use super::{Flow, GetKey, Io, PutSecret, TakeSecret};

/// [`Flow`] for reading a secret from a keyring entry.
#[derive(Clone, Debug)]
pub struct ReadEntryFlow {
key: String,
secret: Option<SecretSlice<u8>>,
}

impl ReadEntryFlow {
/// Creates a new [`ReadEntryFlow`] from the given keyring entry
/// key.
pub fn new(key: impl ToString) -> Self {
Self {
key: key.to_string(),
secret: None,
}
}
}

impl Iterator for ReadEntryFlow {
type Item = Io;

fn next(&mut self) -> Option<Self::Item> {
if self.secret.is_none() {
Some(Io::Read)
} else {
None
}
}
}

impl Flow for ReadEntryFlow {}

impl GetKey for ReadEntryFlow {
fn get_key(&self) -> &str {
self.key.as_str()
}
}

impl TakeSecret for ReadEntryFlow {
fn take_secret(&mut self) -> Option<SecretSlice<u8>> {
self.secret.take()
}
}

impl PutSecret for ReadEntryFlow {
fn put_secret(&mut self, secret: SecretSlice<u8>) {
self.secret.replace(secret);
}
}
53 changes: 53 additions & 0 deletions src/sans-io/flow-entry-write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use secrecy::SecretSlice;

use super::{Flow, GetKey, Io, PutSecret, TakeSecret};

/// [`Flow`] for writing a secret into a keyring entry.
#[derive(Clone, Debug)]
pub struct WriteEntryFlow {
key: String,
secret: Option<SecretSlice<u8>>,
}

impl WriteEntryFlow {
/// Creates a new [`WriteEntryFlow`] from the given keyring entry
/// key and the given secret.
pub fn new(key: impl ToString, secret: impl Into<SecretSlice<u8>>) -> Self {
Self {
key: key.to_string(),
secret: Some(secret.into()),
}
}
}

impl Iterator for WriteEntryFlow {
type Item = Io;

fn next(&mut self) -> Option<Self::Item> {
if self.secret.is_some() {
Some(Io::Write)
} else {
None
}
}
}

impl Flow for WriteEntryFlow {}

impl GetKey for WriteEntryFlow {
fn get_key(&self) -> &str {
self.key.as_str()
}
}

impl TakeSecret for WriteEntryFlow {
fn take_secret(&mut self) -> Option<SecretSlice<u8>> {
self.secret.take()
}
}

impl PutSecret for WriteEntryFlow {
fn put_secret(&mut self, secret: SecretSlice<u8>) {
self.secret.replace(secret);
}
}
35 changes: 35 additions & 0 deletions src/sans-io/flow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use secrecy::SecretSlice;

/// Trait used for building keyring-related sans I/O state machine
/// flows.
///
/// A flow is defined as an iterable state machine, where every
/// `.next()` call produces a potential [`Io`] that needs to be
/// performed outside of the flow, and makes the state go forward. No
/// [`Io`] produced means that the flow is terminated and does not
/// require any longer [`Io`] to be performed.
pub trait Flow: Iterator {}

/// Trait dedicated to flows that operate on specific keyring entries.
///
/// This trait make sure that the given flow knows how to retrieve the
/// key of the targeted keyring entry.
pub trait GetKey: Flow {
fn get_key(&self) -> &str;
}

/// Trait dedicated to flows that needs to take secrets.
///
/// This trait make sure that the given flow knows how to take a
/// secret from its inner state.
pub trait TakeSecret: Flow {
fn take_secret(&mut self) -> Option<SecretSlice<u8>>;
}

/// Trait dedicated to flows that needs to put secrets.
///
/// This trait make sure that the given flow knows how to put a secret
/// into its inner state.
pub trait PutSecret: Flow {
fn put_secret(&mut self, secret: SecretSlice<u8>);
}
10 changes: 10 additions & 0 deletions src/sans-io/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// The type of I/O generated by keyring [`Flow`] state machines.
///
/// This enum is the representation of I/Os that need to be performed
/// outside of [`Flow`]s.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Io {
Read,
Write,
Delete,
}
Loading

0 comments on commit e3e239a

Please sign in to comment.