-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
474af2e
commit 204b897
Showing
7 changed files
with
139 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,69 @@ | ||
use super::codes::*; | ||
use crate::errors::TockloaderError; | ||
use std::str; | ||
|
||
/// TODO: What exactly is an attribute? | ||
#[derive(Debug)] | ||
pub struct Attribute { | ||
pub key: String, | ||
pub value: String, | ||
} | ||
|
||
impl Attribute { | ||
/// Intrepret raw bytes, according to the Tock Format (TODO: Source??) | ||
pub fn parse_raw(bytes: Vec<u8>) -> Attribute { | ||
todo!() | ||
pub fn parse_raw(bytes: Vec<u8>) -> Result<Attribute, TockloaderError> { | ||
// First 2 bytes should be: | ||
// <ESCAPE CHAR> <RESPONSE_GET_ATTRIBUTE> | ||
|
||
if bytes[0] != ESCAPE_CHAR { | ||
return Err(TockloaderError::MalformedResponse(format!( | ||
"Expected attribute to start with ESCAPE_CHAR({}), but got {}", | ||
ESCAPE_CHAR, bytes[0] | ||
))); | ||
} | ||
|
||
if bytes[1] != RESPONSE_GET_ATTRIBUTE { | ||
return Err(TockloaderError::MalformedResponse(format!( | ||
"Expected attribute to have second byte RESPONSE_GET_ATTRIBUTE({}), but got {}", | ||
RESPONSE_GET_ATTRIBUTE, bytes[1] | ||
))); | ||
} | ||
|
||
// The next 8 bytes will be the name of the attribute (key) with | ||
// null bytes as padding | ||
|
||
let key_bytes: Vec<u8> = bytes[2..10] | ||
.iter() | ||
.copied() | ||
.filter(|byte| *byte != 0) | ||
.collect(); | ||
let key = str::from_utf8(&key_bytes) | ||
.map_err(|_| { | ||
TockloaderError::MalformedResponse(format!( | ||
"Failed to parse UTF-8 from key: {:?}", | ||
key_bytes | ||
)) | ||
})? | ||
.to_string(); | ||
|
||
// 9-th byte is the length of the value (without padding). | ||
let len = bytes[10]; | ||
// 10th+ bytes is the value | ||
let val_bytes: Vec<u8> = bytes[11..(11 + len as usize)] | ||
.iter() | ||
.copied() | ||
.filter(|byte| *byte != 0) | ||
.collect(); | ||
|
||
let value = str::from_utf8(&val_bytes) | ||
.map_err(|_| { | ||
TockloaderError::MalformedResponse(format!( | ||
"Failed to parse UTF-8 from value: {:?}", | ||
val_bytes | ||
)) | ||
})? | ||
.to_string(); | ||
|
||
Ok(Attribute { key, value }) | ||
} | ||
} |
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
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
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