Debian Codegraph @

src/render/string.rs

raw symbols SCIP
1 //! String rendering support.
2
3 use craterust-analyzer cargo qrcode 0.14.1 crate/::castrust-analyzer cargo qrcode 0.14.1 cast/::Asrust-analyzer cargo qrcode 0.14.1 cast/As#;
4 use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::{Canvasrust-analyzer cargo qrcode 0.14.1 render/Canvas# as RenderCanvasrust-analyzer cargo qrcode 0.14.1 render/Canvas#, Pixelrust-analyzer cargo qrcode 0.14.1 render/Pixel#};
5 use craterust-analyzer cargo qrcode 0.14.1 crate/::typesrust-analyzer cargo qrcode 0.14.1 types/::Colorrust-analyzer cargo qrcode 0.14.1 types/Color#;
6
7 pub trait Elementrust-analyzer cargo qrcode 0.14.1 render/string/Element#: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# {
8 fn default_colorrust-analyzer cargo qrcode 0.14.1 render/string/Element#default_color().(color: Colorrust-analyzer cargo qrcode 0.14.1 types/Color#) -> Self;
9 fn strlenrust-analyzer cargo qrcode 0.14.1 render/string/Element#strlen().(self) -> usize;
10 fn append_to_stringrust-analyzer cargo qrcode 0.14.1 render/string/Element#append_to_string().(self, string: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#);
11 }
12
13 impl Elementrust-analyzer cargo qrcode 0.14.1 render/string/Element# for char {
14 fn default_colorrust-analyzer cargo qrcode 0.14.1 render/string/impl#[char][Element]default_color().(color: Colorrust-analyzer cargo qrcode 0.14.1 types/Color#) -> Selfrust-analyzer cargo qrcode 0.14.1 render/string/impl#[char][Element] {
15 color.selectrust-analyzer cargo qrcode 0.14.1 types/impl#[Color]select().('\u{2588}', ' ')
16 }
17
18 fn strlenrust-analyzer cargo qrcode 0.14.1 render/string/impl#[char][Element]strlen().(self) -> usize {
19 self.len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().()
20 }
21
22 fn append_to_stringrust-analyzer cargo qrcode 0.14.1 render/string/impl#[char][Element]append_to_string().(self, string: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
23 string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(self);
24 }
25 }
26
27 impl<'a> Elementrust-analyzer cargo qrcode 0.14.1 render/string/Element# for &'a str {
28 fn default_colorrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`&'a str`][Element]default_color().(color: Colorrust-analyzer cargo qrcode 0.14.1 types/Color#) -> Selfrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`&'a str`][Element] {
29 color.selectrust-analyzer cargo qrcode 0.14.1 types/impl#[Color]select().("\u{2588}", " ")
30 }
31
32 fn strlenrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`&'a str`][Element]strlen().(self) -> usize {
33 self.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()
34 }
35
36 fn append_to_stringrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`&'a str`][Element]append_to_string().(self, string: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
37 string.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(self);
38 }
39 }
40
41 #[doc(hidden)]
42 pub struct Canvasrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#<P: Elementrust-analyzer cargo qrcode 0.14.1 render/string/Element#> {
43 bufferrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#buffer.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<P>,
44 widthrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#width.: usize,
45 dark_pixelrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#dark_pixel.: P,
46 dark_cap_incrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#dark_cap_inc.: isize,
47 capacityrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#capacity.: isize,
48 }
49
50 impl<P: Elementrust-analyzer cargo qrcode 0.14.1 render/string/Element#> Pixelrust-analyzer cargo qrcode 0.14.1 render/Pixel# for P {
51 type Canvasrust-analyzer cargo qrcode 0.14.1 render/string/impl#[P][Pixel]Canvas# = Canvasrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#<Selfrust-analyzer cargo qrcode 0.14.1 render/string/impl#[P][Pixel]>;
52 type Imagerust-analyzer cargo qrcode 0.14.1 render/string/impl#[P][Pixel]Image# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#;
53
54 fn default_unit_sizerust-analyzer cargo qrcode 0.14.1 render/string/impl#[P][Pixel]default_unit_size().() -> (u32, u32) {
55 (1, 1)
56 }
57
58 fn default_colorrust-analyzer cargo qrcode 0.14.1 render/string/impl#[P][Pixel]default_color().(color: Colorrust-analyzer cargo qrcode 0.14.1 types/Color#) -> Selfrust-analyzer cargo qrcode 0.14.1 render/string/impl#[P][Pixel] {
59 <Selfrust-analyzer cargo qrcode 0.14.1 render/string/impl#[P][Pixel] as Elementrust-analyzer cargo qrcode 0.14.1 render/string/Element#>::default_colorrust-analyzer cargo qrcode 0.14.1 render/string/Element#default_color().(color)
60 }
61 }
62
63 impl<P: Elementrust-analyzer cargo qrcode 0.14.1 render/string/Element#> RenderCanvasrust-analyzer cargo qrcode 0.14.1 render/Canvas# for Canvasrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#<P> {
64 type Pixelrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`Canvas<P>`][Canvas]Pixel# = P;
65 type Imagerust-analyzer cargo qrcode 0.14.1 render/string/impl#[`Canvas<P>`][Canvas]Image# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#;
66
67 fn newrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`Canvas<P>`][Canvas]new().(width: u32, height: u32, dark_pixel: P, light_pixel: P) -> Selfrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`Canvas<P>`][Canvas] {
68 let width = width.as_usizerust-analyzer cargo qrcode 0.14.1 cast/impl#[u32][As]as_usize().();
69 let height = height.as_isizerust-analyzer cargo qrcode 0.14.1 cast/impl#[u32][As]as_isize().();
70 let dark_cap = dark_pixel.strlenrust-analyzer cargo qrcode 0.14.1 render/string/Element#strlen().().as_isizerust-analyzer cargo qrcode 0.14.1 cast/impl#[usize][As]as_isize().();
71 let light_cap = light_pixel.strlenrust-analyzer cargo qrcode 0.14.1 render/string/Element#strlen().().as_isizerust-analyzer cargo qrcode 0.14.1 cast/impl#[usize][As]as_isize().();
72 Selfrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`Canvas<P>`][Canvas] {
73 bufferrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#buffer.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![light_pixel; width *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). height.as_usizerust-analyzer cargo qrcode 0.14.1 cast/impl#[isize][As]as_usize().()],
74 widthrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#width.,
75 dark_pixelrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#dark_pixel.,
76 dark_cap_incrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#dark_cap_inc.: dark_cap rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().light_cap,
77 capacityrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#capacity.: light_cap rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul().width.as_isizerust-analyzer cargo qrcode 0.14.1 cast/impl#[usize][As]as_isize().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Mul<Self>`]mul().height rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Add<Self>`]add().(height rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`Sub<Self>`]sub().1),
78 }
79 }
80
81 fn draw_dark_pixelrust-analyzer cargo qrcode 0.14.1 render/string/impl#[`Canvas<P>`][Canvas]draw_dark_pixel().(&mut self, x: u32, y: u32) {
82 let x = x.as_usizerust-analyzer cargo qrcode 0.14.1 cast/impl#[u32][As]as_usize().();
83 let y = y.as_usizerust-analyzer cargo qrcode 0.14.1 cast/impl#[u32][As]as_usize().();
84 self.capacityrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#capacity. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][`AddAssign<Self>`]add_assign().self.dark_cap_incrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#dark_cap_inc.;
85 self.bufferrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().self.widthrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#width.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = self.dark_pixelrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#dark_pixel.;
86 }
87
88 fn into_imagerust-analyzer cargo qrcode 0.14.1 render/string/impl#[`Canvas<P>`][Canvas]into_image().(self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
89 let mut result = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(self.capacityrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#capacity..as_usizerust-analyzer cargo qrcode 0.14.1 cast/impl#[isize][As]as_usize().());
90 for (i, pixel) in self.bufferrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#buffer..into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
91 if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().0 && i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().self.widthrust-analyzer cargo qrcode 0.14.1 render/string/Canvas#width. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
92 result.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('\n');
93 }
94 pixel.append_to_stringrust-analyzer cargo qrcode 0.14.1 render/string/Element#append_to_string().(&mut result);
95 }
96 result
97 }
98 }
99
100 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
101 fn test_render_to_stringrust-analyzer cargo qrcode 0.14.1 render/string/test_render_to_string().() {
102 use craterust-analyzer cargo qrcode 0.14.1 crate/::renderrust-analyzer cargo qrcode 0.14.1 render/::Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#;
103
104 let colors = &[Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Lightrust-analyzer cargo qrcode 0.14.1 types/Color#Light#, Colorrust-analyzer cargo qrcode 0.14.1 types/Color#::Darkrust-analyzer cargo qrcode 0.14.1 types/Color#Dark#];
105 let image: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# = Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#::<char>::newrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]new().(colors, 2, 1).buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().();
106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&image, " \n \u{2588} \n \u{2588} \n ");
107
108 let image2 = Rendererrust-analyzer cargo qrcode 0.14.1 render/Renderer#::newrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]new().(colors, 2, 1).light_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]light_color().("A").dark_colorrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]dark_color().("!B!").module_dimensionsrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]module_dimensions().(2, 2).buildrust-analyzer cargo qrcode 0.14.1 render/impl#[`Renderer<'a, P>`]build().();
109
110 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
111 &image2,
112 "AAAAAAAA\n\
113 AAAAAAAA\n\
114 AA!B!!B!AAAA\n\
115 AA!B!!B!AAAA\n\
116 AAAA!B!!B!AA\n\
117 AAAA!B!!B!AA\n\
118 AAAAAAAA\n\
119 AAAAAAAA"
120 );
121 }