Skip to content

Commit ddd53a1

Browse files
committed
log: Revert level mapping to being impl From
1 parent 0f8d6ff commit ddd53a1

File tree

1 file changed

+36
-50
lines changed

1 file changed

+36
-50
lines changed

qmetaobject/src/log.rs

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -73,63 +73,49 @@ pub enum QtMsgType {
7373
// QtSystemMsg = QtCriticalMsg
7474
}
7575

76-
/// Mapping from Qt logging levels to Rust logging facade's levels.
77-
///
78-
/// Due to the limited range of levels from both sides,
79-
/// [`QtCriticalMsg`][`QtMsgType`] and [`QtFatalMsg`][`QtMsgType`]
80-
/// both map to [`log::Level::Error`][Level],
81-
/// while [`log::Level::Trace`][Level] is never returned.
82-
///
83-
/// [Level]: https://docs.rs/log/0.4.10/log/enum.Level.html
84-
/// [`QtMsgType`]: https://doc.qt.io/qt-5/qtglobal.html#QtMsgType-enum
85-
pub fn map_level(lvl: QtMsgType) -> Level {
86-
match lvl {
87-
QtDebugMsg => Level::Debug,
88-
QtInfoMsg => Level::Info,
89-
QtWarningMsg => Level::Warn,
90-
QtCriticalMsg => Level::Error,
91-
QtFatalMsg => Level::Error,
92-
// XXX: What are the external guarantees about possible values of QtMsgType?
93-
// XXX: Are they promised to be limited to the valid enum variants?
94-
}
95-
}
96-
97-
/// Mapping back from Rust logging facade's levels to Qt logging levels.
98-
///
99-
/// Not used internally, exists just for completeness of API.
100-
///
101-
/// Due to the limited range of levels from both sides,
102-
/// [`log::Level::Debug`][Level] and [`log::Level::Trace`][Level]
103-
/// both map to [`QtDebugMsg`][`QtMsgType`],
104-
/// while [`QtFatalMsg`][`QtMsgType`] is never returned.
105-
///
106-
/// [Level]: https://docs.rs/log/0.4.10/log/enum.Level.html
107-
/// [`QtMsgType`]: https://doc.qt.io/qt-5/qtglobal.html#QtMsgType-enum
108-
pub fn unmap_level(lvl: Level) -> QtMsgType {
109-
match lvl {
110-
Level::Error => QtCriticalMsg,
111-
Level::Warn => QtWarningMsg,
112-
Level::Info => QtInfoMsg,
113-
Level::Debug => QtDebugMsg,
114-
Level::Trace => QtDebugMsg,
115-
}
116-
}
117-
11876
impl From<QtMsgType> for Level {
119-
/// Delegates to [default][] mapping algorithm.
77+
/// Mapping from Qt logging levels to Rust logging facade's levels.
78+
///
79+
/// Due to the limited range of levels from both sides,
80+
/// [`QtCriticalMsg`][`Qt::QtMsgType`] and [`QtFatalMsg`][`Qt::QtMsgType`]
81+
/// both map to [`log::Level::Error`][Level],
82+
/// while [`log::Level::Trace`][Level] is never returned.
12083
///
121-
/// [default]: ./fn.map_level.html
84+
/// [Level]: https://docs.rs/log/0.4.10/log/enum.Level.html
85+
/// [`Qt::QtMsgType`]: https://doc.qt.io/qt-5/qtglobal.html#QtMsgType-enum
12286
fn from(lvl: QtMsgType) -> Self {
123-
map_level(lvl)
87+
match lvl {
88+
QtDebugMsg => Level::Debug,
89+
QtInfoMsg => Level::Info,
90+
QtWarningMsg => Level::Warn,
91+
QtCriticalMsg => Level::Error,
92+
QtFatalMsg => Level::Error,
93+
// XXX: What are the external guarantees about possible values of QtMsgType?
94+
// XXX: Are they promised to be limited to the valid enum variants?
95+
}
12496
}
12597
}
12698

12799
impl From<Level> for QtMsgType {
128-
/// Delegates to [default][] mapping algorithm.
100+
/// Mapping back from Rust logging facade's levels to Qt logging levels.
101+
///
102+
/// Not used internally, exists just for completeness of API.
129103
///
130-
/// [default]: ./fn.unmap_level.html
104+
/// Due to the limited range of levels from both sides,
105+
/// [`log::Level::Debug`][Level] and [`log::Level::Trace`][Level]
106+
/// both map to [`QtDebugMsg`][`Qt::QtMsgType`],
107+
/// while [`QtFatalMsg`][`Qt::QtMsgType`] is never returned.
108+
///
109+
/// [Level]: https://docs.rs/log/0.4.10/log/enum.Level.html
110+
/// [`Qt::QtMsgType`]: https://doc.qt.io/qt-5/qtglobal.html#QtMsgType-enum
131111
fn from(lvl: Level) -> Self {
132-
unmap_level(lvl)
112+
match lvl {
113+
Level::Error => QtCriticalMsg,
114+
Level::Warn => QtWarningMsg,
115+
Level::Info => QtInfoMsg,
116+
Level::Debug => QtDebugMsg,
117+
Level::Trace => QtDebugMsg,
118+
}
133119
}
134120
}
135121

@@ -184,14 +170,14 @@ extern "C" fn log_capture(msg_type: QtMsgType,
184170
/// [Rust logging facade][log].
185171
///
186172
/// Most metadata from Qt logging context is retained and passed to [`log::Record`][].
187-
/// Logging levels are mapped with the [default][map_level] algorithm.
173+
/// Logging levels are mapped with the default [`From`][lvl] implementation.
188174
///
189175
/// This function may be called more than once.
190176
///
191177
/// [qt-log]: https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
192178
/// [log]: https://docs.rs/log
193179
/// [`log::Record`]: https://docs.rs/log/0.4.10/log/struct.Record.html
194-
/// [map_level]: ./fn.map_level.html
180+
/// [lvl]: ./struct.QtMsgType.html
195181
pub fn init_qt_to_rust() {
196182
// The reason it is named so complex instead of simple `init` is that
197183
// such descriptive name is future-proof. Consider if someone someday

0 commit comments

Comments
 (0)