|
| 1 | +serde-pod (plain old data) |
| 2 | +========================== |
| 3 | +Реализует простую сериализацию и десериализацию структур, наиболее близкую к их представлению в памяти. |
| 4 | + |
| 5 | +# Пример |
| 6 | +Читаем заголовок GFF файла (формат Bioware, используемый для хранения данных в |
| 7 | +таких играх, как Neverwinter Nights, Neverwinter Nights 2 и Ведьмак): |
| 8 | + |
| 9 | +```rust |
| 10 | +extern crate byteorder; |
| 11 | +#[macro_use] |
| 12 | +extern crate serde_derive; |
| 13 | +extern crate serde_pod; |
| 14 | +use serde_pod::{from_bytes, Result}; |
| 15 | + |
| 16 | +#[derive(Debug, Deserialize, PartialEq)] |
| 17 | +struct Signature([u8; 4]); |
| 18 | + |
| 19 | +#[derive(Debug, Deserialize, PartialEq)] |
| 20 | +struct Version([u8; 4]); |
| 21 | + |
| 22 | +#[derive(Debug, Deserialize, PartialEq)] |
| 23 | +struct Section { |
| 24 | + offset: u32, |
| 25 | + count: u32, |
| 26 | +} |
| 27 | +#[derive(Debug, Deserialize, PartialEq)] |
| 28 | +struct GffHeader { |
| 29 | + signature: Signature, |
| 30 | + version: Version, |
| 31 | + structs: Section, |
| 32 | + fields: Section, |
| 33 | + labels: Section, |
| 34 | + field_data: Section, |
| 35 | + field_indices: Section, |
| 36 | + list_indices: Section, |
| 37 | +} |
| 38 | + |
| 39 | +fn main() { |
| 40 | + let header: GffHeader = from_bytes::<byteorder::LE, _>(&[ |
| 41 | + // Signature |
| 42 | + 0x47, 0x55, 0x49, 0x20, |
| 43 | + // Version |
| 44 | + 0x56, 0x33, 0x2E, 0x32, |
| 45 | + // structs |
| 46 | + 0x38, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, |
| 47 | + // fields |
| 48 | + 0xEC, 0x00, 0x00, 0x00, 0x93, 0x00, 0x00, 0x00, |
| 49 | + // labels |
| 50 | + 0xD0, 0x07, 0x00, 0x00, 0x1A, 0x00, 0x00, 0x00, |
| 51 | + // field_data |
| 52 | + 0x70, 0x09, 0x00, 0x00, 0x1D, 0x02, 0x00, 0x00, |
| 53 | + // field_indices |
| 54 | + 0x8D, 0x0B, 0x00, 0x00, 0x4C, 0x02, 0x00, 0x00, |
| 55 | + // list_indices |
| 56 | + 0xD9, 0x0D, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, |
| 57 | + ])?; |
| 58 | + |
| 59 | + assert_eq!(header, GffHeader { |
| 60 | + signature: Signature(*b"GUI "), |
| 61 | + version: Version(*b"V3.2"), |
| 62 | + structs: Section { offset: 0x38, count: 15 }, |
| 63 | + fields: Section { offset: 0xEC, count: 147 }, |
| 64 | + labels: Section { offset: 0x07D0, count: 26 }, |
| 65 | + field_data: Section { offset: 0x0970, count: 541 }, |
| 66 | + field_indices: Section { offset: 0x0B8D, count: 588 }, |
| 67 | + list_indices: Section { offset: 0x0DD9, count: 36 }, |
| 68 | + }); |
| 69 | +} |
| 70 | +``` |
0 commit comments