Skip to content

Commit

Permalink
Simplify return type of Data::print_string
Browse files Browse the repository at this point in the history
Change the return type from `Option<String>` to `String` because
libyang guarantees that a valid string is always returned on success.

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
  • Loading branch information
rwestphal committed Aug 6, 2024
1 parent 5975282 commit 7a225d5
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub trait Data<'a> {
&self,
format: DataFormat,
options: DataPrinterFlags,
) -> Result<Option<String>> {
) -> Result<String> {
let mut cstr = std::ptr::null_mut();
let cstr_ptr = &mut cstr;

Expand All @@ -322,15 +322,15 @@ pub trait Data<'a> {
return Err(Error::new(self.context()));
}

Ok(char_ptr_to_opt_string(cstr))
Ok(char_ptr_to_string(cstr))
}

/// Print data tree in the specified format to a bytes vector.
fn print_bytes(
&self,
format: DataFormat,
options: DataPrinterFlags,
) -> Result<Option<Vec<u8>>> {
) -> Result<Vec<u8>> {
let mut cstr = std::ptr::null_mut();
let cstr_ptr = &mut cstr;

Expand All @@ -346,10 +346,6 @@ pub trait Data<'a> {
return Err(Error::new(self.context()));
}

if cstr.is_null() {
return Ok(None);
}

let bytes = match format {
DataFormat::XML | DataFormat::JSON => {
// Convert the null-terminated C string to a vector of bytes.
Expand All @@ -369,7 +365,7 @@ pub trait Data<'a> {
.to_vec()
}
};
Ok(Some(bytes))
Ok(bytes)
}
}

Expand Down

0 comments on commit 7a225d5

Please sign in to comment.