#[non_exhaustive]pub struct Secret(/* private fields */);Expand description
Represents a secret string, such as an API key.
It wraps secrecy::SecretString to prevent accidental exposure.
§Example
use api_openai::Secret;
// Create from environment variable (recommended)
let secret = Secret::load_from_env("OPENAI_API_KEY")?;
// Create from string with validation
let secret = Secret::new("sk-example123".to_string())?;
// Create without validation (for testing)
let secret = Secret::new_unchecked("test-key".to_string());Implementations§
Source§impl Secret
impl Secret
Sourcepub fn new(secret: String) -> Result<Self>
pub fn new(secret: String) -> Result<Self>
Creates a new Secret from a string with validation.
§Errors
Returns OpenAIError::InvalidArgument if the secret format is invalid.
Sourcepub fn new_unchecked(secret: String) -> Self
pub fn new_unchecked(secret: String) -> Self
Creates a new Secret from a string without validation.
This should only be used when the secret format is already known to be valid.
§Safety
This function bypasses validation and should only be used in controlled contexts.
Sourcepub fn load_with_path(path: &Path) -> Result<Self>
pub fn load_with_path(path: &Path) -> Result<Self>
Sourcepub fn load_from_env(env_var: &str) -> Result<Self>
pub fn load_from_env(env_var: &str) -> Result<Self>
Sourcepub fn load_from_workspace(key_name: &str, filename: &str) -> Result<Self>
pub fn load_from_workspace(key_name: &str, filename: &str) -> Result<Self>
Sourcepub fn load_with_fallbacks(key_name: &str) -> Result<Self>
pub fn load_with_fallbacks(key_name: &str) -> Result<Self>
Load secret with comprehensive fallback chain using workspace_tools
Priority order:
- Environment variable (fastest)
- Workspace secrets file (main secrets file)
- Alternative secrets files
§Arguments
key_name: The name of the secret key to load (e.g., “OPENAI_API_KEY”)
§Errors
Returns OpenAIError::MissingEnvironment if the key is not found in any location
§Examples
use api_openai::Secret;
// Tries environment variable first, then workspace secrets
let secret = Secret::load_with_fallbacks("OPENAI_API_KEY")?;Sourcepub fn exposure_count() -> u64
pub fn exposure_count() -> u64
Get the total number of secret exposures that have occurred This is useful for security auditing and monitoring
Trait Implementations§
Source§impl ExposeSecret<str> for Secret
impl ExposeSecret<str> for Secret
Source§fn expose_secret(&self) -> &str
fn expose_secret(&self) -> &str
Auto Trait Implementations§
impl Freeze for Secret
impl RefUnwindSafe for Secret
impl Send for Secret
impl Sync for Secret
impl Unpin for Secret
impl UnwindSafe for Secret
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Source§type Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry in complex collections.
For example, in a HashMap, while Entry might be a ( key, value ) tuple, Val might only be the value part.Source§fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Source§fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry function of the CollectionValToEntry trait to convert the value to an entry.
Source§type Entry = <C as CollectionValToEntry<Val>>::Entry
type Entry = <C as CollectionValToEntry<Val>>::Entry
Entry is defined by the Collection trait.