Skip to content

Commit

Permalink
chore: rename 'gservice' into 'service' (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky authored Jul 24, 2024
1 parent 2d140dc commit 87fb50a
Show file tree
Hide file tree
Showing 34 changed files with 77 additions and 95 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use gstd::debug;

struct MyPing;

#[gservice]
#[service]
impl MyPing {
pub const fn new() -> Self {
Self
Expand Down Expand Up @@ -75,7 +75,7 @@ interacting with the network.
Sails architecture for applications is based on a few key concepts.

The first one is *__service__* which is represented by an impl of some Rust struct
marked with the `#[gservice]` attribute. The service main responsibility is
marked with the `#[service]` attribute. The service main responsibility is
implementing some aspect of application business logic. A set of its __public__
methods defined by the impl is essentially a set of remote calls the service exposes
to external consumers. Each such method working over a `&mut self` is treated as
Expand All @@ -84,12 +84,12 @@ treated as a query keeping everything unchanged and returning some data. Both
types of methods can accept some parameters passed by a client and can be synchronous
or asynchronous. All the other service's methods and associated functions are treated
as implementation details and ignored. The code generated behind the service by the
`#[gservice]` attribute decodes an incoming request message and dispatches it to the
`#[service]` attribute decodes an incoming request message and dispatches it to the
appropriate method based on the method's name. On the method's completion, its result
is encoded and returned as a response to a caller.

```rust
#[gservice]
#[service]
impl MyService {
// This is a command
pub fn do_something(&mut self, p1: u32, p2: String) -> &'static [u8] {
Expand Down Expand Up @@ -176,7 +176,7 @@ impl MyProgram {
The same rules are applicable to service method names:

```rust
#[gservice]
#[service]
impl MyPing {
// The `do_ping` method is exposed as `Ping`
#[groute("ping")]
Expand All @@ -196,9 +196,9 @@ impl MyPing {
Sails offers a mechanism to emit events from your service while processing commands.
These events serve as a means to notify off-chain subscribers about changes in
the application state. In Sails, events are configured and emitted on a per-service
basis through the `events` argument of the `#[gservice]` attribute. They are defined
basis through the `events` argument of the `#[service]` attribute. They are defined
by a Rust enum, with each variant representing a separate event and its optional data.
Once a service declares that it emits events, the `#[gservice]` attribute automatically
Once a service declares that it emits events, the `#[service]` attribute automatically
generates the `notify_on` service method. This method can be called by the service
to emit an event. For example:

Expand All @@ -215,7 +215,7 @@ enum MyCounterEvent {
Incremented(u32),
}

#[gservice(events = MyCounterEvent)]
#[service(events = MyCounterEvent)]
impl MyCounter {
pub fn new() -> Self {
Self
Expand All @@ -226,7 +226,7 @@ impl MyCounter {
self.notify_on(MyCounterEvent::Incremented(*counter_mut())).unwrap();
}

// This method is generated by the `#[gservice]` attribute
// This method is generated by the `#[service]` attribute
fn notify_on(&mut self, event: MyCounterEvent) -> Result<()> {
...
}
Expand All @@ -240,7 +240,7 @@ upon the successful completion of the command that emitted it.
### Service Extending (Mixins)

A standout feature of Sails is its capability to extend (or mix in) existing services.
This is facilitated through the use of the `extends` argument in the `#[gservice]`
This is facilitated through the use of the `extends` argument in the `#[service]`
attribute. Consider you have Service `A` and Service `B`, possibly sourced from
external crates, and you aim to integrate their functionalities into a new
Service `C`. This integration would result in methods and events from Services `A`
Expand All @@ -257,7 +257,7 @@ generation process is the earliest when this can be reported as an error. For ex
```rust
struct MyServiceA;

#[gservice]
#[service]
impl MyServiceA {
pub fn do_a(&mut self) {
...
Expand All @@ -266,7 +266,7 @@ impl MyServiceA {

struct MyServiceB;

#[gservice]
#[service]
impl MyServiceB {
pub fn do_b(&mut self) {
...
Expand All @@ -275,7 +275,7 @@ impl MyServiceB {

struct MyServiceC;

#[gservice(extends = [MyServiceA, MyServiceB])]
#[service(extends = [MyServiceA, MyServiceB])]
impl MyServiceC {
// New method
pub fn do_c(&mut self) {
Expand Down Expand Up @@ -341,7 +341,7 @@ struct Output {
m2: String,
}

#[gservice]
#[service]
impl MyService {
pub fn do_something(&mut self, p1: u32, p2: String) -> Output {
...
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/app/src/counter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<'a> CounterService<'a> {
}

// Declare the service can emit events of type CounterEvents.
#[gservice(events = CounterEvents)]
#[service(events = CounterEvents)]
impl<'a> CounterService<'a> {
pub fn add(&mut self, value: u32) -> u32 {
let mut data_mut = self.data.borrow_mut();
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/app/src/dog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl AsRef<MammalService> for DogService {
// - Barked (from DogEvents)
// - Walked (from WalkerEvents)
// See [IDL](/examples/demo/wasm/demo.idl)
#[gservice(extends = [MammalService, WalkerService], events = DogEvents)]
#[service(extends = [MammalService, WalkerService], events = DogEvents)]
impl DogService {
pub fn make_sound(&mut self) -> &'static str {
self.notify_on(DogEvents::Barked).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/app/src/mammal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl MammalService {
}
}

#[gservice]
#[service]
impl MammalService {
pub fn make_sound(&mut self) -> &'static str {
panic!("Not implemented")
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/app/src/ping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sails_rs::prelude::*;
#[derive(Default)]
pub struct PingService(());

#[gservice]
#[service]
impl PingService {
// This is a service command as it works over `&mut self`
pub fn ping(&mut self, input: String) -> Result<String, String> {
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/app/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static mut BYTES: Vec<u8> = Vec::new();
#[derive(Default)]
pub struct ReferenceService(());

#[gservice]
#[service]
impl ReferenceService {
pub fn baked(&self) -> &'static str {
"Static str!"
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/app/src/this_that/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sails_rs::prelude::*;
#[derive(Default)]
pub struct MyService(());

#[gservice]
#[service]
impl MyService {
// This is a service command as it works over `&mut self`
pub async fn do_this(
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/walker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl WalkerService {
}
}

#[gservice(events = WalkerEvents)]
#[service(events = WalkerEvents)]
impl WalkerService {
pub fn walk(&mut self, dx: i32, dy: i32) {
let from = self.position();
Expand Down
2 changes: 1 addition & 1 deletion examples/proxy/src/this_that/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct ThisThatCaller<ThisThatClient> {
this_that: ThisThatClient,
}

#[gservice]
#[service]
impl<ThisThatClient> ThisThatCaller<ThisThatClient>
where
ThisThatClient: ThisThat,
Expand Down
4 changes: 2 additions & 2 deletions examples/rmrk/catalog/app/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use errors::Error;
use parts::{CollectionId, Part, PartId, SlotPart};
use sails_rs::{
collections::{BTreeMap, BTreeSet},
gstd::{gservice, ExecContext},
gstd::{service, ExecContext},
prelude::*,
Result as RtlResult,
};
Expand Down Expand Up @@ -56,7 +56,7 @@ where
}
}

#[gservice]
#[service]
impl<TExecContext> Catalog<TExecContext>
where
TExecContext: ExecContext,
Expand Down
4 changes: 2 additions & 2 deletions examples/rmrk/resource/app/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use resources::{ComposedResource, PartId, Resource, ResourceId};
use sails_rs::{
calls::Query,
collections::HashMap,
gstd::{gservice, ExecContext},
gstd::{service, ExecContext},
prelude::*,
};

Expand Down Expand Up @@ -38,7 +38,7 @@ pub struct ResourceStorage<TExecContext, TCatalogClient> {
}

// Declare the service can emit events of type ResourceStorageEvent
#[gservice(events = ResourceStorageEvent)]
#[service(events = ResourceStorageEvent)]
impl<TExecContext, TCatalogClient> ResourceStorage<TExecContext, TCatalogClient>
where
TExecContext: ExecContext,
Expand Down
12 changes: 6 additions & 6 deletions rs/macros/core/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn parse_gservice_impl(service_impl_tokens: TokenStream) -> ItemImpl {
syn::parse2(service_impl_tokens).unwrap_or_else(|err| {
abort!(
err.span(),
"`gservice` attribute can be applied to impls only: {}",
"`service` attribute can be applied to impls only: {}",
err
)
})
Expand All @@ -67,13 +67,13 @@ fn ensure_single_gservice_on_impl(service_impl: &ItemImpl) {
.path()
.segments
.last()
.map(|s| s.ident == "gservice")
.map(|s| s.ident == "service")
.unwrap_or(false)
});
if attr_gservice.is_some() {
abort!(
service_impl,
"multiple `gservice` attributes on the same impl are not allowed",
"multiple `service` attributes on the same impl are not allowed",
)
}
}
Expand All @@ -84,7 +84,7 @@ fn ensure_single_gservice_by_name(service_impl: &ItemImpl) {
if unsafe { SERVICE_SPANS.get(&type_ident) }.is_some() {
abort!(
service_impl,
"multiple `gservice` attributes on a type with the same name are not allowed"
"multiple `service` attributes on a type with the same name are not allowed"
)
}
unsafe { SERVICE_SPANS.insert(type_ident, service_impl.span()) };
Expand All @@ -94,7 +94,7 @@ fn generate_gservice(args: TokenStream, service_impl: ItemImpl) -> TokenStream {
let service_args = syn::parse2::<ServiceArgs>(args).unwrap_or_else(|err| {
abort!(
err.span(),
"failed to parse `gservice` attribute arguments: {}",
"failed to parse `service` attribute arguments: {}",
err
)
});
Expand All @@ -113,7 +113,7 @@ fn generate_gservice(args: TokenStream, service_impl: ItemImpl) -> TokenStream {
if service_handlers.is_empty() && service_args.base_types().is_empty() {
abort!(
service_impl,
"`gservice` attribute requires impl to define at least one public method or extend another service"
"`service` attribute requires impl to define at least one public method or extend another service"
);
}

Expand Down
22 changes: 2 additions & 20 deletions rs/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
// This file is part of Gear.

// Copyright (C) 2021-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Procedural macros for the `sails` framework.
//! Procedural macros for the `Sails` framework.
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;

#[proc_macro_error]
#[proc_macro_attribute]
pub fn gservice(args: TokenStream, impl_tokens: TokenStream) -> TokenStream {
pub fn service(args: TokenStream, impl_tokens: TokenStream) -> TokenStream {
sails_macros_core::gservice(args.into(), impl_tokens.into()).into()
}

Expand Down
4 changes: 2 additions & 2 deletions rs/macros/tests/gservice_with_basics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use sails_macros::gservice;
use sails_macros::service;
use sails_rs::Encode;

pub(super) struct MyService;

#[gservice]
#[service]
impl MyService {
pub async fn do_this(&mut self, p1: u32, p2: String) -> String {
format!("{p1}: ") + &p2
Expand Down
4 changes: 2 additions & 2 deletions rs/macros/tests/gservice_with_events/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sails_rs::{gstd::gservice, Encode, TypeInfo};
use sails_rs::{gstd::service, Encode, TypeInfo};

#[allow(dead_code)]
pub struct MyServiceWithEvents(pub u8);
Expand All @@ -8,7 +8,7 @@ pub enum MyEvents {
Event1,
}

#[gservice(events = MyEvents)]
#[service(events = MyEvents)]
impl MyServiceWithEvents {
pub fn my_method(&mut self) {
self.notify_on(MyEvents::Event1).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions rs/macros/tests/gservice_with_extends/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sails_rs::{gstd::gservice, Decode, Encode};
use sails_rs::{gstd::service, Decode, Encode};

pub(super) mod base {
use super::*;
Expand All @@ -10,7 +10,7 @@ pub(super) mod base {
#[derive(Clone)]
pub struct Base;

#[gservice]
#[service]
impl Base {
pub fn base_name(&self) -> String {
"base-name".to_string()
Expand Down Expand Up @@ -38,7 +38,7 @@ pub(super) mod extended {
}
}

#[gservice(extends = base::Base)]
#[service(extends = base::Base)]
impl Extended {
pub fn extended_name(&self) -> String {
"extended-name".to_string()
Expand Down
6 changes: 3 additions & 3 deletions rs/macros/tests/gservice_with_extends_and_lifetimes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sails_rs::gstd::gservice;
use sails_rs::gstd::service;

pub(super) mod base {
use super::*;
Expand All @@ -18,7 +18,7 @@ pub(super) mod base {
}
}

#[gservice]
#[service]
impl<'a> BaseWithLifetime<'a> {
pub fn base_name(&self) -> String {
"base-name".to_string()
Expand Down Expand Up @@ -46,7 +46,7 @@ pub(super) mod extended {
}
}

#[gservice(extends = base::BaseWithLifetime<'a>)]
#[service(extends = base::BaseWithLifetime<'a>)]
impl<'a> ExtendedWithLifetime<'a> {
pub fn extended_name(&self) -> String {
"extended-name".to_string()
Expand Down
4 changes: 2 additions & 2 deletions rs/macros/tests/gservice_with_lifecycles_and_generics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use core::marker::PhantomData;
use sails_rs::gstd::gservice;
use sails_rs::gstd::service;

#[derive(Default)]
pub(super) struct MyGenericService<'a, T> {
_a: PhantomData<&'a T>,
}

#[gservice]
#[service]
impl<'a, T> MyGenericService<'a, T>
where
T: Clone,
Expand Down
Loading

0 comments on commit 87fb50a

Please sign in to comment.