-
-
Notifications
You must be signed in to change notification settings - Fork 35
refactor!: extract framework core to cot-core #444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b3da91c
665082c
383b9f5
a6dd5f8
9dee0ae
2da6c5d
725f180
a4fa150
2994eca
266214e
c1c890a
ead28ff
d13c649
9b372af
0536013
ce1c634
cec103e
3aecce1
9fd687f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| [package] | ||
| name = "cot_core" | ||
| version = "0.4.0" | ||
| description = "The Rust web framework for lazy developers - framework core." | ||
| categories = ["web-programming", "web-programming::http-server", "network-programming"] | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| homepage.workspace = true | ||
| repository.workspace = true | ||
| keywords.workspace = true | ||
| readme.workspace = true | ||
| authors.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [dependencies] | ||
| http.workspace = true | ||
| derive_more = { workspace = true, features = ["debug", "deref", "display", "from"] } | ||
| thiserror.workspace = true | ||
| serde.workspace = true | ||
| serde_json.workspace = true | ||
| backtrace.workspace = true | ||
| bytes.workspace = true | ||
| futures-core.workspace = true | ||
| http-body.workspace = true | ||
| http-body-util.workspace = true | ||
| sync_wrapper.workspace = true | ||
| axum.workspace = true | ||
| cot_macros.workspace = true | ||
| askama = { workspace = true, features = ["alloc"] } | ||
| tower-sessions.workspace = true | ||
| serde_path_to_error.workspace = true | ||
| indexmap.workspace = true | ||
| serde_html_form.workspace = true | ||
| form_urlencoded.workspace = true | ||
| tower.workspace = true | ||
| futures-util.workspace = true | ||
|
|
||
| [dev-dependencies] | ||
| async-stream.workspace = true | ||
| cot = { workspace = true, features = ["test"] } | ||
| futures.workspace = true | ||
| tokio.workspace = true | ||
|
|
||
| [features] | ||
| default = [] | ||
| json = [] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //! Error handling types and utilities for Cot applications. | ||
| //! | ||
| //! This module provides error types, error handlers, and utilities for | ||
| //! handling various types of errors that can occur in Cot applications, | ||
| //! including 404 Not Found errors, uncaught panics, and custom error pages. | ||
|
|
||
| pub mod backtrace; | ||
| pub(crate) mod error_impl; | ||
| mod method_not_allowed; | ||
| mod uncaught_panic; | ||
|
|
||
| pub use error_impl::{Error, impl_into_cot_error}; | ||
| pub use method_not_allowed::MethodNotAllowed; | ||
| pub use uncaught_panic::UncaughtPanic; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,6 @@ use crate::StatusCode; | |
| // Need to rename Backtrace to CotBacktrace, because otherwise it triggers special behavior | ||
| // in the thiserror library | ||
| use crate::error::backtrace::{__cot_create_backtrace, Backtrace as CotBacktrace}; | ||
| use crate::error::not_found::NotFound; | ||
|
|
||
| /// An error that can occur while using Cot. | ||
| pub struct Error { | ||
|
|
@@ -151,46 +150,6 @@ impl Error { | |
| Self::internal(error) | ||
| } | ||
|
|
||
| /// Create a new "404 Not Found" error without a message. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use cot::Error; | ||
| /// | ||
| /// let error = Error::not_found(); | ||
| /// ``` | ||
| #[must_use] | ||
| #[deprecated( | ||
| note = "Use `cot::Error::from(cot::error::NotFound::new())` instead", | ||
| since = "0.4.0" | ||
| )] | ||
| pub fn not_found() -> Self { | ||
| Self::from(NotFound::new()) | ||
| } | ||
|
|
||
| /// Create a new "404 Not Found" error with a message. | ||
| /// | ||
| /// Note that the message is only displayed when Cot's debug mode is | ||
| /// enabled. It will not be exposed to the user in production. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use cot::Error; | ||
| /// | ||
| /// let id = 123; | ||
| /// let error = Error::not_found_message(format!("User with id={id} not found")); | ||
| /// ``` | ||
| #[must_use] | ||
| #[deprecated( | ||
| note = "Use `cot::Error::from(cot::error::NotFound::with_message())` instead", | ||
| since = "0.4.0" | ||
| )] | ||
| pub fn not_found_message(message: String) -> Self { | ||
| Self::from(NotFound::with_message(message)) | ||
| } | ||
|
|
||
| /// Returns the HTTP status code associated with this error. | ||
| /// | ||
| /// This method returns the appropriate HTTP status code that should be | ||
|
|
@@ -216,7 +175,7 @@ impl Error { | |
| } | ||
|
|
||
| #[must_use] | ||
| pub(crate) fn backtrace(&self) -> &CotBacktrace { | ||
| pub fn backtrace(&self) -> &CotBacktrace { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an implementation detail and cannot be exposed to users. The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's problematic, it has to be public so that the error page can use it to show backtrace properly. The easiest solution would be to pull that to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the error struct should be kept in the core crate. I can see several approaches here:
mod private {
pub struct Token;
}
pub struct MyStruct;
impl MyStruct {
pub fn restricted_method(&self, _: private::Token) {
println!("Restricted!");
}
}
#[doc(hidden)]
pub fn get_token() -> private::Token {
private::Token
}I'll let you pick the favorite one for the job.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went with option 1. I want to keep this PR as change-light as possible and if it's used like that by other crates, I suppose it's good enough. If it's ever become a problem, we can change it then. |
||
| &self.repr.backtrace | ||
| } | ||
|
|
||
|
|
@@ -319,6 +278,7 @@ impl From<Error> for askama::Error { | |
| } | ||
| } | ||
|
|
||
| #[macro_export] | ||
| macro_rules! impl_into_cot_error { | ||
| ($error_ty:ty) => { | ||
| impl From<$error_ty> for $crate::Error { | ||
|
|
@@ -335,7 +295,7 @@ macro_rules! impl_into_cot_error { | |
| } | ||
| }; | ||
| } | ||
| pub(crate) use impl_into_cot_error; | ||
| pub use impl_into_cot_error; | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| #[error("failed to render template: {0}")] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cot-core crate has a circular dev-dependency on the main
cotcrate. This creates a circular dependency structure wherecotdepends oncot_core, andcot_corehas a dev-dependency oncotfor tests. While this is technically allowed for dev-dependencies in Rust, it can complicate the build process and may cause issues with certain tools or workflows. Consider whether the tests in cot-core can be restructured to avoid depending on the main cot crate, or whether they can be moved to the main cot crate's test suite instead.