Skip to content

Commit

Permalink
Merge pull request #130 from xsnippet/import-snippet-v2
Browse files Browse the repository at this point in the history
Allow to "import" snippets via POST /v1/snippets/import
  • Loading branch information
malor committed Apr 19, 2021
2 parents ed8dc0b + 4f38bd8 commit b53f77f
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 93 deletions.
80 changes: 40 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn create_app() -> Result<rocket::Rocket, Box<dyn Error>> {
routes::snippets::create_snippet,
routes::snippets::get_snippet,
routes::syntaxes::get_syntaxes,
routes::snippets::import_snippet,
];
Ok(app
.manage(config)
Expand Down
7 changes: 7 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ use crate::web::Output;
#[derive(Debug)]
pub enum ApiError {
BadRequest(String), // ==> HTTP 400 Bad Request
Forbidden(String), // ==> HTTP 403 Forbidden
NotFound(String), // ==> HTTP 404 Not Found
NotAcceptable(&'static str), // ==> HTTP 406 Not Acceptable
Conflict(String), // ==> HTTP 409 Conflict
UnsupportedMediaType(&'static str), // ==> HTTP 415 Unsupported Media Type
InternalError(String), // ==> HTTP 500 Internal Server Error
}
Expand All @@ -31,7 +33,9 @@ impl ApiError {
pub fn reason(&self) -> &str {
match self {
ApiError::BadRequest(msg) => &msg,
ApiError::Forbidden(msg) => &msg,
ApiError::NotAcceptable(msg) => &msg,
ApiError::Conflict(msg) => &msg,
ApiError::NotFound(msg) => &msg,
ApiError::InternalError(msg) => &msg,
ApiError::UnsupportedMediaType(msg) => &msg,
Expand All @@ -42,7 +46,9 @@ impl ApiError {
pub fn status(&self) -> http::Status {
match self {
ApiError::BadRequest(_) => http::Status::BadRequest,
ApiError::Forbidden(_) => http::Status::Forbidden,
ApiError::NotAcceptable(_) => http::Status::NotAcceptable,
ApiError::Conflict(_) => http::Status::Conflict,
ApiError::NotFound(_) => http::Status::NotFound,
ApiError::UnsupportedMediaType(_) => http::Status::UnsupportedMediaType,
ApiError::InternalError(_) => http::Status::InternalServerError,
Expand All @@ -53,6 +59,7 @@ impl ApiError {
impl From<StorageError> for ApiError {
fn from(value: StorageError) -> Self {
match value {
StorageError::Duplicate { id: _ } => ApiError::Conflict(value.to_string()),
StorageError::NotFound { id: _ } => ApiError::NotFound(value.to_string()),
_ => ApiError::InternalError(value.to_string()),
}
Expand Down

0 comments on commit b53f77f

Please sign in to comment.