pub struct Ob { /* private fields */ }Expand description
A flexible ObtextCodec implementation with runtime format selection.
Ob allows you to specify any format at runtime via constructor parameters,
and provides methods to change the format after construction if needed.
This provides a unified interface for all runtime format needs, from immutable configurations to dynamic format switching.
§Examples
§Basic usage with immutable format
let ob = Ob::new("aasv.b64", &key)?;
let ot = ob.enc("hello")?; // obtext
let pt2 = ob.dec(&ot)?; // recovered plaintext
assert_eq!(pt2, "hello");§Dynamic format switching
let mut ob = Ob::new("aasv.c32", &key)?;
let ot1 = ob.enc("hello")?; // aasv.c32 format
// Change format at runtime
ob.set_scheme(Scheme::Mock1)?;
let ot2 = ob.enc("hello")?; // mock1.c32 format
// Change encoding
ob.set_encoding(Encoding::B64)?; // now mock1.b64
// Set entire format at once
ob.set_format("aasv.hex")?; // now aasv.hex
ob.set_format(AASV_B64)?; // now aasv.b64 (using constant)Implementations§
Source§impl Ob
impl Ob
Sourcepub fn new(format: impl IntoFormat, key: &str) -> Result<Self, Error>
pub fn new(format: impl IntoFormat, key: &str) -> Result<Self, Error>
Create a new Ob with the specified format and base64 key.
Accepts either a format string (&str) or a Format instance.
§Examples
// Using format string
let ob1 = Ob::new("aasv.b64", &key)?;
// Using Format instance
let format = Format::new(Scheme::Aasv, Encoding::B64);
let ob2 = Ob::new(format, &key)?;Sourcepub fn set_format(&mut self, format: impl IntoFormat) -> Result<(), Error>
pub fn set_format(&mut self, format: impl IntoFormat) -> Result<(), Error>
Set the format to a new value.
Accepts either a format string (&str) or a Format instance.
§Examples
let mut ob = Ob::new("aasv.c32", &key)?;
ob.set_format("mock1.b64")?; // switch using string
ob.set_format(Format::new(Scheme::Mock2, Encoding:: Hex))?; // switch using FormatSourcepub fn set_scheme(&mut self, scheme: Scheme) -> Result<(), Error>
pub fn set_scheme(&mut self, scheme: Scheme) -> Result<(), Error>
Set the scheme while keeping the current encoding.
§Examples
let mut ob = Ob::new("aasv.c32", &key)?;
ob.set_scheme(Scheme::Mock1)?; // switch to mock1, keeping c32 encodingSourcepub fn set_encoding(&mut self, encoding: Encoding) -> Result<(), Error>
pub fn set_encoding(&mut self, encoding: Encoding) -> Result<(), Error>
Set the encoding while keeping the current scheme.
§Examples
let mut ob = Ob::new("aasv.c32", &key)?;
ob.set_encoding(Encoding::B64)?; // switch to b64, keeping aasv schemeSourcepub fn autodec(&self, obtext: &str) -> Result<String, Error>
pub fn autodec(&self, obtext: &str) -> Result<String, Error>
Decode and decrypt obtext with automatic format detection.
Tries to decode using the instance’s current encoding first (fast path), then falls back to full format autodetection if that fails.
§Examples
let mut ob = Ob::new("aasv.b64", &key)?;
let ot = ob.enc("test")?;
// Change scheme - autodec will still work
ob.set_scheme(oboron::Scheme::Mock1)?;
let pt2 = ob.autodec(&ot)?;
assert_eq!(pt2, "test");
// Works even with different encoding (slower fallback path)
ob.set_encoding(oboron::Encoding:: Hex)?;
let pt3 = ob.autodec(&ot)?; // Falls back to full autodetection
assert_eq!(pt3, "test");Source§impl Ob
impl Ob
Sourcepub fn enc(&self, plaintext: &str) -> Result<String, Error>
pub fn enc(&self, plaintext: &str) -> Result<String, Error>
Encrypt and encode plaintext to obtext.
§Examples
let ob = Ob::new("aasv.b64", &key)?;
let ot = ob.enc("secret data")?;
assert! (!ot.is_empty());Sourcepub fn format(&self) -> Format
pub fn format(&self) -> Format
Get the current format (scheme + encoding).
§Examples
let ob = Ob::new("aasv.b64", &key)?;
let format = ob.format();
assert_eq!(format.scheme(), Scheme::Aasv);
assert_eq!(format.encoding(), Encoding::B64);