Skip to content

Commit

Permalink
long_version field added
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jan 12, 2024
1 parent 0db65a9 commit 39f49d8
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 6 deletions.
3 changes: 2 additions & 1 deletion plugins/example-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use zenoh::plugins::{RunningPluginTrait, ZenohPlugin};
use zenoh::prelude::r#async::*;
use zenoh::runtime::Runtime;
use zenoh_core::zlock;
use zenoh_plugin_trait::{plugin_version, Plugin, PluginControl};
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin, PluginControl};
use zenoh_result::{bail, ZResult};

// The struct implementing the ZenohPlugin and ZenohPlugin traits
Expand All @@ -46,6 +46,7 @@ impl Plugin for ExamplePlugin {
// A mandatory const to define, in case of the plugin is built as a standalone executable
const DEFAULT_NAME: &'static str = "example";
const PLUGIN_VERSION: &'static str = plugin_version!();
const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();

// The first operation called by zenohd on the plugin
fn start(name: &str, runtime: &Self::StartArgs) -> ZResult<Self::Instance> {
Expand Down
3 changes: 2 additions & 1 deletion plugins/example-storage-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use zenoh_backend_traits::{
Capability, History, Persistence, Storage, StorageInsertionResult, StoredData, Volume,
VolumeInstance,
};
use zenoh_plugin_trait::{plugin_version, Plugin};
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin};
use zenoh_result::ZResult;

zenoh_plugin_trait::declare_plugin!(ExampleBackend);
Expand All @@ -39,6 +39,7 @@ impl Plugin for ExampleBackend {

const DEFAULT_NAME: &'static str = "example_backend";
const PLUGIN_VERSION: &'static str = plugin_version!();
const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();
}

pub struct ExampleBackend {}
Expand Down
3 changes: 2 additions & 1 deletion plugins/zenoh-plugin-rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use zenoh::query::{QueryConsolidation, Reply};
use zenoh::runtime::Runtime;
use zenoh::selector::TIME_RANGE_KEY;
use zenoh::Session;
use zenoh_plugin_trait::{plugin_version, Plugin, PluginControl};
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin, PluginControl};
use zenoh_result::{bail, zerror, ZResult};

mod config;
Expand Down Expand Up @@ -219,6 +219,7 @@ impl Plugin for RestPlugin {
type Instance = zenoh::plugins::RunningPlugin;
const DEFAULT_NAME: &'static str = "rest";
const PLUGIN_VERSION: &'static str = plugin_version!();
const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();

fn start(name: &str, runtime: &Self::StartArgs) -> ZResult<zenoh::plugins::RunningPlugin> {
// Try to initiate login.
Expand Down
2 changes: 2 additions & 0 deletions plugins/zenoh-plugin-storage-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use zenoh_backend_traits::config::StorageConfig;
use zenoh_backend_traits::config::VolumeConfig;
use zenoh_backend_traits::VolumeInstance;
use zenoh_core::zlock;
use zenoh_plugin_trait::plugin_long_version;
use zenoh_plugin_trait::plugin_version;
use zenoh_plugin_trait::Plugin;
use zenoh_plugin_trait::PluginControl;
Expand All @@ -57,6 +58,7 @@ impl ZenohPlugin for StoragesPlugin {}
impl Plugin for StoragesPlugin {
const DEFAULT_NAME: &'static str = "storage_manager";
const PLUGIN_VERSION: &'static str = plugin_version!();
const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();

type StartArgs = Runtime;
type Instance = zenoh::plugins::RunningPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh::prelude::r#async::*;
use zenoh::time::Timestamp;
use zenoh_backend_traits::config::{StorageConfig, VolumeConfig};
use zenoh_backend_traits::*;
use zenoh_plugin_trait::Plugin;
use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin};
use zenoh_result::ZResult;

use crate::MEMORY_BACKEND_NAME;
Expand All @@ -33,7 +33,8 @@ impl Plugin for MemoryBackend {
type Instance = VolumeInstance;

const DEFAULT_NAME: &'static str = MEMORY_BACKEND_NAME;
const PLUGIN_VERSION: &'static str = env!("CARGO_PKG_VERSION");
const PLUGIN_VERSION: &'static str = plugin_version!();
const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();

fn start(_: &str, args: &VolumeConfig) -> ZResult<VolumeInstance> {
Ok(Box::new(MemoryBackend {
Expand Down
3 changes: 3 additions & 0 deletions plugins/zenoh-plugin-trait/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatus
fn version(&self) -> Option<&str> {
self.0.version()
}
fn long_version(&self) -> Option<&str> {
self.0.long_version()
}
fn path(&self) -> &str {
self.0.path()
}
Expand Down
6 changes: 5 additions & 1 deletion plugins/zenoh-plugin-trait/src/manager/dynamic_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct DynamicPluginStarter<StartArgs, Instance> {
_lib: Library,
path: PathBuf,
plugin_version: &'static str,
plugin_long_version: &'static str,
vtable: PluginVTable<StartArgs, Instance>,
}

Expand Down Expand Up @@ -90,6 +91,7 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance>
_lib: lib,
path,
plugin_version: plugin_compatibility_record.plugin_version(),
plugin_long_version: plugin_compatibility_record.plugin_long_version(),
vtable,
})
}
Expand Down Expand Up @@ -130,7 +132,9 @@ impl<StartArgs: PluginStartArgs, Instance: PluginInstance> PluginStatus
fn version(&self) -> Option<&str> {
self.starter.as_ref().map(|v| v.plugin_version)
}

fn long_version(&self) -> Option<&str> {
self.starter.as_ref().map(|v| v.plugin_long_version)
}
fn path(&self) -> &str {
if let Some(starter) = &self.starter {
starter.path()
Expand Down
3 changes: 3 additions & 0 deletions plugins/zenoh-plugin-trait/src/manager/static_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ where
fn version(&self) -> Option<&str> {
Some(P::PLUGIN_VERSION)
}
fn long_version(&self) -> Option<&str> {
Some(P::PLUGIN_LONG_VERSION)
}
fn path(&self) -> &str {
"<static>"
}
Expand Down
17 changes: 17 additions & 0 deletions plugins/zenoh-plugin-trait/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub trait PluginStatus {
fn name(&self) -> &str;
/// Returns the version of the loaded plugin (usually the version of the plugin's crate)
fn version(&self) -> Option<&str>;
/// Returns the long version of the loaded plugin (usually the version of the plugin's crate + git commit hash)
fn long_version(&self) -> Option<&str>;
/// Returns the path of the loaded plugin
fn path(&self) -> &str;
/// Returns the plugin's state (Declared, Loaded, Started)
Expand All @@ -78,6 +80,7 @@ pub struct PluginStatusRec<'a> {
pub name: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<Cow<'a, str>>,
pub long_version: Option<Cow<'a, str>>,
pub path: Cow<'a, str>,
pub state: PluginState,
pub report: PluginReport,
Expand All @@ -90,6 +93,9 @@ impl PluginStatus for PluginStatusRec<'_> {
fn version(&self) -> Option<&str> {
self.version.as_deref()
}
fn long_version(&self) -> Option<&str> {
self.long_version.as_deref()
}
fn path(&self) -> &str {
&self.path
}
Expand All @@ -107,6 +113,7 @@ impl<'a> PluginStatusRec<'a> {
Self {
name: Cow::Borrowed(plugin.name()),
version: plugin.version().map(Cow::Borrowed),
long_version: plugin.long_version().map(Cow::Borrowed),
path: Cow::Borrowed(plugin.path()),
state: plugin.state(),
report: plugin.report(),
Expand All @@ -117,6 +124,7 @@ impl<'a> PluginStatusRec<'a> {
PluginStatusRec {
name: Cow::Owned(self.name.into_owned()),
version: self.version.map(|v| Cow::Owned(v.into_owned())),
long_version: self.long_version.map(|v| Cow::Owned(v.into_owned())),
path: Cow::Owned(self.path.into_owned()),
state: self.state,
report: self.report,
Expand Down Expand Up @@ -166,12 +174,21 @@ pub trait Plugin: Sized + 'static {
const DEFAULT_NAME: &'static str;
/// Plugin's version. Used only for information purposes. It's recommended to use [plugin_version!] macro to generate this string.
const PLUGIN_VERSION: &'static str;
/// Plugin's long version (with git commit hash). Used only for information purposes. It's recommended to use [plugin_long_version!] macro to generate this string.
const PLUGIN_LONG_VERSION: &'static str;
/// Starts your plugin. Use `Ok` to return your plugin's control structure
fn start(name: &str, args: &Self::StartArgs) -> ZResult<Self::Instance>;
}

#[macro_export]
macro_rules! plugin_version {
() => {
env!("CARGO_PKG_VERSION")
};
}

#[macro_export]
macro_rules! plugin_long_version {
() => {
git_version::git_version!(prefix = "v", cargo_prefix = "v")
};
Expand Down
7 changes: 7 additions & 0 deletions plugins/zenoh-plugin-trait/src/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct Compatibility {
start_args_version: StructVersion,
instance_version: StructVersion,
plugin_version: &'static str,
plugin_long_version: &'static str,
}

impl Compatibility {
Expand All @@ -82,12 +83,14 @@ impl Compatibility {
let start_args_version = StructVersion::new::<StartArgsType>();
let instance_version = StructVersion::new::<InstanceType>();
let plugin_version = PluginType::PLUGIN_VERSION;
let plugin_long_version = PluginType::PLUGIN_LONG_VERSION;
Self {
rust_version,
vtable_version,
start_args_version,
instance_version,
plugin_version,
plugin_long_version,
}
}
pub fn with_empty_plugin_version<
Expand All @@ -104,11 +107,15 @@ impl Compatibility {
start_args_version,
instance_version,
plugin_version: "",
plugin_long_version: "",
}
}
pub fn plugin_version(&self) -> &'static str {
self.plugin_version
}
pub fn plugin_long_version(&self) -> &'static str {
self.plugin_long_version
}
/// Returns true if rust compiler and structures version are exactly the same and
/// plugin version is compatible with the requested version range in the configuration file
pub fn are_compatible(&self, other: &Self) -> bool {
Expand Down

0 comments on commit 39f49d8

Please sign in to comment.