liranringel / structure

Use format strings to create strongly-typed data pack/unpack interfaces
Apache License 2.0
62 stars 4 forks source link

help #4

Closed yxdxx closed 7 years ago

yxdxx commented 7 years ago

I was writing a program send telegram with TCPSTREAM . send an XML file with an head of the length of the XML characters.
let s = structure!(">4S716s"); let buffsend:Vec =s.pack(lengthstring.as_bytes(),contentbuff).unwrap(); turns out an error: buffer size in format: 3, actual size: 4", I wanna to padding the left with 0000, how can I do with it .seems > does't work . if I use s, no error ,but the telegram seems still not correct in the head. also how can I replace with 716. the length of the XML . everytime the length will change.

liranringel commented 7 years ago

Hey! I glad to see that people use this crate, though I doubt it's the right solution for sending xml. structure is really good for writing fixed-size buffers, but you want a variable size buffer.

If you still think it could work for you, it could be useful if you upload your hole program or describe better what's the desired output.

Using 's' is the right thing to do if you want to pad your buffer with zeros, but it obviously won't play well with xml, which is a text format.

yxdxx commented 7 years ago

thank you for your reply... I have write a python code with module struct . it works. if possible, you can give a version of rust code. that will be nice of you.. the xml1 does't seem to display.. I put the attachment as xml content. CreateVMDT_066_01.txt

xml1 = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>

""" import struct import socket buffer = struct.pack('!i' + str(len(xml1)) + 's', len(xml1) + 4, xml1.encode("utf8")) Host = "10.174.xxx.xxx" Port = 55065 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((Host, Port)) s.sendall(buffer) buf = s.recv(1024) s.close() result = buf[4:].decode("utf8") #print(result) if 'returnCode="0"' in result: print("modified success") else: print("check your telegram")
liranringel commented 7 years ago

You create a format string at runtime, but it's not supported by this crate (and probably will never be). Rust is a static typed language. Therefore it's not possible to process the format string at runtime without sacrifying the type safety you get (pack gets the right types as parameters, and unpack returns the right types). Processing the format string at runtime is a feature that should be considered, but I've yet to think of a good way to do it. If the interface won't be easy, it will miss the purpose of this crate.

Since you're trying to parse xml, I'd really want you to try serde: https://github.com/RReverser/serde-xml-rs

yxdxx commented 7 years ago

thank you very much. you are really nice person. I will try to understand the language more deep..^_^,also try to use serde.

liranringel commented 7 years ago

Good luck :)