diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cd9d0d1c..201e8ffd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- CAA record value is bytes, not a string + ## 9.2.1 (26 May 2024) - Include the whole API in docs diff --git a/examples/epoll.rs b/examples/epoll.rs index aa670d2a5..59244ae65 100644 --- a/examples/epoll.rs +++ b/examples/epoll.rs @@ -49,12 +49,7 @@ mod example { Ok(ref caa_results) => { println!("Successful CAA lookup..."); for caa_result in caa_results { - println!( - "critical: {}, property: {}, value: {}", - caa_result.critical(), - caa_result.property().to_string_lossy(), - caa_result.value().to_string_lossy() - ); + println!("{}", caa_result); } } } diff --git a/src/caa.rs b/src/caa.rs index a6c34d0d7..4d98a48eb 100644 --- a/src/caa.rs +++ b/src/caa.rs @@ -1,9 +1,7 @@ use std::ffi::CStr; -use std::fmt; use std::marker::PhantomData; use std::os::raw::{c_int, c_uchar, c_void}; -use std::ptr; -use std::slice; +use std::{fmt, ptr, slice, str}; use itertools::Itertools; @@ -114,12 +112,8 @@ impl<'a> CAAResult<'a> { } /// The value represented by this `CAAResult`. - /// - /// In practice this is very likely to be a valid UTF-8 string, but the underlying `c-ares` - /// library does not guarantee this - so we leave it to users to decide whether they prefer a - /// fallible conversion, a lossy conversion, or something else altogether. - pub fn value(self) -> &'a CStr { - unsafe { CStr::from_ptr(self.caa_reply.value.cast()) } + pub fn value(self) -> &'a [u8] { + unsafe { slice::from_raw_parts(self.caa_reply.value, self.caa_reply.length) } } } @@ -131,11 +125,8 @@ impl<'a> fmt::Display for CAAResult<'a> { "Property: {}, ", self.property().to_str().unwrap_or("") )?; - write!( - fmt, - "Value: {}", - self.value().to_str().unwrap_or("") - ) + let value = str::from_utf8(self.value()).unwrap_or(""); + write!(fmt, "Value: {}", value) } }