generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Diane Huxley
committed
May 22, 2024
1 parent
cde7b93
commit 2eb36cc
Showing
5 changed files
with
170 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use simple_dns::SimpleDnsError; | ||
|
||
mod rdata_encoder; | ||
pub mod service; | ||
|
||
const DEFAULT_TTL: u32 = 7200; // seconds | ||
|
75 changes: 75 additions & 0 deletions
75
crates/dids/src/method/dht/document_packet/rdata_encoder.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::collections::HashMap; | ||
|
||
use simple_dns::{rdata::RData, ResourceRecord}; | ||
use ssi_core::one_or_many::OneOrMany; | ||
|
||
use super::DocumentPacketError; | ||
|
||
/// Gets the RData from the record. If RData is RData::TXT, get the text as a string. | ||
/// Convert strings like "id=foo;t=bar;se=baz" into a hash map like { 'id': 'foo', 't': 'bar', 'se': 'baz' } | ||
/// If there is any issue, return DocumentPacketError::RDataError | ||
pub fn record_rdata_to_hash_map( | ||
record: ResourceRecord, | ||
) -> Result<HashMap<String, String>, DocumentPacketError> { | ||
// Get RData text as String | ||
let rdata_txt = match record.rdata { | ||
RData::TXT(txt) => txt, | ||
_ => { | ||
return Err(DocumentPacketError::RDataError( | ||
"RData must have type TXT".to_owned(), | ||
)) | ||
} | ||
}; | ||
let text = match String::try_from(rdata_txt) { | ||
Ok(text) => text, | ||
Err(_) => { | ||
return Err(DocumentPacketError::RDataError( | ||
"Failed to convert to string".to_owned(), | ||
)) | ||
} | ||
}; | ||
|
||
// Parse key-value pairs: | ||
// Split string by ";" to get entries | ||
// Split each entry by "=" to get key and value | ||
let mut attributes = HashMap::new(); | ||
for entry in text.split(';') { | ||
let k_v: Vec<&str> = entry.split('=').collect(); | ||
if k_v.len() != 2 { | ||
return Err(DocumentPacketError::RDataError( | ||
"Could not get values from RData text".to_owned(), | ||
)); | ||
} | ||
|
||
let k = k_v[0].trim().to_string(); | ||
let v = k_v[1].trim().to_string(); | ||
|
||
attributes.insert(k, v); | ||
} | ||
|
||
Ok(attributes) | ||
} | ||
|
||
pub fn to_one_or_many(value: String) -> OneOrMany<String> { | ||
let split: Vec<String> = value.split(',').map(|s| s.to_string()).collect(); | ||
if split.len() == 1 { | ||
OneOrMany::One(value) | ||
} else { | ||
OneOrMany::Many(split) | ||
} | ||
} | ||
|
||
/// Get value from the RData HashMap created by record_rdata_to_hash_map(). | ||
/// Convert `None` into DocumentPacketError | ||
pub fn get_rdata_txt_value( | ||
rdata_map: &HashMap<String, String>, | ||
key: &str, | ||
) -> Result<String, DocumentPacketError> { | ||
let val = rdata_map | ||
.get(key) | ||
.ok_or(DocumentPacketError::RDataError(format!( | ||
"Could not extract {} from RData", | ||
key | ||
)))?; | ||
Ok(val.to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters