1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::{
cell::{Cell, RefCell},
rc::{Rc, Weak},
};
use crate::{format::IsFormat, ElementUuid};
use crate::{
format::{FormattedElement, ImageFormat},
text_document::{Element, ElementManager, ElementTrait, ModelError},
Block,
};
#[derive(Default, Clone, Debug)]
pub struct Image {
uuid: Cell<ElementUuid>,
element_manager: Weak<ElementManager>,
text: RefCell<String>,
image_format: RefCell<ImageFormat>,
}
impl PartialEq for Image {
fn eq(&self, other: &Self) -> bool {
self.uuid == other.uuid && self.image_format == other.image_format
}
}
impl Image {
pub(crate) fn new(element_manager: Weak<ElementManager>) -> Self {
Image {
element_manager,
uuid: Default::default(),
text: RefCell::new("\u{FFFC}".to_string()),
..Default::default()
}
}
pub fn uuid(&self) -> ElementUuid {
self.uuid.get()
}
pub(crate) fn image_format(&self) -> ImageFormat {
self.format()
}
pub fn plain_text(&self) -> String {
" ".to_string()
}
pub fn text_length(&self) -> usize {
1
}
fn parent_bloc_rc(&self) -> Rc<Block> {
let element_manager = self.element_manager.upgrade().unwrap();
match element_manager
.get_parent_element_using_uuid(self.uuid())
.unwrap()
{
Element::BlockElement(block) => block,
_ => unreachable!(),
}
}
pub fn position_in_block(&self) -> usize {
let parent_block = self.parent_bloc_rc();
parent_block.position_of_child(self.uuid())
}
pub fn start(&self) -> usize {
let parent_block = self.parent_bloc_rc();
parent_block.position() + self.position_in_block()
}
pub fn end(&self) -> usize {
self.start() + self.text_length()
}
}
impl ElementTrait for Image {
fn set_uuid(&self, uuid: usize) {
self.uuid.set(uuid);
}
fn verify_rule_with_parent(&self, parent_element: &Element) -> Result<(), ModelError> {
match parent_element {
Element::FrameElement(_) => Err(ModelError::WrongParent),
Element::BlockElement(_) => Ok(()),
Element::TextElement(_) => Err(ModelError::WrongParent),
Element::ImageElement(_) => Err(ModelError::WrongParent),
}
}
}
impl FormattedElement<ImageFormat> for Image {
fn format(&self) -> ImageFormat {
self.image_format.borrow().clone()
}
fn set_format(&self, format: &ImageFormat) -> Result<(), ModelError> {
self.image_format.replace(format.clone());
Ok(())
}
fn merge_format(&self, format: &ImageFormat) -> Result<ImageFormat, ModelError> {
self.image_format.borrow_mut().merge(format)
}
}
#[cfg(test)]
mod tests {
use crate::InsertMode;
use super::*;
#[test]
fn basics() {
let image = Image::new(Weak::new());
assert_eq!(image.uuid(), 0);
assert_eq!(image.plain_text(), " ");
assert_eq!(image.text_length(), 1);
assert_eq!(image.image_format(), ImageFormat::new());
let image_bis = Image::new(Weak::new());
assert_eq!(image, image_bis);
}
#[test]
fn position() {
let element_manager_rc = ElementManager::new_rc();
ElementManager::create_root_frame(element_manager_rc.clone());
let image = element_manager_rc
.insert_new_image(1, InsertMode::AsChild)
.unwrap();
assert_eq!(image.parent_bloc_rc().uuid(), 1);
assert_eq!(image.start(), 0);
assert_eq!(image.end(), 1);
}
}