Skip to content

Commit 4931eb3

Browse files
committed
Move proto::message::Unparsed to own module
Signed-off-by: Ross Williams <ross@ross-williams.net>
1 parent c50efaf commit 4931eb3

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

src/proto/message.rs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
//! Agent protocol message structures.
22
3+
pub mod unparsed;
4+
35
use core::str::FromStr;
46

57
use ssh_encoding::{CheckedSum, Decode, Encode, Error as EncodingError, Reader, Writer};
68
use ssh_key::{
79
certificate::Certificate, private::KeypairData, public::KeyData, Algorithm, Error, Signature,
810
};
911

12+
pub use self::unparsed::*;
1013
use super::{
1114
extension::{KeyConstraintExtension, MessageExtension},
1215
PrivateKeyData, ProtoError,
@@ -546,7 +549,7 @@ impl Extension {
546549
extension.encode(&mut buffer)?;
547550
Ok(Self {
548551
name: T::NAME.into(),
549-
details: Unparsed(buffer),
552+
details: buffer.into(),
550553
})
551554
}
552555

@@ -578,7 +581,7 @@ impl Extension {
578581
extension.encode(&mut buffer)?;
579582
Ok(Self {
580583
name: T::NAME.into(),
581-
details: Unparsed(buffer),
584+
details: buffer.into(),
582585
})
583586
}
584587

@@ -627,42 +630,6 @@ impl Encode for Extension {
627630
}
628631
}
629632

630-
/// Generic container for [`Extension`]-specific content
631-
#[derive(Debug, PartialEq, Clone)]
632-
pub struct Unparsed(pub Vec<u8>);
633-
634-
impl Unparsed {
635-
/// Decode unparsed bytes as SSH structures.
636-
pub fn parse<T>(&self) -> std::result::Result<T, <T as Decode>::Error>
637-
where
638-
T: Decode,
639-
{
640-
let mut v = &self.0[..];
641-
T::decode(&mut v)
642-
}
643-
}
644-
645-
impl From<Vec<u8>> for Unparsed {
646-
fn from(value: Vec<u8>) -> Self {
647-
Self(value)
648-
}
649-
}
650-
651-
impl Encode for Unparsed {
652-
fn encoded_len(&self) -> ssh_encoding::Result<usize> {
653-
Ok(self.0.len())
654-
}
655-
656-
fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
657-
// NOTE: Unparsed fields do not embed a length u32,
658-
// as the inner Vec<u8> encoding is implementation-defined
659-
// (usually an Extension)
660-
writer.write(&self.0[..])?;
661-
662-
Ok(())
663-
}
664-
}
665-
666633
/// SSH agent protocol request messages.
667634
///
668635
/// These message types are sent from a client *to* an agent.
@@ -1056,7 +1023,7 @@ mod tests {
10561023
},
10571024
constraints: vec![KeyConstraint::Extension(Extension {
10581025
name: "restrict-destination-v00@openssh.com".to_string(),
1059-
details: Unparsed(
1026+
details: Unparsed::from(
10601027
hex!(
10611028
" 00
10621029
0002 6f00 0000 0c00 0000 0000 0000 0000

src/proto/message/unparsed.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//! Generic container for [`Extension`](super::Extension)-specific content
2+
3+
use ssh_encoding::{self, Decode, Encode, Writer};
4+
5+
/// Generic container for [`Extension`](super::Extension)-specific content.
6+
/// Accessing the inner `Vec<u8>` is only possible via conversion methods.
7+
#[derive(Debug, PartialEq, Clone)]
8+
pub struct Unparsed(Vec<u8>);
9+
10+
impl Unparsed {
11+
/// Decode unparsed bytes as SSH structures.
12+
pub fn parse<T>(&self) -> std::result::Result<T, <T as Decode>::Error>
13+
where
14+
T: Decode,
15+
{
16+
let mut v = &self.0[..];
17+
T::decode(&mut v)
18+
}
19+
}
20+
21+
impl From<Vec<u8>> for Unparsed {
22+
fn from(value: Vec<u8>) -> Self {
23+
Self(value)
24+
}
25+
}
26+
27+
impl Encode for Unparsed {
28+
fn encoded_len(&self) -> ssh_encoding::Result<usize> {
29+
Ok(self.0.len())
30+
}
31+
32+
fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
33+
// NOTE: Unparsed fields do not embed a length u32,
34+
// as the inner Vec<u8> encoding is implementation-defined
35+
// (usually an Extension)
36+
writer.write(&self.0[..])?;
37+
38+
Ok(())
39+
}
40+
}

0 commit comments

Comments
 (0)