0

I am very new to Rust and trying to get a bit of code working that can push a record to a kinesis firehose stream.

struct AuditRecord{
    user_id : String,
    request : Value,
    request_id : String,
    timestamp_raw : i64,
}
...
let client = KinesisClient::new(Region::UsEast1);
let record = PutRecordInput{
    partition_key : requestId.to_string(),
    stream_name : streamName,
    data : auditRecord
};

When I tried this it wants the data in bytes::bytes::Bytes but I am unclear as to how to convert my struct to a bytes::bytes::Bytes representation. Any examples of how to go about this would be appreciated.

For clarification I am using

rusoto = "0.24.2"
rusoto_kinesis = "0.43.0"
rusoto_core = "0.43.0"

Also if anyone know of a good place to see real examples of how to use rusoto to talk with varios AWS entities that would be appreciated.

fedonev
  • 20,327
  • 2
  • 25
  • 34
NSA
  • 5,689
  • 8
  • 37
  • 48
  • It looks like your question might be answered by the answers of [How to convert 'struct' to '&\[u8\]'?](https://stackoverflow.com/q/28127165/155423); [Streamed upload to s3 with rusoto](https://stackoverflow.com/q/57810173/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster May 12 '20 at 20:35
  • By the way, idiomatic Rust uses `snake_case` for variables, methods, macros, fields and modules; `UpperCamelCase` for types and enum variants; and `SCREAMING_SNAKE_CASE` for statics and constants. – Shepmaster May 12 '20 at 20:36
  • @Shepmaster one big difference between what you suggest and what I am asking is how to convert my struct. I have an object and not a file or string, for more experienced rust people that may not be much of a difference but for those of us who are rust inexperienced its not clear how the one applies to the other. – NSA May 12 '20 at 20:41
  • No one can tell you how to convert your struct into bytes — that's only something **you** can do. One solution is XML, one is JSON, one is Protocol Buffers, one is Flatbuffers, one is Capn Proto, one is CSV, one is TSV, etc. This has nothing to do with Rust; it's a general programming question. – Shepmaster May 12 '20 at 20:45
  • And the question I linked has an answer that shows one specific way to convert an arbitrary struct into bytes; thus it's an answer. – Shepmaster May 12 '20 at 20:46
  • @Shepmaster thanks for your input, but one of the things you edited out of my question was a notation that I am new to rust which helps people have context for their responses. Second I don't really care what is the idiomatic style for rust, I use what I like sorry if that offends you I think you should move on to a different question. Finally converting my struct into bytes is not the same as XML vs JSON vs CSV, I am asking if there is a straightforward way to achieve this with rust/rusoto, which there likely is. Especially since the method I am trying to call takes that as a type. Thank you – NSA May 12 '20 at 20:56
  • 1
    Stating you are a beginner doesn’t change how Stack Overflow works, and the content of the question already conveys that you are a beginner in this context. It also wouldn’t matter if you were an expert if you wanted to ask the same question. Unfortunately, your question in the original (and current) form is off topic for Stack Overflow. – Shepmaster May 12 '20 at 21:10

1 Answers1

0

As other people have stated, It really depends on your choice of serialization. If you chose a string based encoding like xml or json, then you could use something like:

let msg: String = "some message".to_string();

let record = PutRecordInput{
    partition_key : requestId.to_string(),
    stream_name : streamName,
    data : msg.into()
};

// or it could be 
let record = PutRecordInput{
    partition_key : requestId.to_string(),
    stream_name : streamName,
    data : Bytes::from(msg)
};

Where you replace "some message" with your string sterilization of choice.

Though technically, using some binary encoding like protobuf or something are usually a lot more compact, but then whatever is on the other side must be able to interpret the format.