Skip to content

Update quick start example. #130

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

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ Before writing the code, we first prepare the dependency and startup code.
1. Add the metadata to the `Cargo.toml` to build the `.so` file.

```toml
# Cargo.toml

[lib]
crate-type = ["cdylib"]
```
Expand Down Expand Up @@ -92,8 +90,6 @@ Now let's begin to finish the logic.
1. First, we create `src/errors.rs` to make the `HttpClientException` class:

```rust
/*** src/errors.rs ***/

use phper::{
classes::{ClassEntry, ClassEntity},
errors::{exception_class, Throwable},
Expand Down Expand Up @@ -148,49 +144,45 @@ Now let's begin to finish the logic.
1. Then, create the `HttpClientBuilder` class in `src/client.rs`.

```rust
/*** src/errors.rs ***/

use phper::{
classes::{ClassEntry, ClassEntity},
errors::{exception_class, Throwable},
};

/// The exception class name of extension.
const EXCEPTION_CLASS_NAME: &str = "HttpClient\\HttpClientException";

pub fn make_exception_class() -> ClassEntity<()> {
let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
// The `extends` is same as the PHP class `extends`.
class.extends(exception_class);
class
}

#[derive(Debug, thiserror::Error)]
pub enum HttpClientError {
#[error(transparent)]
Reqwest(reqwest::Error),

#[error("should call '{method_name}()' before call 'body()'")]
ResponseAfterRead { method_name: String },

#[error("should not call 'body()' multi time")]
ResponseHadRead,
}

impl Throwable for HttpClientError {
fn get_class(&self) -> &ClassEntry {
ClassEntry::from_globals(EXCEPTION_CLASS_NAME).unwrap_or_else(|_| exception_class())
}
}

impl From<HttpClientError> for phper::Error {
fn from(e: HttpClientError) -> Self {
phper::Error::throw(e)
}
}

/*** src/client.rs ***/

# use phper::{
# classes::{ClassEntry, ClassEntity},
# errors::{exception_class, Throwable},
# };
#
# /// The exception class name of extension.
# const EXCEPTION_CLASS_NAME: &str = "HttpClient\\HttpClientException";
#
# pub fn make_exception_class() -> ClassEntity<()> {
# let mut class = ClassEntity::new(EXCEPTION_CLASS_NAME);
# // The `extends` is same as the PHP class `extends`.
# class.extends(exception_class);
# class
# }
#
# #[derive(Debug, thiserror::Error)]
# pub enum HttpClientError {
# #[error(transparent)]
# Reqwest(reqwest::Error),
#
# #[error("should call '{method_name}()' before call 'body()'")]
# ResponseAfterRead { method_name: String },
#
# #[error("should not call 'body()' multi time")]
# ResponseHadRead,
# }
#
# impl Throwable for HttpClientError {
# fn get_class(&self) -> &ClassEntry {
# ClassEntry::from_globals(EXCEPTION_CLASS_NAME).unwrap_or_else(|_| exception_class())
# }
# }
#
# impl From<HttpClientError> for phper::Error {
# fn from(e: HttpClientError) -> Self {
# phper::Error::throw(e)
# }
# }
#
use phper::{
alloc::ToRefOwned,
classes::{StaticStateClass, Visibility},
Expand Down