Skip to content

Commit e90177e

Browse files
committed
Add optional language field to File
1 parent 5c751a8 commit e90177e

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

echo/openapi.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@
278278
"type": "string",
279279
"description": "The File's content."
280280
},
281+
"language": {
282+
"type": "string",
283+
"nullable": true,
284+
"description": "Syntax highlighting language for the File."
285+
},
281286
"lines": {
282287
"type": "integer",
283288
"description": "Amount of newlines the content has."
@@ -288,7 +293,7 @@
288293
},
289294
"annotations": {
290295
"type": "array",
291-
"description": "Auto-generated notices about the File content.",
296+
"description": "Auto-generated notices about File content.",
292297
"items": {
293298
"$ref": "#/components/schemas/Annotation"
294299
}
@@ -297,6 +302,7 @@
297302
"example": {
298303
"name": "beaver.txt",
299304
"content": "Beavers are soo cute!",
305+
"language": "plaintext",
300306
"lines": 1,
301307
"characters": 21,
302308
"annotations": []
@@ -372,6 +378,10 @@
372378
"minLength": 1,
373379
"maxLength": 300000,
374380
"description": "The File's content."
381+
},
382+
"language": {
383+
"type": "string",
384+
"description": "Syntax highlighting language for the File."
375385
}
376386
},
377387
"required": ["content"],

echo/schema/01-base.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ psql -v ON_ERROR_STOP=1 --username "echo" --dbname "echo" <<-EOSQL
2222
paste_id TEXT REFERENCES pastes(id) ON DELETE CASCADE,
2323
name TEXT NOT NULL,
2424
content TEXT NOT NULL,
25+
language TEXT, -- highlight.js syntax highlighting language
2526
lines INTEGER NOT NULL GENERATED ALWAYS AS (LENGTH(REGEXP_REPLACE(content, '[^\n]', '', 'g')) + 1) STORED,
2627
characters INTEGER NOT NULL GENERATED ALWAYS AS (LENGTH(content)) STORED
2728
);

echo/src/database/pastes.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl File {
125125
async fn fetch(conn: &mut Connection<PgDatabase>, paste_id: &str) -> Result<Vec<Self>> {
126126
let result = sqlx::query(
127127
"
128-
SELECT id, name, content, lines, characters
128+
SELECT id, name, content, language, lines, characters
129129
FROM files
130130
WHERE paste_id = $1
131131
ORDER BY id ASC
@@ -159,14 +159,15 @@ impl File {
159159
) -> Result<Self> {
160160
let result = sqlx::query(
161161
"
162-
INSERT INTO files (paste_id, name, content)
163-
VALUES ($1, $2, $3)
164-
RETURNING id, name, content, lines, characters
162+
INSERT INTO files (paste_id, name, content, language)
163+
VALUES ($1, $2, $3, $4)
164+
RETURNING id, name, content, language, lines, characters
165165
",
166166
)
167167
.bind(paste_id)
168168
.bind(file.name().or(Some("unknown")))
169169
.bind(file.content())
170+
.bind(file.language())
170171
.fetch_one(&mut **tx)
171172
.await;
172173

@@ -184,11 +185,12 @@ impl File {
184185
fn from_row(row: PgRow, annotations: Vec<Annotation>) -> Self {
185186
let name = row.get("name");
186187
let content = row.get("content");
188+
let language = row.get("language");
187189

188190
let lines = row.get("lines");
189191
let characters = row.get("characters");
190192

191-
File::new(name, content, lines, characters, annotations)
193+
File::new(name, content, language, lines, characters, annotations)
192194
}
193195
}
194196

echo/src/models/pastes.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl FromRow<'_, PgRow> for Annotation {
7777
pub struct File {
7878
name: String,
7979
content: String,
80+
language: Option<String>,
8081

8182
lines: i32,
8283
characters: i32,
@@ -88,13 +89,15 @@ impl File {
8889
pub fn new(
8990
name: String,
9091
content: String,
92+
language: Option<String>,
9193
lines: i32,
9294
characters: i32,
9395
annotations: Vec<Annotation>,
9496
) -> Self {
9597
File {
9698
name,
9799
content,
100+
language,
98101
lines,
99102
characters,
100103
annotations,
@@ -150,19 +153,21 @@ impl Paste {
150153

151154
#[derive(Deserialize, Serialize, Validate)]
152155
pub struct CreateFile<'r> {
153-
#[validate(length(min = 1, max = 32), custom(function = "validate_name"))]
156+
#[validate(length(min = 1, max = 32), custom(function = "no_newlines"))]
154157
name: Option<&'r str>,
155158
#[validate(length(min = 1, max = 300_000))]
156159
// Using String instead of str because of newlines:
157160
// https://github.com/somehowchris/rocket-validation/issues/41
158161
content: String,
162+
#[validate(length(min = 1, max = 32), custom(function = "no_newlines"))]
163+
language: Option<&'r str>,
159164
}
160165

161-
fn validate_name(name: &str) -> result::Result<(), ValidationError> {
166+
fn no_newlines(name: &str) -> result::Result<(), ValidationError> {
162167
if !name.contains("\n") {
163168
Ok(())
164169
} else {
165-
Err(ValidationError::new("Newline in file name."))
170+
Err(ValidationError::new("Found newline in unsupported field."))
166171
}
167172
}
168173

@@ -174,6 +179,10 @@ impl<'r> CreateFile<'r> {
174179
pub fn content(&self) -> &str {
175180
&self.content
176181
}
182+
183+
pub fn language(&self) -> Option<&'r str> {
184+
self.language
185+
}
177186
}
178187

179188
#[derive(Deserialize, Serialize, Validate)]
@@ -215,6 +224,7 @@ impl<'r> CreatePaste<'r> {
215224
let file = CreateFile {
216225
name: None,
217226
content,
227+
language: None,
218228
};
219229

220230
Ok(CreatePaste {

0 commit comments

Comments
 (0)