Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remind users of the maximum length of the attribute. #304

Merged
merged 5 commits into from
Feb 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/service_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ impl ServiceInfo {
/// - `Option<HashMap<String, String>>`
/// - slice of tuple: `&[(K, V)]` where `K` and `V` are [`std::string::ToString`].
///
/// Note: The maximum length of a single property string is `255`, Property that exceed the length are truncated.
/// > `len(key + value) < u8::MAX`
///
/// `ip` can be one or more IP addresses, in a type that implements
/// [`AsIpAddrs`] trait. It supports:
///
Expand Down Expand Up @@ -691,6 +694,11 @@ fn encode_txt<'a>(properties: impl Iterator<Item = &'a TxtProperty>) -> Vec<u8>
s.extend(v);
}

// Property that exceed the length are truncated
if s.len() > u8::MAX as usize {
s.resize(u8::MAX as usize, 0);
}

// TXT uses (Length,Value) format for each property,
// i.e. the first byte is the length.
bytes.push(s.len().try_into().unwrap());
Expand Down Expand Up @@ -1127,6 +1135,21 @@ mod tests {
assert_eq!(encoded.len(), "key6".len() + property_count);
let decoded = decode_txt(&encoded);
assert_eq!(properties, decoded);

// test very long property.
let properties = vec![TxtProperty::from(
String::from_utf8(vec![0x30; 1024]).unwrap().as_str(), // A long string of 0 char
)];
let property_count = properties.len();
let encoded = encode_txt(properties.iter());
assert_eq!(encoded.len(), 255 + property_count);
let decoded = decode_txt(&encoded);
assert_eq!(
vec![TxtProperty::from(
String::from_utf8(vec![0x30; 255]).unwrap().as_str()
)],
decoded
);
}

#[test]
Expand Down