Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Jan 11, 2024
1 parent 7624b1e commit a7a5aca
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 68 deletions.
53 changes: 30 additions & 23 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ tunet-helper = { path = "tunet-helper" }
tunet-model = { path = "tunet-model" }
tunet-settings = { path = "tunet-settings" }

async-trait = "0.1"
once_cell = "1"

anyhow = "1"
Expand Down Expand Up @@ -71,5 +70,5 @@ objc = "0.2"
core-foundation = "0.9"
system-configuration = "0.5"

windows = "0.51"
windows = "0.52"
winresource = "0.1"
2 changes: 1 addition & 1 deletion tunet-flutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void main() async {
class MyApp extends StatelessWidget {
final ManagedRuntime runtime;

const MyApp({Key? key, required this.runtime}) : super(key: key);
const MyApp({super.key, required this.runtime});

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion tunet-flutter/lib/views/about_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:shimmer_animation/shimmer_animation.dart';
import 'package:url_launcher/url_launcher_string.dart';

class AboutCard extends StatelessWidget {
const AboutCard({Key? key}) : super(key: key);
const AboutCard({super.key});

@override
Widget build(BuildContext context) {
Expand Down
2 changes: 1 addition & 1 deletion tunet-flutter/lib/views/details_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:shimmer_animation/shimmer_animation.dart';
import '../runtime.dart';

class DetailsCard extends StatefulWidget {
const DetailsCard({Key? key}) : super(key: key);
const DetailsCard({super.key});

@override
State<StatefulWidget> createState() => _DetailsCardState();
Expand Down
11 changes: 4 additions & 7 deletions tunet-flutter/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ use chrono::Datelike;
use flutter_rust_bridge::{frb, RustOpaque, StreamSink};

pub use netstatus::NetStatus;
pub use std::{
net::Ipv4Addr,
sync::{Arc, Mutex},
};
pub use std::{net::Ipv4Addr, sync::Mutex};
pub use tokio::{runtime::Handle, sync::mpsc};
pub use tunet_helper::{
usereg::{NetDateTime, NetDetail, NetUser},
Balance, Duration as NewDuration, Flux, NaiveDate, NaiveDateTime, NaiveDuration as Duration,
NetFlux, NetState,
usereg::{NetDateTime, NetDetail},
Balance, Duration as NewDuration, Flux, NaiveDateTime, NaiveDuration as Duration, NetFlux,
NetState,
};
pub use tunet_model::{Action, DetailDaily, Model, UpdateMsg};

Expand Down
1 change: 1 addition & 0 deletions tunet-flutter/native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod api;
#[allow(clippy::unnecessary_literal_unwrap)]
mod bridge_generated;
1 change: 0 additions & 1 deletion tunet-gui/ui/about.slint
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export global AboutModel {
in property <[[StandardListViewItem]]> deps: [
[ { text: "anyhow" }, { text: "MIT"} ],
[ { text: "async-stream" }, { text: "MIT"} ],
[ { text: "async-trait" }, { text: "MIT"} ],
[ { text: "chrono" }, { text: "MIT"} ],
[ { text: "clap" }, { text: "MIT"} ],
[ { text: "console" }, { text: "MIT"} ],
Expand Down
2 changes: 0 additions & 2 deletions tunet-helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ repository.workspace = true
[dependencies]
authtea = { workspace = true }
netstatus = { workspace = true }
async-trait = { workspace = true }
async-stream = "0.3"
futures-core = { workspace = true }
reqwest = { version = "0.11", default-features = false, features = [
Expand All @@ -29,7 +28,6 @@ mac_address2 = { workspace = true }
once_cell = { workspace = true }
regex = { version = "1", default-features = false, features = ["std"] }
thiserror = { workspace = true }
enum_dispatch = { workspace = true }
allo-isolate = { version = "0.1", optional = true }

[target.'cfg(target_os = "android")'.dependencies]
Expand Down
5 changes: 2 additions & 3 deletions tunet-helper/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ impl<U: AuthConnectUri + Send + Sync> AuthConnect<U> {
}
}

#[async_trait]
impl<U: AuthConnectUri + Send + Sync> TUNetHelper for AuthConnect<U> {
async fn login(&self, u: &str, p: &str) -> NetHelperResult<String> {
let ac_id = self.get_ac_id().await.unwrap_or(1);
Ok(self.try_login(ac_id, u, p).await?)
self.try_login(ac_id, u, p).await
}

async fn logout(&self, u: &str) -> NetHelperResult<String> {
Expand All @@ -154,7 +153,7 @@ impl<U: AuthConnectUri + Send + Sync> TUNetHelper for AuthConnect<U> {

async fn flux(&self) -> NetHelperResult<NetFlux> {
let res = self.client.get(U::flux_uri()).send().await?;
Ok(res.text().await?.parse()?)
res.text().await?.parse()
}
}

Expand Down
50 changes: 36 additions & 14 deletions tunet-helper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![forbid(unsafe_code)]

use async_trait::async_trait;
use enum_dispatch::enum_dispatch;
use std::fmt::{Display, Formatter};
use std::future::Future;
use thiserror::Error;

pub use chrono::{
Expand Down Expand Up @@ -180,28 +179,51 @@ impl std::str::FromStr for NetState {
}
}

#[async_trait]
#[enum_dispatch(TUNetConnect)]
pub trait TUNetHelper: Send + Sync {
async fn login(&self, u: &str, p: &str) -> NetHelperResult<String>;
async fn logout(&self, u: &str) -> NetHelperResult<String>;
async fn flux(&self) -> NetHelperResult<NetFlux>;
fn login(&self, u: &str, p: &str) -> impl Future<Output = NetHelperResult<String>> + Send;
fn logout(&self, u: &str) -> impl Future<Output = NetHelperResult<String>> + Send;
fn flux(&self) -> impl Future<Output = NetHelperResult<NetFlux>> + Send;
}

#[enum_dispatch]
#[derive(Clone)]
pub enum TUNetConnect {
NetConnect,
Auth4Connect,
Auth6Connect,
Net(NetConnect),
Auth4(Auth4Connect),
Auth6(Auth6Connect),
}

impl TUNetHelper for TUNetConnect {
async fn login(&self, u: &str, p: &str) -> NetHelperResult<String> {
match self {
Self::Net(inner) => TUNetHelper::login(inner, u, p).await,
Self::Auth4(inner) => TUNetHelper::login(inner, u, p).await,
Self::Auth6(inner) => TUNetHelper::login(inner, u, p).await,
}
}

async fn logout(&self, u: &str) -> NetHelperResult<String> {
match self {
TUNetConnect::Net(inner) => TUNetHelper::logout(inner, u).await,
TUNetConnect::Auth4(inner) => TUNetHelper::logout(inner, u).await,
TUNetConnect::Auth6(inner) => TUNetHelper::logout(inner, u).await,
}
}

async fn flux(&self) -> NetHelperResult<NetFlux> {
match self {
TUNetConnect::Net(inner) => TUNetHelper::flux(inner).await,
TUNetConnect::Auth4(inner) => TUNetHelper::flux(inner).await,
TUNetConnect::Auth6(inner) => TUNetHelper::flux(inner).await,
}
}
}

impl TUNetConnect {
pub fn new(s: NetState, client: HttpClient) -> NetHelperResult<TUNetConnect> {
match s {
NetState::Net => Ok(Self::NetConnect(net::NetConnect::new(client))),
NetState::Auth4 => Ok(Self::Auth4Connect(auth::AuthConnect::new(client))),
NetState::Auth6 => Ok(Self::Auth6Connect(auth::AuthConnect::new(client))),
NetState::Net => Ok(Self::Net(net::NetConnect::new(client))),
NetState::Auth4 => Ok(Self::Auth4(auth::AuthConnect::new(client))),
NetState::Auth6 => Ok(Self::Auth6(auth::AuthConnect::new(client))),
_ => Err(NetHelperError::InvalidHost),
}
}
Expand Down
3 changes: 1 addition & 2 deletions tunet-helper/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ impl NetConnect {
}
}

#[async_trait]
impl TUNetHelper for NetConnect {
async fn login(&self, u: &str, p: &str) -> NetHelperResult<String> {
let password_md5 = {
Expand All @@ -43,6 +42,6 @@ impl TUNetHelper for NetConnect {

async fn flux(&self) -> NetHelperResult<NetFlux> {
let res = self.client.get(NET_FLUX_URI).send().await?;
Ok(res.text().await?.parse()?)
res.text().await?.parse()
}
}
1 change: 0 additions & 1 deletion tunet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ tunet-settings = { workspace = true }
anyhow = { workspace = true }
futures-util = { workspace = true }
tokio = { workspace = true, features = ["rt"] }
async-trait = { workspace = true }
mac_address2 = { workspace = true }
clap = { workspace = true }
itertools = { workspace = true }
Expand Down
Loading

0 comments on commit a7a5aca

Please sign in to comment.