Skip to content

Commit bf30c57

Browse files
committed
Add constructors to the remaining models
1 parent 667b9f7 commit bf30c57

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

echo/src/database/pastes.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,23 +230,16 @@ impl Annotation {
230230
",
231231
)
232232
.bind(file_id)
233-
.bind(scan.head.line)
234-
.bind(scan.head.char)
235-
.bind(scan.tail.line)
236-
.bind(scan.tail.char)
233+
.bind(scan.head.line())
234+
.bind(scan.head.char())
235+
.bind(scan.tail.line())
236+
.bind(scan.tail.char())
237237
.bind(&content)
238238
.execute(&mut **tx)
239239
.await;
240240

241241
match result {
242-
Ok(_) => {
243-
let value = Annotation {
244-
head: scan.head,
245-
tail: scan.tail,
246-
content,
247-
};
248-
annotations.push(value)
249-
}
242+
Ok(_) => annotations.push(Annotation::new(scan.head, scan.tail, content)),
250243
Err(_) => return Err(HTTPError::new(500, "Failed to create paste.")),
251244
};
252245
}

echo/src/models/pastes.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,39 @@ use crate::result::{HTTPError, Result};
1010

1111
#[derive(Serialize)]
1212
pub struct Position {
13-
pub line: i32,
14-
pub char: i32,
13+
line: i32,
14+
char: i32,
15+
}
16+
17+
impl Position {
18+
pub fn line(&self) -> i32 {
19+
self.line
20+
}
21+
22+
pub fn char(&self) -> i32 {
23+
self.char
24+
}
25+
26+
pub fn new(line: i32, char: i32) -> Self {
27+
Position { line, char }
28+
}
1529
}
1630

1731
#[derive(Serialize)]
1832
pub struct Annotation {
19-
pub head: Position,
20-
pub tail: Position,
21-
pub content: String,
33+
head: Position,
34+
tail: Position,
35+
content: String,
36+
}
37+
38+
impl Annotation {
39+
pub fn new(head: Position, tail: Position, content: String) -> Self {
40+
Annotation {
41+
head,
42+
tail,
43+
content,
44+
}
45+
}
2246
}
2347

2448
impl FromRow<'_, PgRow> for Annotation {

echo/src/scanners.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ fn find_position(content: &str, position: usize) -> Position {
1717
let haystack = &content[0..position];
1818
let (idx, line) = haystack.split('\n').enumerate().last().unwrap();
1919

20-
Position {
21-
line: idx as i32,
22-
char: line.len() as i32,
23-
}
20+
Position::new(idx as i32, line.len() as i32)
2421
}
2522

2623
pub struct ScanResult<'r> {

0 commit comments

Comments
 (0)