### Initialize DmgWiz from Reader Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.DmgWiz.html Creates a DmgWiz instance from a seekable byte stream. ```rust use std::fs::File; use dmgwiz::{DmgWiz, Verbosity}; let input = File::open("tests/input_zlib.dmg").unwrap(); let mut wiz = match DmgWiz::from_reader(input, Verbosity::None) { Err(err) => panic!(format!("could not read input file - {}", err)), Ok(val) => val, }; ``` -------------------------------- ### DmgWiz::from_reader Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.DmgWiz.html Creates a new DmgWiz instance by parsing a DMG file from a seekable byte stream. ```APIDOC ## pub fn from_reader(input: R, verbosity: Verbosity) -> Result> ### Description Creates a `DmgWiz` instance from a seekable byte stream. ### Parameters - **input** (R: Read + Seek) - Required - A seekable reader. - **verbosity** (Verbosity) - Required - Verbosity level for debugging. ### Example ```rust use std::fs::File; use dmgwiz::{DmgWiz, Verbosity}; let input = File::open("tests/input_zlib.dmg").unwrap(); let mut wiz = match DmgWiz::from_reader(input, Verbosity::None) { Err(err) => panic!(format!("could not read input file - {}", err)), Ok(val) => val, }; ``` ``` -------------------------------- ### Initialize EncryptedDmgReader Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.EncryptedDmgReader.html Create a new reader instance from a seekable byte stream using a password and verbosity level. ```rust use std::fs::File; use dmgwiz::{EncryptedDmgReader, Verbosity}; let input = File::open("tests/input_aes256.dmg").unwrap(); let mut reader = match EncryptedDmgReader::from_reader(input, "test123", Verbosity::None) { Err(err) => panic!(format!("could not read input file - {}", err)), Ok(val) => val, }; ``` -------------------------------- ### Extract All Partitions Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.DmgWiz.html Decompresses all partitions from the DMG and writes them to the provided output stream. ```rust use std::{fs::File, io::BufWriter}; use dmgwiz::{DmgWiz, Verbosity}; let input = File::open("tests/input_zlib.dmg").unwrap(); let outfile = File::create("tests/tmp_output_zlib.bin").unwrap(); let output = BufWriter::new(outfile); let mut wiz = DmgWiz::from_reader(input, Verbosity::None).unwrap(); match wiz.extract_all(output) { Err(err) => panic!(format!("error while extracting: {}", err)), Ok(bytes) => println!("done. {} bytes written", bytes), } ``` -------------------------------- ### pub fn unwrap_or_default(self) -> T Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Ok value or a default value if the result is Err. ```APIDOC ## pub fn unwrap_or_default(self) -> T ### Description Consumes the `self` argument then, if `Ok`, returns the contained value, otherwise if `Err`, returns the default value for that type. ### Parameters - **self** (Result) - The Result instance. ### Returns - **T** - The contained value or the default value for type T. ``` -------------------------------- ### DmgWiz::extract_all Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.DmgWiz.html Decompresses all partitions within the DMG and writes the output to a provided writer. ```APIDOC ## pub fn extract_all(&mut self, output: W) -> Result ### Description Decompress all partitions and write to output. ### Parameters - **output** (W: Write + Seek) - Required - A seekable writer. ### Example ```rust use std::{fs::File, io::BufWriter}; use dmgwiz::{DmgWiz, Verbosity}; let input = File::open("tests/input_zlib.dmg").unwrap(); let outfile = File::create("tests/tmp_output_zlib.bin").unwrap(); let output = BufWriter::new(outfile); let mut wiz = DmgWiz::from_reader(input, Verbosity::None).unwrap(); match wiz.extract_all(output) { Err(err) => panic!(format!("error while extracting: {}", err)), Ok(bytes) => println!("done. {} bytes written", bytes), } ``` ``` -------------------------------- ### Copying Result Contents Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Methods to copy or clone the contents of an Ok variant from a reference. ```rust let val = 12; let x: Result<&i32, i32> = Ok(&val); assert_eq!(x, Ok(&12)); let copied = x.copied(); assert_eq!(copied, Ok(12)); ``` ```rust let val = 12; let x: Result<&i32, i32> = Ok(&val); assert_eq!(x, Ok(&12)); let cloned = x.cloned(); assert_eq!(cloned, Ok(12)); ``` ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let copied = x.copied(); assert_eq!(copied, Ok(12)); ``` ```rust let mut val = 12; let x: Result<&mut i32, i32> = Ok(&mut val); assert_eq!(x, Ok(&mut 12)); let cloned = x.cloned(); assert_eq!(cloned, Ok(12)); ``` -------------------------------- ### Chain Result with And Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the provided result if the current result is Ok, otherwise returns the current Err. ```rust let x: Result = Ok(2); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("late error")); let x: Result = Err("early error"); let y: Result<&str, &str> = Ok("foo"); assert_eq!(x.and(y), Err("early error")); let x: Result = Err("not a 2"); let y: Result<&str, &str> = Err("late error"); assert_eq!(x.and(y), Err("not a 2")); let x: Result = Ok(2); let y: Result<&str, &str> = Ok("different result type"); assert_eq!(x.and(y), Ok("different result type")); ``` -------------------------------- ### pub fn unwrap(self) -> T Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Ok value, consuming the self value. Panics if the value is an Err. ```APIDOC ## pub fn unwrap(self) -> T ### Description Returns the contained `Ok` value, consuming the `self` value. Because this function may panic, its use is generally discouraged. ### Parameters - **self** (Result) - The Result instance to unwrap. ### Returns - **T** - The contained value if Ok. ### Panics Panics if the value is an `Err`, with a panic message provided by the `Err`’s value. ``` -------------------------------- ### Calculate Product of Result Iterator Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Shows how to compute the product of a sequence of Results, short-circuiting on the first Err encountered. ```rust let nums = vec!["5", "10", "1", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert_eq!(total, Ok(100)); let nums = vec!["5", "10", "one", "2"]; let total: Result = nums.iter().map(|w| w.parse::()).product(); assert!(total.is_err()); ``` -------------------------------- ### pub fn and(self, res: Result) -> Result Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns res if the result is Ok, otherwise returns the Err value of self. ```APIDOC ## pub fn and(self, res: Result) -> Result ### Description Returns `res` if the result is `Ok`, otherwise returns the `Err` value of `self`. ### Parameters - **res** (Result) - The result to return if self is Ok. ### Returns - **Result** - The provided result or the original error. ``` -------------------------------- ### Convert Result to Iterator Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Demonstrates converting a Result into an iterator, which yields zero elements if the result is an Err. ```rust let x: Result = Err("nothing!"); let v: Vec = x.into_iter().collect(); assert_eq!(v, []); ``` -------------------------------- ### Fallback to alternative Result with or Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the provided result if the current one is Err, otherwise returns the Ok value. Arguments are evaluated eagerly. ```rust let x: Result = Ok(2); let y: Result = Err("late error"); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("early error"); let y: Result = Ok(2); assert_eq!(x.or(y), Ok(2)); let x: Result = Err("not a 2"); let y: Result = Err("late error"); assert_eq!(x.or(y), Err("late error")); let x: Result = Ok(2); let y: Result = Ok(100); assert_eq!(x.or(y), Ok(2)); ``` -------------------------------- ### Partition Struct Definition Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.Partition.html The definition of the Partition struct, which holds the name of the partition. ```rust pub struct Partition { pub name: String, /* private fields */ } ``` -------------------------------- ### Inspect Ok Variant Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Executes a closure with a reference to the Ok value without modifying the result. ```rust let x: u8 = "4" .parse::() .inspect(|x| println!("original: {x}")) .map(|x| x.pow(3)) .expect("failed to parse number"); ``` -------------------------------- ### Expect Result Value Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the Ok value or panics with a provided message if the result is Err. ```rust let x: Result = Err("emergency failure"); x.expect("Testing expect"); // panics with `Testing expect: emergency failure` ``` ```rust let path = std::env::var("IMPORTANT_PATH") .expect("env variable `IMPORTANT_PATH` should be set by `wrapper_script.sh`"); ``` -------------------------------- ### Convert Result to Option Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Methods for converting Result to Option by consuming the Result. ```rust let x: Result = Ok(2); assert_eq!(x.ok(), Some(2)); let x: Result = Err("Nothing here"); assert_eq!(x.ok(), None); ``` ```rust let x: Result = Ok(2); assert_eq!(x.err(), None); let x: Result = Err("Nothing here"); assert_eq!(x.err(), Some("Nothing here")); ``` -------------------------------- ### Reference Result with as_ref and as_mut Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Converts a Result to a Result containing references to the original values. ```rust let x: Result = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); ``` ```rust fn mutate(r: &mut Result) { match r.as_mut() { Ok(v) => *v = 42, Err(e) => *e = 0, } } let mut x: Result = Ok(2); mutate(&mut x); assert_eq!(x.unwrap(), 42); let mut x: Result = Err(13); mutate(&mut x); assert_eq!(x.unwrap_err(), 0); ``` -------------------------------- ### Unwrap with closure default using unwrap_or_else Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Ok value or computes a default value using the provided closure. ```rust fn count(x: &str) -> usize { x.len() } ``` -------------------------------- ### Unwrap Result with Default Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the Ok value or the default value of the type if the result is Err. ```rust let good_year_from_input = "1909"; let bad_year_from_input = "190blarg"; let good_year = good_year_from_input.parse().unwrap_or_default(); let bad_year = bad_year_from_input.parse().unwrap_or_default(); assert_eq!(1909, good_year); assert_eq!(0, bad_year); ``` -------------------------------- ### Map Result with defaults Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Maps a Result to a value, providing a default or fallback for Err cases. ```rust let x: Result<_, &str> = Ok("foo"); assert_eq!(x.map_or(42, |v| v.len()), 3); let x: Result<&str, _> = Err("bar"); assert_eq!(x.map_or(42, |v| v.len()), 42); ``` ```rust let k = 21; let x : Result<_, &str> = Ok("foo"); assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 3); let x : Result<&str, _> = Err("bar"); assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42); ``` ```rust let x: Result<_, &str> = Ok("foo"); let y: Result<&str, _> = Err("bar"); assert_eq!(x.map_or_default(|x| x.len()), 3); assert_eq!(y.map_or_default(|y| y.len()), 0); ``` -------------------------------- ### Collect Result from Iterator Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Demonstrates collecting an iterator of Results into a Result of a collection, stopping at the first Err. ```rust let v = vec![1, 2]; let res: Result, &'static str> = v.iter().map(|x: &u32| x.checked_add(1).ok_or("Overflow!") ).collect(); assert_eq!(res, Ok(vec![2, 3])); ``` ```rust let v = vec![1, 2, 0]; let res: Result, &'static str> = v.iter().map(|x: &u32| x.checked_sub(1).ok_or("Underflow!") ).collect(); assert_eq!(res, Err("Underflow!")); ``` ```rust let v = vec![3, 2, 1, 10]; let mut shared = 0; let res: Result, &'static str> = v.iter().map(|x: &u32| { shared += x; x.checked_sub(2).ok_or("Underflow!") }).collect(); assert_eq!(res, Err("Underflow!")); assert_eq!(shared, 6); ``` -------------------------------- ### Unwrap with default value using unwrap_or Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Ok value or the provided default value if the result is Err. ```rust let default = 2; let x: Result = Ok(9); assert_eq!(x.unwrap_or(default), 9); let x: Result = Err("error"); assert_eq!(x.unwrap_or(default), default); ``` -------------------------------- ### pub fn unwrap_err(self) -> E Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Err value, consuming the self value. Panics if the value is an Ok. ```APIDOC ## pub fn unwrap_err(self) -> E ### Description Returns the contained `Err` value, consuming the `self` value. ### Returns - **E** - The contained error value. ### Panics Panics if the value is an `Ok`, with a custom panic message provided by the `Ok`’s value. ``` -------------------------------- ### DmgWiz Struct Definition Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.DmgWiz.html The main structure representing a DMG image, containing partition information and verbosity settings. ```rust pub struct DmgWiz { pub partitions: Vec, pub verbosity: Verbosity, /* private fields */ } ``` -------------------------------- ### EncryptedDmgReader::from_reader Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.EncryptedDmgReader.html Creates a new EncryptedDmgReader instance from a seekable byte stream using a provided password. ```APIDOC ## pub fn from_reader(reader: R, password: &str, verbosity: Verbosity) -> Result> ### Description Creates a `EncryptedDmgReader` instance from a seekable byte stream. This method initializes the reader with the necessary decryption parameters. ### Parameters - **reader** (R: Read + Seek) - The input stream to read from. - **password** (&str) - The password used for decryption. - **verbosity** (Verbosity) - The verbosity level for debugging output. ### Example ```rust use std::fs::File; use dmgwiz::{EncryptedDmgReader, Verbosity}; let input = File::open("tests/input_aes256.dmg").unwrap(); let mut reader = match EncryptedDmgReader::from_reader(input, "test123", Verbosity::None) { Err(err) => panic!(format!("could not read input file - {}", err)), Ok(val) => val, }; ``` ``` -------------------------------- ### Decrypt and Write DMG Data Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.EncryptedDmgReader.html Decrypt the entire contents of the DMG and write the output to a provided writer. ```rust use std::{fs::File, io::BufWriter}; use dmgwiz::{EncryptedDmgReader, Verbosity}; let input = File::open("tests/input_aes256.dmg").unwrap(); let outfile = File::create("tests/tmp_output.dmg").unwrap(); let output = BufWriter::new(outfile); let mut reader = EncryptedDmgReader::from_reader(input, "test123", Verbosity::None).unwrap(); match reader.read_all(output) { Err(err) => panic!(format!("error while decrypting: {}", err)), Ok(bytes) => println!("done. {} bytes written", bytes), } ``` -------------------------------- ### Chain Result with And Then Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Calls a function to produce a new Result if the current result is Ok, otherwise returns the current Err. ```rust fn sq_then_to_string(x: u32) -> Result { x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed") } ``` -------------------------------- ### Into Ok Value Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Extracts the Ok value from a Result that is guaranteed not to contain an error. ```rust fn only_good_news() -> Result { Ok("this is fine".into()) } let s: String = only_good_news().into_ok(); println!("{s}"); ``` -------------------------------- ### Inspecting Result Variants Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Methods to check if a result is Ok or Err, including predicate-based checks. ```rust let x: Result = Ok(-3); assert_eq!(x.is_ok(), true); let x: Result = Err("Some error message"); assert_eq!(x.is_ok(), false); ``` ```rust let x: Result = Ok(2); assert_eq!(x.is_ok_and(|x| x > 1), true); let x: Result = Ok(0); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Err("hey"); assert_eq!(x.is_ok_and(|x| x > 1), false); let x: Result = Ok("ownership".to_string()); assert_eq!(x.as_ref().is_ok_and(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` ```rust let x: Result = Ok(-3); assert_eq!(x.is_err(), false); let x: Result = Err("Some error message"); assert_eq!(x.is_err(), true); ``` -------------------------------- ### Check Result with is_err_and Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Checks if a Result is an Err and matches a specific predicate. ```rust use std::io::{Error, ErrorKind}; let x: Result = Err(Error::new(ErrorKind::NotFound, "!")); assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), true); let x: Result = Err(Error::new(ErrorKind::PermissionDenied, "!")); assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), false); let x: Result = Ok(123); assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), false); let x: Result = Err("ownership".to_string()); assert_eq!(x.as_ref().is_err_and(|x| x.len() > 1), true); println!("still alive {:?}", x); ``` -------------------------------- ### EncryptedDmgReader::read_all Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.EncryptedDmgReader.html Decrypts all data from the reader and writes it to the specified output writer. ```APIDOC ## pub fn read_all(&mut self, output: W) -> Result where W: Write ### Description Decrypts all data from the encrypted DMG and writes the decrypted content to the provided output writer. ### Parameters - **output** (W: Write) - The writer to which the decrypted data will be written. ### Example ```rust use std::{fs::File, io::BufWriter}; use dmgwiz::{EncryptedDmgReader, Verbosity}; let input = File::open("tests/input_aes256.dmg").unwrap(); let outfile = File::create("tests/tmp_output.dmg").unwrap(); let output = BufWriter::new(outfile); let mut reader = EncryptedDmgReader::from_reader(input, "test123", Verbosity::None).unwrap(); match reader.read_all(output) { Err(err) => panic!(format!("error while decrypting: {}", err)), Ok(bytes) => println!("done. {} bytes written", bytes), } ``` ``` -------------------------------- ### Extract Specific Partition Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.DmgWiz.html Decompresses a single partition identified by its index and writes it to the provided output stream. ```rust use std::{fs::File, io::BufWriter}; use dmgwiz::{DmgWiz, Verbosity}; let input = File::open("tests/input_zlib.dmg").unwrap(); let outfile = File::create("tests/tmp_output_zlib.bin").unwrap(); let output = BufWriter::new(outfile); let mut wiz = DmgWiz::from_reader(input, Verbosity::None).unwrap(); match wiz.extract_partition(output, 0) { Err(err) => panic!(format!("error while extracting: {}", err)), Ok(bytes) => println!("done. {} bytes written", bytes), } ``` -------------------------------- ### Unwrap Error from Result Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the Err value or panics if the result is Ok. ```rust let x: Result = Ok(2); x.unwrap_err(); // panics with `2` ``` ```rust let x: Result = Err("emergency failure"); assert_eq!(x.unwrap_err(), "emergency failure"); ``` -------------------------------- ### Unwrap Result Value Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Extracts the Ok value or panics if the result is Err. Use with caution as it may abort the program. ```rust let x: Result = Ok(2); assert_eq!(x.unwrap(), 2); ``` ```rust let x: Result = Err("emergency failure"); x.unwrap(); // panics with `emergency failure` ``` -------------------------------- ### pub fn expect_err(self, msg: &str) -> E Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Err value, consuming the self value. Panics if the value is an Ok. ```APIDOC ## pub fn expect_err(self, msg: &str) -> E ### Description Returns the contained `Err` value, consuming the `self` value. ### Parameters - **msg** (&str) - The message to include in the panic if the result is Ok. ### Returns - **E** - The contained error value. ### Panics Panics if the value is an `Ok`, with a panic message including the passed message. ``` -------------------------------- ### Transposing Result and Option Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Converts a Result of an Option into an Option of a Result. ```rust #[derive(Debug, Eq, PartialEq)] struct SomeErr; let x: Result, SomeErr> = Ok(Some(5)); let y: Option> = Some(Ok(5)); assert_eq!(x.transpose(), y); ``` -------------------------------- ### DmgWiz::extract_partition Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/struct.DmgWiz.html Decompresses a specific partition identified by its index and writes the output to a provided writer. ```APIDOC ## pub fn extract_partition(&mut self, output: W, partition_num: usize) -> Result ### Description Decompress a specific partition and write to output. Partition information can be retrieved using the `partitions` attribute. ### Parameters - **output** (W: Write) - Required - A writer. - **partition_num** (usize) - Required - Index of the partition. ### Example ```rust use std::{fs::File, io::BufWriter}; use dmgwiz::{DmgWiz, Verbosity}; let input = File::open("tests/input_zlib.dmg").unwrap(); let outfile = File::create("tests/tmp_output_zlib.bin").unwrap(); let output = BufWriter::new(outfile); let mut wiz = DmgWiz::from_reader(input, Verbosity::None).unwrap(); match wiz.extract_partition(output, 0) { Err(err) => panic!(format!("error while extracting: {}", err)), Ok(bytes) => println!("done. {} bytes written", bytes), } ``` ``` -------------------------------- ### Calculate Sum of Result Iterator Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Demonstrates summing elements while returning an Err if any element fails a validation check. ```rust let f = |&x: &i32| if x < 0 { Err("Negative element found") } else { Ok(x) }; let v = vec![1, 2]; let res: Result = v.iter().map(f).sum(); assert_eq!(res, Ok(3)); let v = vec![1, -2]; let res: Result = v.iter().map(f).sum(); assert_eq!(res, Err("Negative element found")); ``` -------------------------------- ### Expect Error from Result Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the Err value or panics with a custom message if the result is Ok. ```rust let x: Result = Ok(10); x.expect_err("Testing expect_err"); // panics with `Testing expect_err: 10` ``` -------------------------------- ### Chain fallible operations with and_then Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Use and_then to chain operations that return a Result. The closure is only executed if the previous result is Ok. ```rust assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4.to_string())); assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed")); assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number")); ``` ```rust use std::{io::ErrorKind, path::Path}; // Note: on Windows "/" maps to "C:\" let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified()); assert!(root_modified_time.is_ok()); let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified()); assert!(should_fail.is_err()); assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound); ``` -------------------------------- ### Iterate Result Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Creates an iterator that yields the contained value if the result is Ok, or nothing if it is Err. ```rust let x: Result = Ok(7); assert_eq!(x.iter().next(), Some(&7)); let x: Result = Err("nothing!"); assert_eq!(x.iter().next(), None); ``` -------------------------------- ### pub unsafe fn unwrap_unchecked(self) -> T Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Ok value, consuming the self value, without checking that the value is not an Err. Calling this method on an Err is undefined behavior. ```APIDOC ### pub unsafe fn unwrap_unchecked(self) -> T Returns the contained `Ok` value, consuming the `self` value, without checking that the value is not an `Err`. #### Safety Calling this method on an `Err` is _undefined behavior_. ``` -------------------------------- ### Map Result values Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Applies functions to the contained value of a Result. ```rust let line = "1\n2\n3\n4\n"; for num in line.lines() { match num.parse::().map(|i| i * 2) { Ok(n) => println!("{n}"), Err(..) => {} } } ``` -------------------------------- ### Into Error Value Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Extracts the Err value from a Result that is guaranteed not to contain an Ok value. ```rust fn only_bad_news() -> Result { Err("Oops, it failed".into()) } let error: String = only_bad_news().into_err(); println!("{error}"); ``` -------------------------------- ### pub unsafe fn unwrap_err_unchecked(self) -> E Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Returns the contained Err value, consuming the self value, without checking that the value is not an Ok. Calling this method on an Ok is undefined behavior. ```APIDOC ### pub unsafe fn unwrap_err_unchecked(self) -> E Returns the contained `Err` value, consuming the `self` value, without checking that the value is not an `Ok`. #### Safety Calling this method on an `Ok` is _undefined behavior_. ``` -------------------------------- ### Result Type Definition Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html The basic definition of the Result type alias and its underlying enum structure. ```rust pub type Result = Result; ``` ```rust pub enum Result { Ok(T), Err(Error), } ``` -------------------------------- ### Convert Result to Iterator Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Converts a Result into an iterator that yields the contained value if it is Ok. ```rust let x: Result = Ok(5); let v: Vec = x.into_iter().collect(); assert_eq!(v, [5]); ``` -------------------------------- ### Define Verbosity Enum Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/enum.Verbosity.html The core definition of the Verbosity enum used to control output detail. ```rust pub enum Verbosity { None, Info, Debug, } ``` -------------------------------- ### Fallback to closure result with or_else Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Calls the provided closure if the result is Err, allowing for lazy evaluation of fallback logic. ```rust fn sq(x: u32) -> Result { Ok(x * x) } fn err(x: u32) -> Result { Err(x) } assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2)); assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2)); assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9)); assert_eq!(Err(3).or_else(err).or_else(err), Err(3)); ``` -------------------------------- ### Define Error Enum Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/enum.Error.html The Error enum structure used to categorize different failure types within the dmgwiz crate. ```rust pub enum Error { Io(Error), Parse(Box), Encrypted, InvalidInput(String), InvalidPartition(usize), Decompress { partition_num: usize, chunk_num: usize, chunk_type: ChunkType, }, UnsupportedEncryption(String), InvalidPassword, Decryption(ErrorStack), } ``` -------------------------------- ### Flattening Nested Results Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Reduces one level of nesting in a Result of a Result. ```rust let x: Result, u32> = Ok(Ok("hello")); assert_eq!(Ok("hello"), x.flatten()); let x: Result, u32> = Ok(Err(6)); assert_eq!(Err(6), x.flatten()); let x: Result, u32> = Err(6); assert_eq!(Err(6), x.flatten()); ``` ```rust let x: Result, u32>, u32> = Ok(Ok(Ok("hello"))); assert_eq!(Ok(Ok("hello")), x.flatten()); assert_eq!(Ok("hello"), x.flatten().flatten()); ``` -------------------------------- ### Inspect Err Variant Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Executes a closure with a reference to the Err value without modifying the result. ```rust use std::{fs, io}; fn read() -> io::Result { fs::read_to_string("address.txt") .inspect_err(|e| eprintln!("failed to read file: {e}")) } ``` -------------------------------- ### Iterate Mut Result Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Creates a mutable iterator that yields a mutable reference to the contained value if the result is Ok. ```rust let mut x: Result = Ok(7); match x.iter_mut().next() { Some(v) => *v = 40, None => {}, } assert_eq!(x, Ok(40)); let mut x: Result = Err("nothing!"); assert_eq!(x.iter_mut().next(), None); ``` -------------------------------- ### Unwrap Err Unchecked Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Unwraps an Err variant without checking for Ok, which is undefined behavior if called on an Ok variant. ```rust let x: Result = Ok(2); unsafe { x.unwrap_err_unchecked() }; // Undefined behavior! ``` ```rust let x: Result = Err("emergency failure"); assert_eq!(unsafe { x.unwrap_err_unchecked() }, "emergency failure"); ``` -------------------------------- ### Define ChunkType Enum Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/enum.ChunkType.html The ChunkType enum uses a u32 representation to map specific integer values to compression types. ```rust #[repr(u32)] pub enum ChunkType { Zero = 0, Raw = 1, Ignore = 2, Comment = 2_147_483_646, ADC = 2_147_483_652, ZLIB = 2_147_483_653, BZLIB = 2_147_483_654, LZFSE = 2_147_483_655, Term = 4_294_967_295, } ``` -------------------------------- ### Deref Result Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Converts a Result containing a dereferenceable type into a Result containing a reference to the target type. ```rust let x: Result = Ok("hello".to_string()); let y: Result<&str, &u32> = Ok("hello"); assert_eq!(x.as_deref(), y); let x: Result = Err(42); let y: Result<&str, &u32> = Err(&42); assert_eq!(x.as_deref(), y); ``` -------------------------------- ### Map Error Variant Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Transforms the error variant of a Result while keeping the Ok variant unchanged. ```rust fn stringify(x: u32) -> String { format!("error code: {x}") } let x: Result = Ok(2); assert_eq!(x.map_err(stringify), Ok(2)); let x: Result = Err(13); assert_eq!(x.map_err(stringify), Err("error code: 13".to_string())); ``` -------------------------------- ### Unwrap Result Unchecked Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Unwraps a Result without checking for Err, which is undefined behavior if called on an Err variant. ```rust let x: Result = Ok(2); assert_eq!(unsafe { x.unwrap_unchecked() }, 2); ``` ```rust let x: Result = Err("emergency failure"); unsafe { x.unwrap_unchecked() }; // Undefined behavior! ``` -------------------------------- ### Deref Mut Result Source: https://docs.rs/dmgwiz/1.1.0/dmgwiz/type.Result.html Converts a Result containing a mutable dereferenceable type into a Result containing a mutable reference to the target type. ```rust let mut s = "HELLO".to_string(); let mut x: Result = Ok("hello".to_string()); let y: Result<&mut str, &mut u32> = Ok(&mut s); assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y); let mut i = 42; let mut x: Result = Err(42); let y: Result<&mut str, &mut u32> = Err(&mut i); assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.