Debian Codegraph @

examples/file_read_write_vec.rs

raw symbols SCIP
1 /// Getting RON with type derives and reading/writing a Vec<T> to/from a file.
2 use ronrust-analyzer cargo ron 0.12.0 crate/::{errorrust-analyzer cargo ron 0.12.0 error/::SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#, serrust-analyzer cargo ron 0.12.0 ser/::PrettyConfigrust-analyzer cargo ron 0.12.0 ser/PrettyConfig#, Errorrust-analyzer cargo ron 0.12.0 error/Error#};
3 use serderust-analyzer cargo serde 1.0.228 crate/::{derust-analyzer cargo serde_core 1.0.228 de/::DeserializeOwnedrust-analyzer cargo serde_core 1.0.228 de/DeserializeOwned#, Deserializerust-analyzer cargo serde_core 1.0.228 de/Deserialize#, Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#};
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
5 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#,
6 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#},
7 pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#,
8 strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
9 };
10
11 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:)]
12 struct MetaDatarust-analyzer cargo ron 0.12.0 MetaData# {
13 created_atrust-analyzer cargo ron 0.12.0 MetaData#created_at.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
14 authorrust-analyzer cargo ron 0.12.0 MetaData#author.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
15 }
16
17 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:)]
18 enum UserRolerust-analyzer cargo ron 0.12.0 UserRole# {
19 Userrust-analyzer cargo ron 0.12.0 UserRole#User#,
20 Adminrust-analyzer cargo ron 0.12.0 UserRole#Admin# { keyrust-analyzer cargo ron 0.12.0 UserRole#Admin#key.: usize },
21 }
22
23 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Deserializerust-analyzer cargo serde_derive 1.0.228 Deserialize:, Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:)]
24 struct Userrust-analyzer cargo ron 0.12.0 User# {
25 namerust-analyzer cargo ron 0.12.0 User#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
26 emailrust-analyzer cargo ron 0.12.0 User#email.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
27 commentrust-analyzer cargo ron 0.12.0 User#comment.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
28 rolerust-analyzer cargo ron 0.12.0 User#role.: UserRolerust-analyzer cargo ron 0.12.0 UserRole#,
29 metarust-analyzer cargo ron 0.12.0 User#meta.: MetaDatarust-analyzer cargo ron 0.12.0 MetaData#,
30 }
31
32 fn create_recordsrust-analyzer cargo ron 0.12.0 create_records().() -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Userrust-analyzer cargo ron 0.12.0 User#> {
33 vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
34 Userrust-analyzer cargo ron 0.12.0 User# {
35 namerust-analyzer cargo ron 0.12.0 User#name.: "Alice".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
36 emailrust-analyzer cargo ron 0.12.0 User#email.: "[email protected]".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
37 commentrust-analyzer cargo ron 0.12.0 User#comment.: "New\nLine, and \"quotes\"".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
38 rolerust-analyzer cargo ron 0.12.0 User#role.: UserRolerust-analyzer cargo ron 0.12.0 UserRole#::Adminrust-analyzer cargo ron 0.12.0 UserRole#Admin# { keyrust-analyzer cargo ron 0.12.0 UserRole#Admin#key.: 0xDEADFEED },
39 metarust-analyzer cargo ron 0.12.0 User#meta.: MetaDatarust-analyzer cargo ron 0.12.0 MetaData# {
40 created_atrust-analyzer cargo ron 0.12.0 MetaData#created_at.: "2025-06-22".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
41 authorrust-analyzer cargo ron 0.12.0 MetaData#author.: "Admin".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
42 },
43 },
44 Userrust-analyzer cargo ron 0.12.0 User# {
45 namerust-analyzer cargo ron 0.12.0 User#name.: "Bob".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
46 emailrust-analyzer cargo ron 0.12.0 User#email.: "[email protected]".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
47 commentrust-analyzer cargo ron 0.12.0 User#comment.: "Tabs\ttoo".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
48 rolerust-analyzer cargo ron 0.12.0 User#role.: UserRolerust-analyzer cargo ron 0.12.0 UserRole#::Userrust-analyzer cargo ron 0.12.0 UserRole#User#,
49 metarust-analyzer cargo ron 0.12.0 User#meta.: MetaDatarust-analyzer cargo ron 0.12.0 MetaData# {
50 created_atrust-analyzer cargo ron 0.12.0 MetaData#created_at.: "2025-06-22".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
51 authorrust-analyzer cargo ron 0.12.0 MetaData#author.: "Admin".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
52 },
53 },
54 ]
55 }
56
57 /// Serializes a list of T into a string with one record per line
58 fn write_ron_vec_to_strrust-analyzer cargo ron 0.12.0 write_ron_vec_to_str().<T: Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#>(records: &[T]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Errorrust-analyzer cargo ron 0.12.0 error/Error#> {
59 let mut mut_str = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
60
61 let as_strings = {
62 records
63 .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`&'a [T]`][IntoIterator]into_iter().()
64 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|record| {
65 ronrust-analyzer cargo ron 0.12.0 crate/::serrust-analyzer cargo ron 0.12.0 ser/::to_string_prettyrust-analyzer cargo ron 0.12.0 ser/to_string_pretty().(
66 &record,
67 PrettyConfigrust-analyzer cargo ron 0.12.0 ser/PrettyConfig#::newrust-analyzer cargo ron 0.12.0 ser/impl#[PrettyConfig]new().()
68 .compact_arraysrust-analyzer cargo ron 0.12.0 ser/impl#[PrettyConfig]compact_arrays().(true)
69 .compact_mapsrust-analyzer cargo ron 0.12.0 ser/impl#[PrettyConfig]compact_maps().(true)
70 .compact_structsrust-analyzer cargo ron 0.12.0 ser/impl#[PrettyConfig]compact_structs().(true)
71 .escape_stringsrust-analyzer cargo ron 0.12.0 ser/impl#[PrettyConfig]escape_strings().(true),
72 )
73 })
74 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>, _>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
75 };
76
77 as_strings.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|s| {
78 mut_str.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&s);
79 mut_str.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(not(target_os = "windows")) {
80 "\n"
81 } else {
82 "\r\n"
83 })
84 });
85
86 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mut_str)
87 }
88
89 /// Serializes a list of T into a text file with one record per line
90 fn write_ron_vec_to_filerust-analyzer cargo ron 0.12.0 write_ron_vec_to_file().<T: Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#>(path: &PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, records: &[T]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<usize, Errorrust-analyzer cargo ron 0.12.0 error/Error#> {
91 let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| Errorrust-analyzer cargo ron 0.12.0 error/Error#::Iorust-analyzer cargo ron 0.12.0 error/Error#Io#(e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
92
93 file.writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Write]write().(write_ron_vec_to_strrust-analyzer cargo ron 0.12.0 write_ron_vec_to_str().(records)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..as_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_bytes().())
94 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| Errorrust-analyzer cargo ron 0.12.0 error/Error#::Iorust-analyzer cargo ron 0.12.0 error/Error#Io#(err.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))
95 }
96
97 /// This reader assumes that every row has one entry, so it would not work if they are split across lines.
98 fn read_ron_vec_from_strrust-analyzer cargo ron 0.12.0 read_ron_vec_from_str().<T: DeserializeOwnedrust-analyzer cargo serde_core 1.0.228 de/DeserializeOwned#>(s: &str) -> SpannedResultrust-analyzer cargo ron 0.12.0 error/SpannedResult#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>> {
99 s //_
100 .linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().()
101 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| ronrust-analyzer cargo ron 0.12.0 crate/::from_strrust-analyzer cargo ron 0.12.0 de/from_str().::<T>(s))
102 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>, _>>()
103 }
104
105 fn read_ron_vec_from_filerust-analyzer cargo ron 0.12.0 read_ron_vec_from_file().<T: DeserializeOwnedrust-analyzer cargo serde_core 1.0.228 de/DeserializeOwned#>(path: &PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, Errorrust-analyzer cargo ron 0.12.0 error/Error#> {
106 let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| Errorrust-analyzer cargo ron 0.12.0 error/Error#::Iorust-analyzer cargo ron 0.12.0 error/Error#Io#(e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
107
108 let mut content = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
109
110 file.read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_string().(&mut content)
111 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| Errorrust-analyzer cargo ron 0.12.0 error/Error#::Iorust-analyzer cargo ron 0.12.0 error/Error#Io#(e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
112
113 read_ron_vec_from_strrust-analyzer cargo ron 0.12.0 read_ron_vec_from_str().(&content).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| e.coderust-analyzer cargo ron 0.12.0 error/SpannedError#code.)
114 }
115
116 pub fn mainrust-analyzer cargo ron 0.12.0 main().() {
117 let users = create_recordsrust-analyzer cargo ron 0.12.0 create_records().();
118
119 let path = PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::from_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][FromStr]from_str().("vec-example.ron").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
120
121 write_ron_vec_to_filerust-analyzer cargo ron 0.12.0 write_ron_vec_to_file().(&path, &users).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
122
123 let read_users: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Userrust-analyzer cargo ron 0.12.0 User#> = read_ron_vec_from_filerust-analyzer cargo ron 0.12.0 read_ron_vec_from_file().(&path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
124
125 // Comment this out if you want to view the file:
126 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_file().("vec-example.ron").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
127
128 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{:?}", read_users);
129 }