plctag_async/
lib.rs

1// plctag-rs
2//
3// a rust wrapper of libplctag, with rust style APIs and useful extensions.
4// Copyright: 2022, Joylei <leingliu@gmail.com>
5// License: MIT
6
7/*!
8# plctag-async
9
10async wrapper for `libplctag`.
11
12[![crates.io](https://img.shields.io/crates/v/plctag-async.svg)](https://crates.io/crates/plctag-async)
13[![docs](https://docs.rs/plctag-async/badge.svg)](https://docs.rs/plctag-async)
14[![build](https://github.com/joylei/plctag-rs/workflows/build/badge.svg?branch=master)](https://github.com/joylei/plctag-rs/actions?query=workflow%3A%22build%22)
15[![license](https://img.shields.io/crates/l/plctag.svg)](https://github.com/joylei/plctag-rs/blob/master/LICENSE)
16
17## How to use
18
19Add `plctag-async` to your Cargo.toml
20
21```toml
22[dependencies]
23plctag-async= "0.4"
24```
25
26## Examples
27
28```rust,no_run
29use plctag_async::{Error, AsyncTag};
30use tokio::runtime;
31
32let rt = runtime::Runtime::new().unwrap();
33rt.block_on(async {
34   let path="protocol=ab-eip&plc=controllogix&path=1,0&gateway=192.168.1.120&name=MyTag1&elem_count=1&elem_size=16";// YOUR TAG DEFINITION
35
36   let mut tag = AsyncTag::create(path).await.unwrap();
37   let offset = 0;
38   let value:u16 = tag.read_value(offset).await.unwrap();
39   println!("tag value: {}", value);
40
41   let value = value + 10;
42   tag.write_value(offset, value).await.unwrap();
43});
44```
45
46## License
47
48MIT
49
50*/
51#![warn(missing_docs)]
52
53extern crate plctag_core;
54mod entry;
55
56pub use entry::AsyncTag;
57
58use plctag_core::{RawTag, Status};
59use std::{fmt, sync::Arc};
60
61/// result for [`plctag-async`]
62pub type Result<T> = std::result::Result<T, Error>;
63
64/// errors for [`plctag-async`]
65#[derive(Debug)]
66pub enum Error {
67    /// plc tag error
68    TagError(Status),
69    /// other error
70    Other(Box<dyn std::error::Error + Send + Sync + 'static>),
71}
72
73impl std::error::Error for Error {
74    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
75        match self {
76            Error::TagError(_) => None,
77            Error::Other(e) => Some(e.as_ref()),
78        }
79    }
80}
81
82impl fmt::Display for Error {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        match self {
85            Error::TagError(e) => write!(f, "TagError - {}", e),
86            Error::Other(e) => write!(f, "{}", e),
87        }
88    }
89}
90
91impl From<Status> for Error {
92    fn from(s: Status) -> Self {
93        Error::TagError(s)
94    }
95}