Debian Codegraph @

src/writer.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/;
2 use 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/;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/;
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#;
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/;
8
9
10 /// Primary object used to write constant files.
11 ///
12 /// # Example
13 /// ```no_run
14 /// # use std::path::Path;
15 /// # #[derive(Debug)]
16 /// # struct Point { x: u8, y: u8 }
17 /// use build_const::ConstWriter;
18 ///
19 /// // use `for_build` in `build.rs`
20 /// let mut consts = ConstWriter::from_path(
21 /// &Path::new("/tmp/constants.rs")
22 /// ).unwrap();
23 ///
24 /// // add an external dependency (`use xyz::Point`)
25 /// consts.add_dependency("xyz::Point");
26 ///
27 /// // finish dependencies and starting writing constants
28 /// let mut consts = consts.finish_dependencies();
29 ///
30 /// // add an array of values
31 /// let values: Vec<u8> = vec![1, 2, 3, 36];
32 /// consts.add_array("ARRAY", "u8", &values);
33 ///
34 /// // Add a value that is a result of "complex" calculations
35 /// consts.add_value("VALUE", "u8", values.iter().sum::<u8>());
36 ///
37 /// // Add a value from an external crate (must implement `Debug`)
38 /// consts.add_value("VALUE", "Point", &Point { x: 3, y: 7});
39 /// ```
40 pub struct ConstWriterrust-analyzer cargo build_const 0.2.1 writer/ConstWriter# {
41 frust-analyzer cargo build_const 0.2.1 writer/ConstWriter#f.: 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#,
42 }
43
44 /// Created from `ConstWriter::finish_dependencies`. See
45 /// documentation for `ConstWriter`.
46 pub struct ConstValueWriterrust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter# {
47 frust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter#f.: 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#,
48 }
49
50 impl ConstWriterrust-analyzer cargo build_const 0.2.1 writer/ConstWriter# {
51 /// Create a ConstWriter to be used for your crate's `build.rs`
52 pub fn for_buildrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstWriter]for_build().(mod_name: &str) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ConstWriterrust-analyzer cargo build_const 0.2.1 writer/ConstWriter#> {
53 let out_dir = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("OUT_DIR").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
54 let mod_name = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}.rs", mod_name);
55 let dest_path = Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().(&out_dir).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(mod_name);
56
57 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ConstWriterrust-analyzer cargo build_const 0.2.1 writer/ConstWriter# {
58 frust-analyzer cargo build_const 0.2.1 writer/ConstWriter#f.: 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#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(&dest_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
59 })
60 }
61
62 /// Create a new ConstWriter to write to an path. If a file
63 /// already exists at the path then it will be deleted.
64 pub fn from_pathrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstWriter]from_path().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<ConstWriterrust-analyzer cargo build_const 0.2.1 writer/ConstWriter#> {
65 let f = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]new().()
66 .writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]write().(true)
67 .truncaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]truncate().(true)
68 .openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
69 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ConstWriterrust-analyzer cargo build_const 0.2.1 writer/ConstWriter# {
70 frust-analyzer cargo build_const 0.2.1 writer/ConstWriter#f.: f,
71 })
72 }
73
74 /// finish writing dependencies and start writing constants
75 pub fn finish_dependenciesrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstWriter]finish_dependencies().(self) -> ConstValueWriterrust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter# {
76 ConstValueWriterrust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter# { frust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter#f.: self.frust-analyzer cargo build_const 0.2.1 writer/ConstWriter#f. }
77 }
78
79 /// Add a dependency to your constants file.
80 pub fn add_dependencyrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstWriter]add_dependency().(&mut self, lib: &str) {
81 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(self.frust-analyzer cargo build_const 0.2.1 writer/ConstWriter#f., "pub use {};\n", lib).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
82 }
83
84 /// Add a raw string to the constants file.
85 ///
86 /// This method only changes `raw` by adding a `\n` at the end.
87 pub fn add_rawrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstWriter]add_raw().(&mut self, raw: &str) {
88 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(self.frust-analyzer cargo build_const 0.2.1 writer/ConstWriter#f., "{}\n", raw).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
89 }
90
91 }
92
93 impl ConstValueWriterrust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter# {
94 /// Add a value to the constants file.
95 ///
96 /// You have to manually specify the `name`, type (`ty`) and `value`
97 /// of the constant you want to add.
98 ///
99 /// The `value` uses the `Debug` trait to determine the formating of
100 /// the value being added. If `Debug` is not accurate or will not work,
101 /// you must use `add_value_raw` instead and format it yourself.
102 pub fn add_valuerust-analyzer cargo build_const 0.2.1 writer/impl#[ConstValueWriter]add_value().<T: Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#>(&mut self, name: &str, ty: &str, value: T) {
103 self.add_value_rawrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstValueWriter]add_value_raw().(name, ty, &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", value));
104 }
105
106 /// Add a pre-formatted value to the constants file.
107 ///
108 /// `add_value` depends on `Debug` being implemented in such a way
109 /// that it accurately represents the type's creation. Sometimes that
110 /// cannot be relied on and `add_value_raw` has to be used instead.
111 pub fn add_value_rawrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstValueWriter]add_value_raw().(&mut self, name: &str, ty: &str, raw_value: &str) {
112 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
113 self.frust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter#f., "pub const {}: {} = {};\n",
114 name,
115 ty,
116 raw_value,
117 ).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
118 }
119
120 /// Add an array of len > 0 to the constants
121 ///
122 /// You have to manually specify the `name`, type (`ty`) of the **items** and
123 /// `values` of the array constant you want to add. The length of the array
124 /// is determined automatically.
125 ///
126 /// Example: `const.add_array("foo", "u16", &[1,2,3])`
127 ///
128 /// The `value` of each item uses the `Debug` trait to determine the
129 /// formatting of the value being added. If `Debug` is not accurate or will
130 /// not work, you must use `add_array_raw` instead and format it yourself.
131 pub fn add_arrayrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstValueWriter]add_array().<T: Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#>(&mut self, name: &str, ty: &str, values: &[T]) {
132 write_arrayrust-analyzer cargo build_const 0.2.1 writer/write_array().(&mut self.frust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter#f., name, ty, values);
133 }
134
135 /// Add an array of pre-formatted values to the constants file. The length of the array is
136 /// determined automatically.
137 ///
138 /// `add_array` depends on `Debug` being implemented for each item in such a way that it
139 /// accurately represents the item's creation. Sometimes that cannot be relied on and
140 /// `add_array_raw` has to be used instead.
141 pub fn add_array_rawrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstValueWriter]add_array_raw().(&mut self, name: &str, ty: &str, raw_values: &[&str]) {
142 write_array_rawrust-analyzer cargo build_const 0.2.1 writer/write_array_raw().(&mut self.frust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter#f., name, ty, raw_values);
143 }
144
145 /// Add a raw string to the constants file.
146 ///
147 /// This method only changes `raw` by adding a `\n` at the end.
148 pub fn add_rawrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstValueWriter]add_raw().(&mut self, raw: &str) {
149 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(self.frust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter#f., "{}\n", raw).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
150 }
151
152 /// Finish writing to the constants file and consume self.
153 pub fn finishrust-analyzer cargo build_const 0.2.1 writer/impl#[ConstValueWriter]finish().(&mut self) {
154 self.frust-analyzer cargo build_const 0.2.1 writer/ConstValueWriter#f..flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Write]flush().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
155 }
156
157 }
158
159 // Public Functions
160
161 /// Write an array and return the array's full type representation.
162 ///
163 /// This can be used to create nested array constant types.
164 pub fn write_arrayrust-analyzer cargo build_const 0.2.1 writer/write_array().<T: Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(w: &mut W, name: &str, ty: &str, values: &[T])
165 -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#
166 {
167 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
168 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().values.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().(),
169 "attempting to add an array of len zero. If this is intentional, use \
170 add_value_raw instead."
171 );
172 let full_ty = write_array_headerrust-analyzer cargo build_const 0.2.1 writer/write_array_header().(w, name, ty, values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
173 for v in values.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
174 write_array_item_rawrust-analyzer cargo build_const 0.2.1 writer/write_array_item_raw().(w, &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", v));
175 }
176 write_array_endrust-analyzer cargo build_const 0.2.1 writer/write_array_end().(w);
177 full_ty
178 }
179
180 /// Write an array of raw values and return the array's full type representation.
181 ///
182 /// This can be used to create nested array constant types.
183 pub fn write_array_rawrust-analyzer cargo build_const 0.2.1 writer/write_array_raw().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(
184 w: &mut W, name: &str, ty: &str, raw_values: &[&str]
185 )
186 -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#
187 {
188 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
189 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().raw_values.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().(),
190 "attempting to add an array of len zero. If this is intentional, use \
191 add_value_raw instead."
192 );
193 let full_ty = write_array_headerrust-analyzer cargo build_const 0.2.1 writer/write_array_header().(w, name, ty, raw_values.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
194 for &v in raw_values {
195 write_array_item_rawrust-analyzer cargo build_const 0.2.1 writer/write_array_item_raw().(w, v);
196 }
197 write_array_endrust-analyzer cargo build_const 0.2.1 writer/write_array_end().(w);
198 full_ty
199 }
200
201 // Helpers
202
203 /// Write the array header and return the array's full type.
204 fn write_array_headerrust-analyzer cargo build_const 0.2.1 writer/write_array_header().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(w: &mut W, name: &str, ty: &str, len: usize) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
205 let full_ty = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("[{}; {}]", ty, len);
206 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(w, "pub const {}: {} = [\n", name, &full_ty).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
207 full_ty
208 }
209
210 fn write_array_item_rawrust-analyzer cargo build_const 0.2.1 writer/write_array_item_raw().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(w: &mut W, raw_item: &str) {
211 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(w, " {},\n", raw_item).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
212 }
213
214 fn write_array_endrust-analyzer cargo build_const 0.2.1 writer/write_array_end().<W: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#>(w: &mut W) {
215 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(w, "];\n").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
216 }